return char* from func in c?
Hi,
I wrote a function that needs to return a string created in the function in c. Is it the best way in c to have a return pointer as an argument? Using malloc makes it hard to know when to free etc and using an internal static memory allocation will get erased leaving the function i suppose. Having global pointers just suck. I cant think about other options. like this char * my_func(char* ret) { strcpy(ret, some_get_a_string_func()); /* assigne the sting pointer */ return ret; } a lot of other languages do not have these problems. So im not so used to it. Thanks |
Quote:
Quote:
Quote:
|
Thanks.
Quote:
I meen with static as not using malloc like this. char *myfunc() { char my_buff[512]; ....fill the buff return buff; } Did you understand it like that johnsfine? Your explanation makes sence to me but I want to be shore that we talk about the same static. |
The declatation:
Code:
void function(void) The following will declare buff as static, and reserve memory for this function to use for buff: Code:
void function(void) 1) Please be adviced as to check documentation for the compiler/linker you use, since different compilers/linkers have different allocation strategy. There exists compilers that treat function internal static variables like if they where auto/register without even giving out a warning. (This is mostly a problem with embedded systems programming, and not that often a problem in a desktop environment). 2) If the program is multithreaded, or the function may get called recursively, the static declared storage will be overwritten by subsequent calls to the function. Therefore it is deprecated to declare function internal static variables and returning pointer to its contents on return. |
Quote:
|
Quote:
Quote:
Maybe your point is that you might reasonably be able to design to finish using the data before it could be overwritten in a non recursive function in a single threaded program, but making sure the data isn't overwritten before you're done with it is a much more difficult constraint if recursive or multithreaded. Quote:
|
Quote:
|
Quote:
Added: Of course one can use local static variables. In some cases one may not be allowed to use auto variables that allocate on stack. But my point is that the caller should allocate the space for the function to return data in, and not just trusting the allocation done in that function. The standard libraries are full of deprecated methods because they return pointers to local static data. Simply because they return pointers to local static data, and thus are not reentrant. One example of that is getdate() . |
Quote:
But to make it worse when programming PIC Micros, one has to differ between pointers to const global and global data too. ;) Quote:
|
And maybe it is good to add that a global static variable, (that is declared outside functions), is always initialized, to zero, or anything else if the linker is instructed to use a custom initializer. But a local static variable, (that is declared inside a function), need not be initialized when program starts, unless explicitly stated so, by for example "char b[9] = "good;" and still in some design environments this may generate a compile error instead.
Some older compilers tend to dislike local initializers, and one should know that it is a big difference between: Code:
void function(void) Code:
void function(void) In desktop environments local static is often initialized to zero anyhow, even if the linked in initializer doesn't, because the memory allocation in the OS wipes the memory before mapping it to the new process. Still, it is possible to turn off page scrubbing. In VMS the system administrator have the ability to choose with a single command wether page blocks will be scrubbed or not on allocation and/or freeing, and if it should simply be wiped once with zeroes, or follow DoD 5220.22-M standard for multiple pattern wiping. So it is better to be safe than sorry. And writing safe, portable code can be very frustrating sometimes. |
Hi -
Code:
/* OK: caller allocates the memory */ Code:
/* BAD!!!! This local variable is NOT VALID when my_func() returns */ Code:
/* Legal ... but not re-entrant. Code:
/* Also OK: callee allocates memory; caller (or SOMEBODY!) needs to remember to free it. */ |
'malloc' call is also slow by definition because it's a system call.
|
thanks paulsm4 for the 4 ways of (not) doing it. Is clear as cristal now.
Sloppy of me using the word static and not fixed length or something. |
Hi, kalleanka -
Thanx for posting back. FYI, I had a couple of (unintentional!) bugs in my sample code - I fixed them. Also FYI, none of the examples (except for Example #2, the one commented "BAD!!!") is "wrong". All of them have different drawbacks and benefits, all of them are useful in different scenarios. Even the "static" example. I'm doing maintenance programming on a DOS/Watcom C++ system (no kidding!) where using stack variables can literally crash the system. So I often have to declare stupid temp, debug variables as "static" variables, just because of limitations of the compiler and the architecture. Please do mark the post as "Solved", and please feel free to "thank" anybody whose response helped. PS: And don't feel bashful about using "malloc()". It's almost never needed in C++ ... but it's perfectly appropriate and strongly encouraged in C. IMHO .. PSM |
well i have to thank all of you here on the forum. I sitt alone and program and since i moved from my home contry i do not know anyone who does programming at all. So I only have forums to ask. And I want to get a good understanding not a quick fix since this will make me a better programmer. Hoppfully these threads will help others too.
|
All times are GMT -5. The time now is 09:26 PM. |