Connecting Tech Pros Worldwide Forums | Help | Site Map

Pointer assigned by a function problem...

jamaj
Guest
 
Posts: n/a
#1: Sep 3 '08
#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

Ian Collins
Guest
 
Posts: n/a
#2: Sep 3 '08

re: Pointer assigned by a function problem...


jamaj wrote:
Quote:
#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));
}

--
Ian Collins.
jamaj
Guest
 
Posts: n/a
#3: Sep 3 '08

re: Pointer assigned by a function problem...


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


On 2 set, 21:53, jamaj <jama...@gmail.comwrote:
Quote:
#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
Ian Collins
Guest
 
Posts: n/a
#4: Sep 3 '08

re: Pointer assigned by a function problem...


jamaj wrote:

[please don't top-post]
Quote:
I have altered for this version, and it worked. But is this the unique
and right way?
>
Yes.
Quote:
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);
}
--
Ian Collins.
vippstar@gmail.com
Guest
 
Posts: n/a
#5: Sep 3 '08

re: Pointer assigned by a function problem...


On Sep 3, 4:02 am, jamaj <jama...@gmail.comwrote:
Quote:
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);
Quote:
*ptr = malloc(sizeof(**ptr));
Don't forget that it's possible for malloc to return NULL.
Quote:
printf("foo ptr=%p\n",*ptr);
Same here; *ptr is (double *), `p' expects (void *).
Quote:
*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;
Quote:
printf("foo value=%f\n",**ptr);}
>
int main(int argc, char **argv)
{
double *ptr=NULL;
printf("main ptr=%p\n",ptr);
And here
Quote:
foo(&ptr);
printf("main ptr=%p\n",ptr);
and here.
Quote:
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.

vippstar@gmail.com
Guest
 
Posts: n/a
#6: Sep 3 '08

re: Pointer assigned by a function problem...


On Sep 3, 4:11 am, vipps...@gmail.com wrote:
Quote:
On Sep 3, 4:02 am, jamaj <jama...@gmail.comwrote:
<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;
jamaj
Guest
 
Posts: n/a
#7: Sep 3 '08

re: Pointer assigned by a function problem...


Quote:
Please don't top post.http://www.catb.org/jargon/html/T/to...uette/?toppost
etc.

Ok. I won't do it anymore.

But the problem was that showed by Ian Collins. Many thanks.

Regards,

jamaj
jamaj
Guest
 
Posts: n/a
#8: Sep 3 '08

re: Pointer assigned by a function problem...


On 2 set, 22:19, vipps...@gmail.com wrote:
Quote:
On Sep 3, 4:11 am, vipps...@gmail.com wrote:
>
Quote:
On Sep 3, 4:02 am, jamaj <jama...@gmail.comwrote:
>
<snip>
Quote:
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



vippstar@gmail.com
Guest
 
Posts: n/a
#9: Sep 3 '08

re: Pointer assigned by a function problem...


On Sep 3, 4:26 am, jamaj <jama...@gmail.comwrote:
Quote:
On 2 set, 22:19, vipps...@gmail.com wrote:
>
Quote:
On Sep 3, 4:11 am, vipps...@gmail.com wrote:
>
Quote:
Quote:
On Sep 3, 4:02 am, jamaj <jama...@gmail.comwrote:
>
Quote:
<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;
>
I think that you already know that these little snippets of code have
only an illustrative purpose, and they did the work
No they did not.
Quote:
- with or without
the memory leaks that you correctly showed. So you can rest, cause my
doubt was kindly removed by Ian Collins.
vippstar@gmail.com
Guest
 
Posts: n/a
#10: Sep 3 '08

re: Pointer assigned by a function problem...


On Sep 3, 4:20 am, jamaj <jama...@gmail.comwrote:
Quote:
Quote:
Please don't top post.
>
Ok. I won't do it anymore.
Thanks, now please leave the attributes untouched.

(original message was by me)
jamaj
Guest
 
Posts: n/a
#11: Sep 3 '08

re: Pointer assigned by a function problem...


On 2 set, 22:31, vipps...@gmail.com wrote:
Quote:
On Sep 3, 4:26 am, jamaj <jama...@gmail.comwrote:
>
Quote:
On 2 set, 22:19, vipps...@gmail.com wrote:
>
Quote:
Quote:
On Sep 3, 4:11 am, vipps...@gmail.com wrote:
>
Quote:
Quote:
On Sep 3, 4:02 am, jamaj <jama...@gmail.comwrote:
>
>
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
Barry Schwarz
Guest
 
Posts: n/a
#12: Sep 3 '08

re: Pointer assigned by a function problem...


On Tue, 2 Sep 2008 18:19:06 -0700 (PDT), vippstar@gmail.com wrote:
Quote:
>On Sep 3, 4:11 am, vipps...@gmail.com wrote:
Quote:
>On Sep 3, 4:02 am, jamaj <jama...@gmail.comwrote:
>
><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);

--
Remove del for email
Barry Schwarz
Guest
 
Posts: n/a
#13: Sep 3 '08

re: Pointer assigned by a function problem...


On Tue, 2 Sep 2008 18:49:09 -0700 (PDT), jamaj <jamajbr@gmail.com>
wrote:
Quote:
>On 2 set, 22:31, vipps...@gmail.com wrote:
Quote:
>On Sep 3, 4:26 am, jamaj <jama...@gmail.comwrote:
>>
Quote:
On 2 set, 22:19, vipps...@gmail.com wrote:
>>
Quote:
On Sep 3, 4:11 am, vipps...@gmail.com wrote:
>>
Quote:
On Sep 3, 4:02 am, jamaj <jama...@gmail.comwrote:
>>
>>
>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...
>
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.
Quote:
>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.

--
Remove del for email
vippstar@gmail.com
Guest
 
Posts: n/a
#14: Sep 3 '08

re: Pointer assigned by a function problem...


On Sep 3, 6:35 am, Barry Schwarz <schwa...@dqel.comwrote:
Quote:
On Tue, 2 Sep 2008 18:19:06 -0700 (PDT), vipps...@gmail.com wrote:
Quote:
On Sep 3, 4:11 am, vipps...@gmail.com wrote:
Quote:
On Sep 3, 4:02 am, jamaj <jama...@gmail.comwrote:
>
Quote:
<snip>
>
Quote:
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);
Right, thanks for that correction.
Closed Thread