Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Functions and return types? (char *)

Status
Not open for further replies.

JaybOt

Programmer
Apr 18, 2001
101
GB
Hi all,

can anyone tell me if there are any restrictions on the 'types' that can be returned from within a function? i.e. (int, float, vector, ect.)

Yes, i have already posted another thread on the same subject, but what i need to know is how i can return an array if characters (char *) from a function.

I have tried the prototype .. char* myFunc(void); .. to no avail. Please, please, please can someone point me in the right direction.

Thanks in advance,

regards,
J@yb0t[afro]

Appologies for reposting this tread - thought it might benifit from being re-worded.

"Always know what you say, but don't always say what you know!"
 
Returning numbers, pointers are ok. The only thing that can possible cause problems are strings and structures.
It works on many compilers, but there are some where it does not. I have seen compiler upgrades where it worked on one version, but did not on the next version.

char *myfunct(void)
{
char data[256];
return &data;
}

is the situation that can cause problems. data is a local variable, and when you return goes away. It all depends on the compiler handles the data allocation. Everything may be ok for a while, but later in the program that area may be destroyed.
The safer way is to return the actual pointer like this.

char *myfunct(void)
{
char *data;
data = (char *) calloc(1,256);
if (!data) return NULL;
return data;
}
 
Even returning a dynamically allocated string can be dangerous.

The caller of your function has to read your documentation to see if the returned string is dynamically allocated and needs to be freed, or if it's stored as a static variable. As a general rule, it's bad practice to allocate something, then make someone else deallocate it.


The safest way to write a function like this is to have the caller pass in a char*, then have your function fill it in (see C library functions like strcat). You can then return this char* if that makes the function easier to use.

This method turns responsibility of the string over to the caller. If he wants to pass in a statically allocated string, fine. If he wants to pass in a dynamically allocated one, also fine; he just has to remember to release the memory when he gets done with it.


Of course, the best solution is to not return a char* at all. Return an std::string or other string object. The same goes for any pointer: return an object that takes care of memory management for itself.
 
Thanks m8

"Always know what you say, but don't always say what you know!"
 
>return an object that takes care of memory management for itself.

just curious, how is the memory management done behind the scenes? For example Java has garbage collection, but is
it done with std::string ??
 
The object allocates memory for itself at various points, including construction, and being assigned a string value that won't fit in its currently allocated memory.

The destructor (which gets called whenever the object goes out of scope, or is deleted if dynamically allocated) frees that memory.

The allocation is usually done with C++ operator new, and the deallocation is done with C++ operator delete. However, STL containers let you provide a custom allocater.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top