to malloc or not to malloc??

J

Johs32

I have this code:

void calc(int *ip)
{
int temp = 333;
*ip = temp;

}

int main(void)
{
int a;
calc(&a);

printf("a's value: %d\n", a);
return 0;

}


it prints the right result but I have learned not to trust this! My question
is can I always be sure that the content of ip = temp?

The reason I ask is that int temp is allocated on the stack and after calc
returns temp no longer exists and therefore I thought that printing a in
main would just by coincidence print the right result.
 
M

Marc Boyer

Le 30-03-2006 said:
I have this code:

void calc(int *ip)
{
int temp = 333;
*ip = temp;

}

int main(void)
{
int a;
calc(&a);

printf("a's value: %d\n", a);
return 0;

}


it prints the right result but I have learned not to trust this! My question
is can I always be sure that the content of ip = temp?

The reason I ask is that int temp is allocated on the stack and after calc
returns temp no longer exists and therefore I thought that printing a in
main would just by coincidence print the right result.

But, when you print it, neither ip nor temp longer exist.
The /value/ of temp (333) have been copied into the object
pointed by ip, which was a. Then, the value 333 have been
copied into a. Then, it prints 333.

Marc Boyer
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Johs32 said:
I have this code:

void calc(int *ip)
{
int temp = 333;
*ip = temp;

}

int main(void)
{
int a;
calc(&a);

printf("a's value: %d\n", a);
return 0;

}


it prints the right result but I have learned not to trust this! My question Who thaught you that ?
is can I always be sure that the content of ip = temp? Yes.

The reason I ask is that int temp is allocated on the stack and after calc
returns temp no longer exists and therefore I thought that printing a in
main would just by coincidence print the right result.

You have copied 'temp' to what int *ip points to, which is 'a' in the
main function. This is perfectly ok and a very useful concept.
 
I

Ian Collins

Johs32 said:
I have this code:

void calc(int *ip)
{
int temp = 333;
*ip = temp;

}

int main(void)
{
int a;
calc(&a);

printf("a's value: %d\n", a);
return 0;

}


it prints the right result but I have learned not to trust this! My question
is can I always be sure that the content of ip = temp?
Yes, you are copying temp to the variable pointed to by ip. Once you've
done this, you can do what you like with temp.
The reason I ask is that int temp is allocated on the stack and after calc
returns temp no longer exists and therefore I thought that printing a in
main would just by coincidence print the right result.

This would be a problem if you had:

int* calc()
{
int temp = 333;
return &temp;
}
 
C

Captain Winston

Johs32 said:
I have this code:
#include said:
void calc(int *ip)
{
int temp = 333;
*ip = temp;

}

int main(void)
{
int a;
calc(&a);

printf("a's value: %d\n", a);
return 0;

}


it prints the right result but I have learned not to trust this! My question
is can I always be sure that the content of ip = temp? Yes.

The reason I ask is that int temp is allocated on the stack and after calc
returns temp no longer exists and therefore I thought that printing a in
main would just by coincidence print the right result.
The result is not by chance. The content of ip is a copy of temp's
value, although temp
have no longer been there when the function calc returned, *ip have
gotten temp's value
which doesn't go away with calc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top