Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

returning char*

This topic is 6099 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

is there something that doesn''t work with returning char*? i have function: char *func(int a) { char *abc; strcpy(abc, "asdf"); cout << abc << endl; } and it cout''s asdf but in main, if i do: cout << func(0) << endl; it cout''s junk -> something like yyyyyyy0. i think that the function is returning null or the char* is being deallocated or something by the time it is being returned to main()

Share this post


Link to post
Share on other sites
Advertisement
There''s three things wrong with what you''ve given. First of all, in func() you''re writing to unallocated memory, which is bad bad bad!! Second, you don''t actually return anything from func() (you say "this function returns a char *", but then you don''t actually tell it what to return. Finally, unless you allocate the memory for abc on the heap (with operator new for example) you can''t return it anyway (since it is deconstruction when func() goes out of scope). Also, while not an error, you don''t need to have func() take any arguments, since it doesn''t use them. You want your code to look something like this:

  
char *func()
{
char *abc;

// allocate memory for abc

abc = new char[10];

strcpy( abc, "asdf" );
cout << abc << endl

return abc;
}

int main( int, char ** )
{
char *p = func();
cout << p << endl;

// delete memory allocated in func()

delete[] p;

return 0;
}


One thing I should point out about this code is that is (generally) bad practice to allocate memory in one function, and delete it in another. Each function should know exactly what resources it owns (or has allocated) and by allocating memory in one function and deleting it another is like transferring ownership of that memory; something that should be avoided.

It would be better to allocate the memory in main, pass it to func() which copies whatever it wants to it, and then delete it in main after it''s been used.


codeka.com - Just click it.

Share this post


Link to post
Share on other sites
Well, first off... you don''t have a return in the function. Second, I think the cout within a function called in a cout doesn''t work too well, but this is just speculation.

Share this post


Link to post
Share on other sites
Most any C/C++ guide will cover dynamic memory location. Character pointers are no different than any other pointer. Here''s an example:
  
// This wont work, since abc isn''t allocated

int *abc;
for(int a=0; a<100; a++)
abc[a] = 10;

/* This won''t work either, since def isn''t allocated. This is basically what you''re doing when you try to copy "asdf" into your "abc" string */

void strcpy(char *into, cosnt char *from) {
while(*from) {
*(into++) = *(from++);
}
}

char *xyz = "abcdefghijklm";
char *def; // def points somewhere random now
strcpy(def,xyz);

/* This would work, however, since def now is allocate to 100 characters until I call free */


def = (char *) malloc(sizeof(char) * 100);
strcpy(def,xyz);
free(def);

// Or this:

def = new char[100];
strcpy(def,xyz);
delete [] def;


[Resist Windows XP''s Invasive Production Activation Technology!]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!