Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 3rd, 2008, 01:55 AM
jamaj
Guest
 
Posts: n/a
Default Pointer assigned by a function problem...

#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
  #2  
Old September 3rd, 2008, 02:05 AM
Ian Collins
Guest
 
Posts: n/a
Default 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.
  #3  
Old September 3rd, 2008, 02:05 AM
jamaj
Guest
 
Posts: n/a
Default 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
  #4  
Old September 3rd, 2008, 02:15 AM
Ian Collins
Guest
 
Posts: n/a
Default 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.
  #5  
Old September 3rd, 2008, 02:15 AM
vippstar@gmail.com
Guest
 
Posts: n/a
Default 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.

  #6  
Old September 3rd, 2008, 02:25 AM
vippstar@gmail.com
Guest
 
Posts: n/a
Default 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;
  #7  
Old September 3rd, 2008, 02:25 AM
jamaj
Guest
 
Posts: n/a
Default 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
  #8  
Old September 3rd, 2008, 02:35 AM
jamaj
Guest
 
Posts: n/a
Default 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



  #9  
Old September 3rd, 2008, 02:35 AM
vippstar@gmail.com
Guest
 
Posts: n/a
Default 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.
  #10  
Old September 3rd, 2008, 02:35 AM
vippstar@gmail.com
Guest
 
Posts: n/a
Default 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)
  #11  
Old September 3rd, 2008, 02:55 AM
jamaj
Guest
 
Posts: n/a
Default 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
  #12  
Old September 3rd, 2008, 04:45 AM
Barry Schwarz
Guest
 
Posts: n/a
Default 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
  #13  
Old September 3rd, 2008, 04:45 AM
Barry Schwarz
Guest
 
Posts: n/a
Default 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
  #14  
Old September 3rd, 2008, 06:25 AM
vippstar@gmail.com
Guest
 
Posts: n/a
Default 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.
 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles