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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

now i want to create a function, th 5

Status
Not open for further replies.

hughLg

Programmer
Feb 18, 2002
136
MY
now i want to create a function, the protptype of this function:

char *sFunc()
{
return "hi world!";
}

any problem with this function? because i'm returning a address. and this address..i don't know how to classify it. it's allocated address or whatever?

or even:

char *sFunc()
{
char *s = (char *) malloc(sizeof(char) * 30);
s = "hi world!";
return s;
}

if i allocate like this, am i need to free it afterward (in order to recover the memory...) using free()?

and, why we need no allocate new memory to a char pointer before do this?

char *s = "hello!";
 
Well, there are a few things wrong with it. You are returning a constant string in the function that returns a char*. Also, you dont know the lifetime of the constant string. In theory it should stick around for a while but you never know. Its actual scope is the function. However, I have seen a function as follows

char foo(int idx)
{
return "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[idx];
}


This is a perfectly legal function!!! What I would suggest for the above function however would be

char* sFunc(char& s)
{
return strcpy(s,"hi world");
}

in the main, you allocate the space but to the pointer you assign it to a constant string. You lose the actaul value of the pointer and free should fail. If you allocate space, then you need to fill the space. You cant adjust the pointer or you will have memory leaks. Also, some compilers may have a problem with this but I assume since your posting this yours does not. You SHOULD see some error stating that you are assinging a const char* to a char* when you compile. If you want a constant string, doing

const char* str = "Hello World";


Matt


 
Exactly Zyrenthian.

btw, the
char foo(int i) {
return "ABCD";
}
is perfectly legal and even very safe, since the character that you get from indexing the string is copied, so it is not a character from the string that you return, but a copy of the character in the string.

It works like this:
"hello" is replaced with the address to a statically allocated string in your program, this string's memory is allocated and released by your compiler. Thus you don't have to allocate or deallocate it, actually, running free() on such a memory address will probably end in a crash.

This means that the following code:
char *foo = (char*)malloc(30);
foo = "hello world";

first allocates 30 bytes and assigns the start address to foo, then you overwrite this address with the address to the statically allocated "hello world"-string, effectively throwing away the other address, thus creating a memory leak.

If you make a function like this:
char *foo() {
char *ret = (char*)malloc(30);
strcpy(ret, "hello world");
return ret;
}
then yes, you do have to use free() later in the program that recieves the result of this function in order to correctly free the memory. I would, however recommend going on Zyrenthian's line here, passing in an already allocated string for your function to will with meaningful characters...however, be careful so that you use strcpy or indexing into that string, since doing something like

void foo(char *str) {
str = "hello world";
}
will have absolutely no effect since you only get a copy of the address, overwriting the local copy with another address does not affect the string what was sent to the function, and even if it would you would be throwing away already allocated memory, which would be even worse.

Sorry if I got into a rant there...I tend to get carried away trying to explain things ;)

Hope it's of some assistance.
 
The lifetime of a constant string will always be the lifetime of the process. The reasoning being that the constant strings are placed as seprate entries in the symbol table. You just need to change the code to

const char *sFunc()
{
return "hi world!";
}

Abbeman's malloc impmentation seems to be the best one,though it could be rewritten as

char *foo() {
char *ret = (char*)malloc(strlen("hello world") + 1);
strcpy(ret, "hello world");
return ret;
}




amit
crazy_indian@mailcity.com

to bug is human to debug devine
 
Or even:
Code:
char *foo() {
    const char *s = "hello world";
    char *ret = (char*)malloc(strlen(s) + 1);
    strcpy(ret, s);
    return ret;
}
This way the constant string appears only once: some compiler create duplicate copies of each litteral strings even if the string contents are identical. Furthemore, it is easier to maintain.

I agree with ami123: the litteral strings are stored in a specific 'segment' of the process memory and could be seen as global
Code:
const char * const
, i.e. constant pointers to constant chars, their lifetime being the one of the process.

Note: This memory segment is sometime set as read-only memory by the MMU, at least under Unix, resulting in a Segmentation Fault at any attempt to modify them like this:
Code:
char * s = "a litteral string";
strcpy(s, "trying to modify it");
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top