 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
kevincw01 Guest
|
Posted: Sun Feb 25, 2007 10:11 am Post subject: automatics? |
|
|
My professor mentioned using automatics in a C program and I've never
heard of them. Searching google came up with nothing (besides
automatic cars). What are they?
Thanks, -Kevin
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Douglas A. Gwyn Guest
|
Posted: Tue Feb 27, 2007 3:02 am Post subject: Re: automatics? |
|
|
"kevincw01" <kevin (AT) netkev (DOT) com> wrote in message
news:clcm-20070225-0010 (AT) plethora (DOT) net...
| Quote: | My professor mentioned using automatics in a C program and I've never
heard of them. Searching google came up with nothing (besides
automatic cars). What are they?
|
Presumably he meant automatic storage duration. C has a
keyword "auto" for this, but it is redundant since automatic
storage duration is evident to the compiler from context.
What automatic storage duration refers to is the instancing of
local variables, which have a lifetime limited to the execution
of the compound-statement block within which they are
declared, including the outer block that constitutes the body
of a function definition. A more colloquial name for this is
"stack variables".
Subclause 6.2.4 of the 1999 C standard explains storage
duration. (The other flavors are "static" and "allocated".)
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Hans-Bernhard Brφker Guest
|
Posted: Tue Feb 27, 2007 3:02 am Post subject: Re: automatics? |
|
|
kevincw01 wrote:
| Quote: | My professor mentioned using automatics in a C program and I've never
heard of them. Searching google came up with nothing (besides
automatic cars).
|
Well, the world existed before Google. Even C did. :-)
"Automatics" is short-hand for "variables of automatic storage
duration". Look it up in your textbook. That's what you (hopefully)
have one for.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
CBFalconer Guest
|
Posted: Tue Feb 27, 2007 3:02 am Post subject: Re: automatics? |
|
|
kevincw01 wrote:
| Quote: |
My professor mentioned using automatics in a C program and I've
never heard of them. Searching google came up with nothing
(besides automatic cars). What are they?
|
Your professors verbiage is sloppy. He is referring to "automatic
storage", which is the type of storage a declaration (without the
static keyword) assigns when found within a function and whose
scope is that function. The other class is static storage, which
is automatically initialized, and allocated storage, which is
effectively static but not initialized (unless secured via calloc).
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
s5n Guest
|
Posted: Tue Feb 27, 2007 3:02 am Post subject: Re: automatics? |
|
|
On Feb 25, 10:14 am, "kevincw01" <k...@netkev.com> wrote:
| Quote: | My professor mentioned using automatics in a C program and I've never
heard of them. Searching google came up with nothing (besides
automatic cars). What are they?
|
He probably meant automatic variables (or "auto vars"). All that means
is variables which you allow the compiler to allocate and deallocate
for you, like so:
void myfunc() {
int auto1;
double auto2;
...
// as opposed to:
void * m = malloc(100);
...
}
m is not "automatic" in the sense that it won't "automatically" be
cleaned up for you. (To be clear, that little definition of
"automatic" is not what the standard says, but it's a convenient way
to think about it, IMO.)
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Joel C. Salomon Guest
|
Posted: Tue Feb 27, 2007 3:02 am Post subject: Re: automatics? |
|
|
kevincw01 wrote:
| Quote: | My professor mentioned using automatics in a C program and I've never
heard of them. Searching google came up with nothing (besides
automatic cars). What are they?
|
Your professor is referring to automatic variables;
http://www.google.com/search?q=automatic+variables turns up plenty of
information about them.
--Joel
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Bob Guest
|
Posted: Tue Feb 27, 2007 3:03 am Post subject: Re: automatics? |
|
|
kevincw01 wrote:
| Quote: | My professor mentioned using automatics in a C program and I've never
heard of them. Searching google came up with nothing (besides
automatic cars). What are they?
Thanks, -Kevin
|
Assuming he's not referring to something like automation (which
is outside scope of C), my guess is auto variables. Variables that
are local to a scope, and no longer exist outside the scope.
For example;
#include <stdio.h>
void f()
{
int i;
/* do something with i, such as */
for (i = 0; i < 1000; ++i)
fprintf(stdout, "%d\n", i);
}
int main()
{
f();
f();
}
i is an auto variable within function f(). It is local, and will
no longer exist when f() returns. The second call of f() will create
a new variable named i, which will cease to exist when f() returns
again.
The auto attribute is the default, so the auto keyword is rarely seen.
But the declaration of i could equivalently be written as
auto int i;
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
David Given Guest
|
Posted: Tue Feb 27, 2007 3:03 am Post subject: Re: automatics? |
|
|
kevincw01 wrote:
| Quote: | My professor mentioned using automatics in a C program and I've never
heard of them. Searching google came up with nothing (besides
automatic cars). What are they?
|
You're probably already using them --- they're just local variables. They're
called automatic because they're allocated on the stack, and so their storage
is automatically freed when they go out of scope.
If you like you can explicitly declare a variable as automatic:
int foo(void)
{
auto int i = 1;
}
(Just like you can declare them 'register' or 'static'.) It doesn't really
gain you anything, though.
--
βββ ο½ο½οΌ ο½ο½ο½ο½ο½ο½ο½οΌο½ο½ο½ βββ http://www.cowlark.com βββββββββββββββββββ
β
β "The first 90% of the code takes the first 90% of the time. The other 10%
β takes the other 90% of the time." --- Anonymous
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Keith Thompson Guest
|
Posted: Thu Mar 01, 2007 8:33 am Post subject: Re: automatics? |
|
|
"s5n" <sgbeal (AT) googlemail (DOT) com> writes:
| Quote: | On Feb 25, 10:14 am, "kevincw01" <k...@netkev.com> wrote:
My professor mentioned using automatics in a C program and I've never
heard of them. Searching google came up with nothing (besides
automatic cars). What are they?
He probably meant automatic variables (or "auto vars"). All that means
is variables which you allow the compiler to allocate and deallocate
for you, like so:
void myfunc() {
int auto1;
double auto2;
...
// as opposed to:
void * m = malloc(100);
...
}
m is not "automatic" in the sense that it won't "automatically" be
cleaned up for you. (To be clear, that little definition of
"automatic" is not what the standard says, but it's a convenient way
to think about it, IMO.)
|
To be painfully precise, m (a pointer object) does have automatic
storage duration, and the memory allocated for it (4 or 8 bytes on
many systems) will be automatically released on exit from the block.
The block of memory allocated by malloc(), on the other hand, is an
object with allocated storage duration, and will not be automatically
deallocated. This block is what m points to (assuming that the
malloc() call succeeded).
--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Bob Guest
|
Posted: Thu Mar 01, 2007 8:35 am Post subject: Re: automatics? |
|
|
kevincw01 wrote:
| Quote: | My professor mentioned using automatics in a C program and I've never
heard of them. Searching google came up with nothing (besides
automatic cars). What are they?
Thanks, -Kevin
|
Assuming he's not referring to something like automation (which
is outside scope of C), my guess is auto variables. Variables that
are local to a scope, and no longer exist outside the scope.
For example;
#include <stdio.h>
void f()
{
int i;
/* do something with i, such as */
for (i = 0; i < 1000; ++i)
fprintf(stdout, "%d\n", i);
}
int main()
{
f();
f();
}
i is an auto variable within function f(). It is local, and will
no longer exist when f() returns. The second call of f() will create
a new variable named i, which will cease to exist when f() returns
again.
The auto attribute is the default, so the auto keyword is rarely seen.
But the declaration of i could equivalently be written as
auto int i;
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
David Thompson Guest
|
Posted: Thu Mar 29, 2007 6:22 am Post subject: Re: automatics? |
|
|
On 26 Feb 2007 21:02:42 GMT, CBFalconer <cbfalconer (AT) yahoo (DOT) com> wrote:
| Quote: | kevincw01 wrote:
My professor mentioned using automatics in a C program and I've
never heard of them. Searching google came up with nothing
(besides automatic cars). What are they?
Your professors verbiage is sloppy. He is referring to "automatic
storage", which is the type of storage a declaration (without the
static keyword) assigns when found within a function and whose
scope is that function.
|
Scope of auto is the immediately containing block, which is sometimes
the (entire) function body and sometimes less. And duration, which is
the important and distinguishing attribute for storage classes, is
from (each) entry into that block until (matching) exit therefrom.
(Except for C99 VLAs which are from declaration to end of block.)
| Quote: | The other class is static storage, which
is automatically initialized,
|
The other (two) classes are static, which is always initialized -- to
zeros, of the appropriate type(s), if you don't write an initializer
-- and has duration for the entire execution of the program, and:
| Quote: | and allocated storage, which is
effectively static but not initialized (unless secured via calloc).
which has duration from when you allocate it until you free it, which |
need not be as much as the entire execution of the program but also
may be more than the execution of a given block, and is not
necessarily initialized if you use malloc but is initialized to zero
bits (which may differ from a type-appropriate zero in some cases) if
you use calloc, and is partially initialized (only up to the existing
data) if you use realloc.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|