If you try to return a pointer to the vector's data instead of returning the vector itself, you will have the same problem as before. The vector will be destroyed and the pointer will be invalid.
I think you're going to have to use new[]/delete[].
Printable View
If you try to return a pointer to the vector's data instead of returning the vector itself, you will have the same problem as before. The vector will be destroyed and the pointer will be invalid.
I think you're going to have to use new[]/delete[].
> I think you're going to have to use new[]/delete[].
That works, or if you don't want to mess with dynamic memory, I think using a static array might be a viable option here as well.
There are some complications with this method, though. Consider the following code:Code:URL_Keyword_Mapping* read(std::ifstream& ifile, int& length) {
//Add the static identifier
static URL_Keyword_Mapping UKM_ARRAY[50];
//...
return UKM_ARRAY;
}
After this, foo and bar will point to the same array (the array that was written the second time read was called). If you want the original value of foo to be conserved, you'd have to declare it as an array and use memcpy.Code:URL_Keyword_Mapping* foo, *bar;
foo = read(blah, blah);
bar = read(blah, blah);
The other alternative is to use a vector and to return the object itself, which, if efficiency isn't a concern, is probably the best option.