Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 9th, 2007, 11:57 AM   #1
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
Function returning double pointer ?

c Syntax (Toggle Plain Text)
  1. int * add(int x,int y)
  2. {
  3. int z;
  4. z = x+y;
  5. return(&z);
  6. }

Ok, that works. How about when i have something like :

char ** thing(){
return (???);
}

and : char *(??) = thing().

How exactly should it look ?

I'm quite sure this is a no brainer .. but ..
Help.
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Dec 9th, 2007, 2:05 PM   #2
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
Re: Function returning double pointer ?

In your first snippet, z is local to the function add(). What your doing is returning the address of a variable that doesn't exist outside the function -- it's an error.

In your second example, If thing() returned a pointer to memory on the heap (allocated by malloc()), then to return it, you'd just return the pointer from the function -- there's no need to return a pointer to a pointer to a char (char **).

If I've made any errors, any of the numerous C-gods present will hopefully correct them.

EDIT: I ran his code involving add() and although valgrind reports an error, the correct answer is computed. I'm 99.99% sure that it is wrong. Why is 5 being computed at all?

c Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2.  
  3. int *add( int x, int y ) {
  4. int z = x + y;
  5.  
  6. return &z;
  7. }
  8.  
  9. int main() {
  10. int *ans = add( 2, 3 );
  11. printf( "%d\n", *ans );
  12.  
  13. return 0;
  14. }

==7676== Memcheck, a memory error detector.
==7676== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==7676== Using LibVEX rev 1732, a library for dynamic binary translation.
==7676== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==7676== Using valgrind-3.2.3-Debian, a dynamic binary instrumentation framework.
==7676== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==7676== For more details, rerun with: -v
==7676== 
==7676== Invalid read of size 4
==7676==    at 0x80483B3: main (in /home/jesse/example)
==7676==  Address 0xBEB40904 is just below the stack ptr.  To suppress, use: --workaround-gcc296-bugs=yes
5
==7676== 
==7676== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 11 from 1)
==7676== malloc/free: in use at exit: 0 bytes in 0 blocks.
==7676== malloc/free: 0 allocs, 0 frees, 0 bytes allocated.
==7676== For counts of detected errors, rerun with: -v
==7676== All heap blocks were freed -- no leaks are possible.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Dec 9th, 2007, 2:34 PM   #3
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
Re: Function returning double pointer ?

Yes, the code is wrong, valgrind is right. As usual

When you make a function call, the stack pointer will be adjusted by some number of bytes.
This effectively reserves space on the stack for local variables, the return address, parameters, etc.

The integer z is a local variable, so it's located on the stack. Probably somewhere around the address to return to, etc. &z gives the address of z as you expect.

When add() returns, the stack pointer is changed back to where it was before add() was called. The local variables, etc are still there. You can still access that address and get the correct answer. However, the next time you call a function, z will get overwritten by something else, because its parameters and local variables may occupy the same space. It works right now, but will surely break later.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Dec 9th, 2007, 3:03 PM   #4
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
Re: Function returning double pointer ?

Thanks for the explanation Dameon.

I realized that I didn't write a correct definition for int main( void ). I will make a sacrificial offering to the C gods when time permits.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!

Last edited by Jessehk; Dec 9th, 2007 at 3:15 PM.
Jessehk is offline   Reply With Quote
Old Dec 9th, 2007, 5:58 PM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Re: Function returning double pointer ?

Quote:
Originally Posted by Jessehk View Post
Thanks for the explanation Dameon.
Dameon told some minor fibs. His explanation is just one of many possibilities for what happens. The formal result of the code is undefined: any set of observable or unobservable effects are allowed to happen.
Quote:
Originally Posted by Jessehk View Post
I realized that I didn't write a correct definition for int main( void ). I will make a sacrificial offering to the C gods when time permits.
Definitions of main() as "int main()" and "int main(void)" are equivalent. Even the latest C standard uses both forms on occasion.

Interestingly enough declarations (eg function prototypes) of such functions are different: the first form declares a function that takes an unspecified set of arguments, and the second declares a function that takes no arguments.

This is one of the minor anomalies introduced into the latest C standard: and some of the reasons are, I suspect, non-technical. The "gods of C" have not earned any sacrificial offerings with this one.
grumpy is offline   Reply With Quote
Old Dec 9th, 2007, 10:12 PM   #6
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
Re: Function returning double pointer ?

ok, thanks for the replies .
I didn't really check that code, int * .. I was using it as an example for a function returning a pointer.

the

char ** thing(){
return (???);
}

and : char *(??) = thing().

is actually a function generated by rpc. So I'm stuck with it. The problem is that i don't know how to return the value, and how to use the function in another part of the program.
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Dec 10th, 2007, 3:42 AM   #7
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Re: Function returning double pointer ?

You'll have to be a bit more precise. What is the caller expecting to receive when thing() returns?

And don't tell me a char **. I mean what operations does it expect to do with that pointer, and what does it expect the pointer to point at?
grumpy is offline   Reply With Quote
Old Dec 10th, 2007, 3:55 AM   #8
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
Re: Function returning double pointer ?

This is the original :
Server :
c Syntax (Toggle Plain Text)
  1. static double found;
  2. double * average_1(input_data *input,CLIENT *client)
  3. {
  4. found = 1;
  5. return (&found);
  6. }
  7.  
  8. double * average_1_svc(input_data *input,struct svc_req *svc)
  9. {
  10. CLIENT *client;
  11. return(average_1(input,client));
  12. }

average_1_svc will returne to the client;

Client:
c Syntax (Toggle Plain Text)
  1. double *result_1;
  2. result_1 = average_1(&average_1_arg.input_data, clnt);
  3. printf("%e\n",*result_1);


Now I modified the rpc .x file to return a string. In turn it generated functions like:
c Syntax (Toggle Plain Text)
  1. char ** average_1(input_data *input,CLIENT *client) {
  2. }

The problem is that in the function i don't know how to return a string
like :

char * string = malloc(sizeof(char)*100);
return string;

and if i return it ... how do I use the function on the client side?
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Dec 10th, 2007, 5:51 AM   #9
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Re: Function returning double pointer ?

So much for a question being asked in a clear form.

Quote:
Originally Posted by xavier View Post
This is the original :
Server :
c Syntax (Toggle Plain Text)
  1. static double found;
  2. double * average_1(input_data *input,CLIENT *client)
  3. {
  4. found = 1;
  5. return (&found);
  6. }
  7. double * average_1_svc(input_data *input,struct svc_req *svc)
  8. {
  9. CLIENT *client;
  10. return(average_1(input,client));
  11. }
static double found; double * average_1(input_data *input,CLIENT *client) { found = 1; return (&found); } double * average_1_svc(input_data *input,struct svc_req *svc) { CLIENT *client; return(average_1(input,client)); }
Well, as described previously, anything the caller does with these functions will yield undefined behaviour. A local variable is not guaranteed to exist when a function returns, so returning it's address gives a pointer to something (potentially) non-existent. If the caller dereferences (eg writes to or reads from) that address, the net result is undefined behaviour.

Quote:
Originally Posted by xavier View Post
c Syntax (Toggle Plain Text)
  1. char ** average_1(input_data *input,CLIENT *client) {
  2. }
char ** average_1(input_data *input,CLIENT *client) { }

The problem is that in the function i don't know how to return a string
like :

char * string = malloc(sizeof(char)*100);
return string;

and if i return it ... how do I use the function on the client side?
You don't need a function to return a pointer to a pointer if you simply want to give the caller a string.

For example;
#include <stdio.h>
#include <stdlib.h>

char *function()
{
     char *x = malloc(100);
     strcpy(x, "Hello");
     return x;
}

int main()
{
     char *str = function();
     fprintf(stdout, "%s\n", str);
     free(str);    /* necessary to release the data malloc()'ed by function */
}
The reason this works is that function() returns the value of x, not it's address. That value is the address of memory that has been obtained using malloc(), which is guaranteed to exist until the corresponding call of free().

However, if you change function to;
char *function()   /*  This function makes the caller exhibit undefined behaviour */
{
     char a[100];
     strcpy(a, "Hello");
     return a;
}
the caller will exhibit undefined behaviour because the array a does not exist when the function returns. And the return statement returns the address of the first element in a. But a will no longer exist when the function returns.....
grumpy is offline   Reply With Quote
Old Dec 10th, 2007, 8:32 AM   #10
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
Re: Function returning double pointer ?

Ok, the method signatures are generated with rpcgen. So i'm not modifying them.
Now , it's all interesting and stuff , and I do appreciate you taking the time to answer Grumpy.

But the question is quite simple . How can I work with " char ** " method signatures ?
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Incompatible pointer type (double pointer in a function) Gabriel Margarido C 16 Nov 23rd, 2007 3:04 AM
Function pointer to a member function Jimbo C++ 3 May 12th, 2006 4:53 PM
Recommended Practice for returning data from function Arla C# 1 Aug 16th, 2005 12:21 PM
Returning a value from a variable to the main function colt C 3 Apr 28th, 2005 7:56 AM
Returning An Array From a Function ViZioN C++ 5 Feb 21st, 2005 6:45 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:57 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC