![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
|
Function returning double pointer ?
c Syntax (Toggle Plain Text)
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 ! |
|
|
|
|
|
#2 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
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)
==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! |
|
|
|
|
|
#3 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#4 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#5 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Re: Function returning double pointer ?
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:
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. |
|
|
|
|
|
|
#6 |
|
Professional Programmer
|
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 ! |
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
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? |
|
|
|
|
|
#8 |
|
Professional Programmer
|
Re: Function returning double pointer ?
This is the original :
Server : c Syntax (Toggle Plain Text)
average_1_svc will returne to the client; Client: c Syntax (Toggle Plain Text)
Now I modified the rpc .x file to return a string. In turn it generated functions like: c Syntax (Toggle Plain Text)
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 ! |
|
|
|
|
|
#9 | ||
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Re: Function returning double pointer ?
So much for a question being asked in a clear form.
Quote:
Quote:
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 */
}However, if you change function to; char *function() /* This function makes the caller exhibit undefined behaviour */
{
char a[100];
strcpy(a, "Hello");
return a;
} |
||
|
|
|
|
|
#10 |
|
Professional Programmer
|
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 ! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |