I am trying to pass some url information into tokenize_urls from process_and_display_information. To get things up and running, I am simply trying to return and retain some array data from tokenize_urls into process_and_display information. However, I am running into an issue where the variables are created and sent back from tokenize_urls but they're only pointers, and so once it gets back to process_and_display_information, the data is gone. How do I actually capture new data that is created by tokenize_urls? Surely not with a global variable!

char * tokenize_urls()
{
	char * my_strings[3] = {"first","second","third"};

	return my_strings;
}


void process_and_display_information()
{
	char * my_new_char = tokenize_urls();

	cout << my_new_char << endl;
}

Recommended Answers

All 3 Replies

once it gets back to process_and_display_information, the data is gone.

Yes, it will be gone, because the scope of myStrings is only within tokenize_urls(). myStrings is created in the stack and once the control goes out of tokenize_urls(), the memory will be freed. That is why you lose your data at process_and_display_information().

To solve this, you have to allocate memory for myStrings using new. By this way, the data of myStrings will be stored in heap. Even if tokenize_urls() finishes it's job, myStrings will still be there in heap and can be accessed by process_and_display_information(). Remember to delete the memory in process_and_display_information() once you are done using myStrings.

Another way to solve this is to declare myStrings in process_and_display_information() and pass it by reference to tokenize_urls(). Let tokenize_urls() be a void function and let it modify myStrings. So when you get back to process_and_display_information() you will have the modified myStrings with you.

I prefer the second method because it doesn't involve any memory allocation and delallocation to be taken care by the programmer.

commented: Very, very clear and well thought out explanation. Even bolded terms and such to make it more clear. +0

simple
your problem is that you sent the pointer address of your variable but when the function ended the memory was freed and when the pointer refered to that memory location it was empty
you do this and you wont have to declare the variable global

char * tokenize_urls()
{
	char * my_strings[3] = {"first","second","third"};
 
	return my_strings;
}
 
 
void process_and_display_information(char x=&my_strings)
{
	char  my_new_char = x;
 
	cout << my_new_char << endl;
}

rohan: you have the right idea, but the code is incorrect. You have to allocate memory for my_strings

#include <iostream>
#include <cstring>

char ** tokenize_urls()
{
	char **my_strings;
	char *temp[3] = {"first","second","third"};

	my_strings = new char*[3]; // allocate memory for the array
	for(int i = 0; i < 3; i++)
	{
		my_strings[i] = new char[20]; // allocate memory for each string
		strcpy_s(my_strings[i],20, temp[i]); // duplicate the string
	}
 
	return my_strings;
}
 
 
void process_and_display_information()
{
	char  **my_new_char = tokenize_urls();
	for(int i = 0; i < 3; i++)
        {
		std::cout << my_new_char[i] << std::endl;
                delete[] my_new_char[i]; // delete the memory this string
        }
        delete[] my_new_char; // delete the array
}

int main()
{
	process_and_display_information();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.