problem returning pointer from a function

I have something like this...

1
2
3
4
5
6
7
8
9
10
11
12
13
int main(void)
{
   char* name = getName();
   cout << "Hello " <<  name;
   return 0;
}
char* getName(void)
{
   char name[25];
   cout << "What is your name?";
   cin >> name;
   return name;
}


However, name is being printed with strange characters. But if I put the lines within getName within the main function then it displays properly. What am I doing wrong?
Last edited on
You're returning a pointer to a buffer that no longer exists.

By doing return name;, you're giving main() a pointer to the 'name' buffer. The problem is, since name is local to the getName function, the name buffer is destroyed and goes away as soon as the getName function exists. Therefore the pointer main is getting instantly becomes a bad pointer (points to rubbish/bad memory).


The easiest way around this is to just not use pointers, and use strings instead of char arrays:

1
2
3
4
5
6
7
string getName()  // <- string, also you don't need the (void) crap, that's a C thing
{
   string name;  // <-  string
   cout << "What is your name?";
   cin >> name;
   return name;  // <- now this will work just fine
}
Thanks, I was really doing this in order to try to learn a few pointer concepts.

I did this, which now works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

char *getName(void);
int main(int argc, char* argv[])
{
	char *myName = getName();
	cout << "Hello " << myName << endl;	
	return 0;
}
char *getName(void)
{
	char *name = new char[25];
	cout << "What is your name?" << endl;
	cin >> name;
	
	return name;
}
That just introduces new problems. If you want to use C strings, let the function take a char* argument and read into it using cout.

If you don't absolutely have to use char*, then for goodness sake use a string.
What new problems am I introducing?
You need to delete that pointer (rather, what it points to) now somewhere.
Last edited on
Right.

Your code works, but leaks memory. You could solve that by doing this:

1
2
3
4
	char *myName = getName();
	cout << "Hello " << myName << endl;	
	delete[] myName; // <- delete it
	return 0;


But that's not something you want to get in the practice of doing.

This is "passing ownership" and leads to very hard-to-find problems unless you are extremely careful. In general when you allocate something with new[], whoever allocated it should be responsible for cleaning it up / deleting it. In this case, getName allocated it, but is leaving it up to whatever function called it to clean it up. Hence, getName is "passing ownership" of the buffer to main().

(That's not to say that passing ownership is always bad. Sometimes it's the best way to go. Here, though, I don't recommend it. Especially since the problem is easily solved with strings)


That's besides the point that char arrays are bad for user input because they are easily overflowed. For example your code will explode if the user enters a name longer than 24 characters.

Strings are safer and easier. Use them.
Last edited on
Thanks for the advice.

Is there a difference between free() and delete[]?
Yes.

free works with malloc. delete[] works with new[].

new -> delete
new[] -> delete[]
malloc -> free

don't mix and match.

Furthermore, malloc/free don't call object constructor/destructors so they usually should be avoided in C++.
Topic archived. No new replies allowed.