M
Manish Tomar
Hi All,
The following code as per my knowledge should not work:
int* some()
{
int b = 10;
return &b;
}
int main(int argc, char** argv)
{
int* test = NULL;
test = some();
printf("test ; %d\n", *test);
*test = 20;
return 0;
}
as it is returning the address of a variable on stack which should not
be valid once the function returns. However, this code seems to be
working absolutely fine in VC++ 6.0 on x86 processor. Could anyone
please explain what is correct or wrong and why is it working.
Also, in the following statement
char *p = "Hello world";
where is the memory for p allocated? is it on stack, heap or global
area and when is it freed, if so?
Thanks in advance,
Manish
The following code as per my knowledge should not work:
int* some()
{
int b = 10;
return &b;
}
int main(int argc, char** argv)
{
int* test = NULL;
test = some();
printf("test ; %d\n", *test);
*test = 20;
return 0;
}
as it is returning the address of a variable on stack which should not
be valid once the function returns. However, this code seems to be
working absolutely fine in VC++ 6.0 on x86 processor. Could anyone
please explain what is correct or wrong and why is it working.
Also, in the following statement
char *p = "Hello world";
where is the memory for p allocated? is it on stack, heap or global
area and when is it freed, if so?
Thanks in advance,
Manish