returning char*

Started by
4 comments, last by MadProgrammer 22 years, 7 months ago
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()
Advertisement
You need to allocate memory to abc before returning it with new or malloc. Later you''ll need to release it with delete or free. I''m surprised that it isn''t crashing with code like that (copying a string into some random memory).

[Resist Windows XP''s Invasive Production Activation Technology!]
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.
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.

I know only that which I know, but I do not know what I know.
yeah, there was supposed to be a return statement, but i was writing the post at like 1am and not copy-pasting code. my bad

is there somewhere that talks about char*''s in depth?
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 allocatedint *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 nowstrcpy(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!]

This topic is closed to new replies.

Advertisement