Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > to malloc or not to malloc??

Reply
Thread Tools

to malloc or not to malloc??

 
 
Johs32
Guest
Posts: n/a
 
      03-30-2006
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.
 
Reply With Quote
 
 
 
 
Marc Boyer
Guest
Posts: n/a
 
      03-30-2006
Le 30-03-2006, Johs32 <(E-Mail Removed)> a écrit*:
> 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

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
Guest
Posts: n/a
 
      03-30-2006
Johs32 wrote:
> 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.

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-30-2006
Johs32 wrote:
> 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;
}

--
Ian Collins.
by
 
Reply With Quote
 
Captain Winston
Guest
Posts: n/a
 
      03-30-2006

Johs32 wrote:
> I have this code:
>

#include <stdio.h>
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
to malloc or not to malloc? kj C Programming 5 06-05-2009 02:22 PM
porting non-malloc code to malloc micromysore@gmail.com C Programming 3 02-19-2005 05:39 AM
Malloc/Free - freeing memory allocated by malloc Peter C Programming 34 10-22-2004 10:23 AM
free'ing malloc'd structure with malloc'd members John C Programming 13 08-02-2004 11:45 AM
Re: free'ing malloc'd structure with malloc'd members ravi C Programming 0 07-30-2004 12:42 PM



Advertisments