Dynamic mem alloc inside function

Hi all,

How would it be possible (if at all) to dynamically allocate memory from a function and then use the data after the function has closed. For i.e.:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void funct(string *sWord)
{
	int length = 0;
	int i;
	fstream fFile("text.txt");
	string buf;
	getline(fFile, buf);

	length = buf.size();

	sWord = new string [length / 10];

	//Write to sWord

	return ;
}


int main()
{
	string *sStr;
	sStr = new string;

	funct(sStr);

	//Play with the data from sStr which is actually from a file "text.txt"

	return 0;
}


So as you can see, all that I want to do is make a function that opens the file, manipulates it, and stores the data, and then passes it back so it can be worked on.
I did something similar to the above example, but what I get back in my string (in this case in sStr) is giberish, since (i think) after the function closes it lets go of all the memory it had, so therefor all the allocated memory, and hence the data it stored in the string *, is this the case?

Thanks for any help.
Last edited on
either void funct(string*&) or string* funct(string*/*though you probably don't need this*/) and return sWord; somewhere in funct().
Nice :)

This one void funct(string*&) works like a charm. Might I ask you for an explanation or for a reference to better understand, as this would seem to have some advanced pointers at work.

thank you
yeah I'm also curious about that first function definition, is it passing in a pointer to a reference to a string? I've never done encountered that before. And why not just pass in a pointer to a string instead?
It's a reference to a pointer. You'll change the pointer's value when you use new inside the function. Though you should note that the in original code you have a memory leak.
doh shouldve known it was a reference to a pointer, must remember to read right to left. You didn't answer my second question though why not just return a pointer instead?
what I get back in my string (in this case in sStr) is giberish

No, sStr will point to the empty string, just like before the call.

I did something similar to the above example, but what I get back in my string (in this case in sStr) is giberish, since (i think) after the function closes it lets go of all the memory it had, so therefor all the allocated memory, and hence the data it stored in the string *, is this the case?

Heavens, no. You need to explicitly delete the objects with delete[]. As it is now, you have a bad memory leak.

This doesn't work because you're assigning the return value of new[] to a local pointer, which will be destroyed when the function exits (just the pointer, not the memory it points to!) and the allocated memory will be unreachable.

If you pass by reference, sWord in funct will be an alias for the pointer in main.

Still, messing around with new[] and delete[] is a bad idea.
An acceptable way to do this is to pass a string vector that the function can write the contents to:

1
2
3
4
5
6
void readFile(vector<string>& lines)
{
	fstream file("text.txt");
	string line;
	while (getline(file, line))lines.push_back(line);
}


or alternatively (if you know your compiler can apply RVO here):
1
2
3
4
5
6
7
8
vector<string> readFile()
{
        vector<string> lines;
	fstream file("text.txt");
	string line;
	while (getline(file, line))lines.push_back(line);
	return lines;
}
Topic archived. No new replies allowed.