Re: declaration of variable in for loop



santosh wrote, On 31/01/08 09:49:
jacob navia wrote:

<snip>

main()
{
int i,j, *pint;
for(i=0;i<10;i++){
int k = i;
printf("%d %p = *%d\n",i,&k,k);
pint=&i;
}
*pint = 789; // Accessing illegal storage
}

This would work on lcc-win since the storage is still valid.
I would not do this since it is absolutely non portable.
Other compilers could implement other strategies.

I hope as a QoI issue lcc-win issues a diagnostic for such uses?

I'm assuming you misread Jacob's code as saying "pint=&k" which is what he intended (as noted in a subsequent post). If so then I would not expect a diagnostic although I would be pleased if the compiler produced one.
--
Flash Gordon
.



Relevant Pages

  • Re: declaration of variable in for loop
    ... int i,j, *pint; ... This would work on lcc-win since the storage is still valid. ... I hope as a QoI issue lcc-win issues a diagnostic for such uses? ...
    (comp.lang.c)
  • Re: a little mistake
    ... Also the use of "inline" for that function is redundant. ... compilers will do it anyways and it isn't portable across all compilers ... int main ... Calling function would be part of the users code. ...
    (comp.lang.c)
  • Re: Hundreds of cases in a switch, optimization?
    ... > In terms of generated machine code, how does hundreds of cases in a switch ... Do compilers or processors do any ... extern int f; ... compiled both with and without optimisation. ...
    (comp.lang.c)
  • Re: one-liner for characater replacement
    ... declaration you can omit the type and it is assumed 'int', ... a qualifier e.g. 'const'. ... unspecified but fixed default-promoted arguments returning int'. ... (Qualification only makes sense for things in storage ...
    (comp.lang.fortran)
  • Re: GCC bitfield packing
    ... The actual amount of storage taken us depends only upon the number of ... The size of the storage unit that those bits are stored ... depend upon the type of the bitfield declaration. ... _Bool, int, signed int, and unsigned int. ...
    (comp.std.c)