Pointer assigned by a function problem...

J

jamaj

#include <stdlib.h>
#include <stdio.h>

void foo(double *ptr)
{
printf("foo ptr=%p\n",ptr);
ptr = malloc(sizeof(*ptr));
printf("foo ptr=%p\n",ptr);
ptr[0] = 12;
printf("foo value=%f\n",*ptr);
}


int main(int argc, char **argv)
{
double *ptr=NULL;
printf("main ptr=%p\n",ptr);
foo(ptr);
printf("main ptr=%p\n",ptr);
}


Returns:

main ptr=(nil)
foo ptr=(nil)
foo ptr=0x804a008
foo value=12.000000
main ptr=(nil)

Why the pointer in the main function is not updated?

Thnaks in advance.

jamaj
 
I

Ian Collins

jamaj said:
#include <stdlib.h>
#include <stdio.h>

void foo(double *ptr)
{
printf("foo ptr=%p\n",ptr);
ptr = malloc(sizeof(*ptr));
printf("foo ptr=%p\n",ptr);
ptr[0] = 12;
printf("foo value=%f\n",*ptr);
}


int main(int argc, char **argv)
{
double *ptr=NULL;
printf("main ptr=%p\n",ptr);
foo(ptr);
printf("main ptr=%p\n",ptr);
}


Returns:

main ptr=(nil)
foo ptr=(nil)
foo ptr=0x804a008
foo value=12.000000
main ptr=(nil)

Why the pointer in the main function is not updated?
Because you are updating the local copy in foo. ptr in foo is copy of
the one in main.

You have to write

void foo(double **ptr)
{
*ptr = malloc(sizeof(*ptr));
}
 
J

jamaj

I have altered for this version, and it worked. But is this the unique
and right way?

void foo(double **ptr)
{
printf("foo ptr=%p\n",*ptr);
*ptr = malloc(sizeof(**ptr));
printf("foo ptr=%p\n",*ptr);
*ptr[0] = 12;
printf("foo value=%f\n",**ptr);
}
int main(int argc, char **argv)
{
double *ptr=NULL;
printf("main ptr=%p\n",ptr);
foo(&ptr);
printf("main ptr=%p\n",ptr);
printf("main value=%f\n",*ptr);
}

Now the results are:

main ptr=(nil)
foo ptr=(nil)
foo ptr=0x804a008
foo value=12.000000
main ptr=0x804a008
main value=12.000000
 
I

Ian Collins

jamaj wrote:

[please don't top-post]
I have altered for this version, and it worked. But is this the unique
and right way?
Yes.

void foo(double **ptr)
{
printf("foo ptr=%p\n",*ptr);
*ptr = malloc(sizeof(**ptr));
printf("foo ptr=%p\n",*ptr);
*ptr[0] = 12;
printf("foo value=%f\n",**ptr);
}
 
V

vippstar

I have altered for this version, and it worked. But is this the unique
and right way?

void foo(double **ptr)
{
printf("foo ptr=%p\n",*ptr);

*ptr is of type (double *). `p' expects (void *).
Change to:

printf("foo ptr=%p\n", (void *)*ptr);
*ptr = malloc(sizeof(**ptr));

Don't forget that it's possible for malloc to return NULL.
printf("foo ptr=%p\n",*ptr);

Same here; *ptr is (double *), `p' expects (void *).
*ptr[0] = 12;

So here you have a problem, since you don't check if *p is indeed non-
null, and you just dereference it.
Change this to:

if(*ptr == NULL) return;
*ptr[0] = 12;
printf("foo value=%f\n",**ptr);}

int main(int argc, char **argv)
{
double *ptr=NULL;
printf("main ptr=%p\n",ptr);

And here
foo(&ptr);
printf("main ptr=%p\n",ptr);

and here.
printf("main value=%f\n",*ptr);

}

Now the results are:

main ptr=(nil)
foo ptr=(nil)
foo ptr=0x804a008
foo value=12.000000
main ptr=0x804a008
main value=12.000000

Please don't top post.
http://www.catb.org/jargon/html/T/top-post.html
http://www.cs.tut.fi/~jkorpela/usenet/brox.html
http://www.caliburn.nl/topposting.html
http://www.html-faq.com/etiquette/?toppost
etc.
 
J

jamaj

Ah yes, also there's a memory leak, since you never free *p.
So add a free(*p); after the last printf, and then add a return 0;

I think that you already know that these little snippets of code have
only an illustrative purpose, and they did the work - with or without
the memory leaks that you correctly showed. So you can rest, cause my
doubt was kindly removed by Ian Collins.

Thanks again.

jamaj
 
J

jamaj

No they did not.

But they are working in my machine!!! Maybe a bug of gcc... I'm
wasting my time with you. Thanks a lot...

The real program i'm building is now working, and what is important
for me.

Thanks Ian Collins...
Regards,

jamaj
 
B

Barry Schwarz

<snip>

Ah yes, also there's a memory leak, since you never free *p.
So add a free(*p); after the last printf, and then add a return 0;

In main, *p is a double. You meant
free(p);
 
B

Barry Schwarz

But they are working in my machine!!! Maybe a bug of gcc... I'm
wasting my time with you. Thanks a lot...

It is possible you are wasting your time but if so it is because you
are learning bad habits and rejecting efforts to correct them.
The real program i'm building is now working, and what is important
for me.

Refusing to correct undefined behavior just because the program
appears to work is a recipe for disaster.
 

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
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top