Variable Declaration & Initialization :: C++

Started by
18 comments, last by kuphryn 22 years, 3 months ago
quote:Original post by DrPizza
A great many platforms offer a function alloca() (or perhaps _alloca() ), which acts like a malloc() , only it allocates memory on the stack.

True, it is possible to allocate new stack memory, so I shouldn''t have spoken so carelessly. I was only trying to clear-up the common misconception that stack variables are given room on the stack at they time they appear in code. As you know, what really happens is that the compiler allocate memory and locations for those variables at the beginning of the function, regardless of where they are declared.

Besides, alloca is EVIL.

PS, Dr.Pizza, this:
quote:
VC++ 6 predates the C++ standard, and it seems a little unfair to expect it to follow it.

..is possibly the most level-headed thing ever said about Microsoft on this board.
Advertisement
anon wrote "well, C only has one option"

this is not entirely true. C only requires that you declare variables at the beginning of a block--they do not have to be at the beginning of a function or loop or anything.

int main()
{
int x = 3;
printf ("x = %d\n", x);
{
int y = 2;
printf ("y = %d\n", y);
x = y;
}
printf ("x = %d\n", x);
return 0;
}

is legal C, and does not define all the function''s variables at the top of the function.

in response to the original question, people such as Stoffel & Magmai Kai Holmlor have already pointed out why "declaration before use" is preferable.

--michael

i don''t know what "C" you guys are talking about, but ANSI C does NOT allow you to define variables in a function after any code. i.e.

  void foo(void){  int i = 1;  i *= 1;  int j; // error.}  


is illegal.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
jenova:

I beleive he meant more like this...

void foo(void){   int i=3;   i *= 2;   printf("%d",i);   {      int j=2;      printf("*%d=%d",j,j*i);   }} 
quote:Original post by jenova
i don''t know what "C" you guys are talking about, but ANSI C does NOT allow you to define variables in a function after any code. i.e.

    void foo(void){  int i = 1;  i *= 1;  int j; // error.}    


is illegal.

I''m not a C programmer, and generally dislike it (I''m a C++ kind of person), but I believe that you''re wrong on two counts.

(1) ANSI C lets you scope (and hence declare) variables to blocks. I think that a trick used by some C programmers is to do something like this:
  void myFunction(void){    /* declarations */    int a, b, c;    char d, e, f;    struct MyStructType g;    /* some actual code */    a = 2; b = 3; c = a * b;    /* you can''t put any more declarations here */    /* but you can do this: */    do    {        /* declarations */        int h, i, j;        /* some actual code */        h = 4; i = 6; j = h * i;    }    while(0);    return;}  

The do... while block lets you declare additional variables, and further, restricts their scope to that block. So you can have declarations that follow code.

(2) C99 permits you to freely mix code and declarations anyway. Whilst C89 doesn''t (but I believe does permit the above), C99 is much closer to C++ in this regard.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
you are correct. my bad.

but i believe i read it somewhere in my "C Primer" book. i will have to check when i get home.

the code does compile under VC++ 6.0.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
quote:i don''t know what "C" you guys are talking about, but ANSI C does NOT allow you to define variables in a function after any code.


According to the "C" in ANSI/ISO C Standard, ISO/IEC IS 9899, you can.
AP: sweet, you must of missed my "you are correct. my bad".

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Sorry, my bad.
AP: and as "DrPizza" stated, i should have stated you cannot declare variables after code in a code block, according to C98 which my "C Primer" book is based on. okay.


your right, your right, your right. geez.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

This topic is closed to new replies.

Advertisement