Contrary to what the title implies, my real problem lie in returning a vector pointer, and then use it.

Here's what I tried:

#include <vector>
#include <string>
#include <cstring> //? Needed?
#define R_OK 0
#define R_ERROR 1

using namespace std;

vector<string>* tester(){
	vector<string> v;
	v.push_back("Lolzer");
	vector<string> *ptr = &v;
	return ptr;
}

int main(){
        vector<string>*ptr = tester();
        cout<<ptr->at(0);
	return R_OK;
}

It compiles, and runs, but as soon as I try to access the vector, the compiler says it's out of bounds (probably because the vector it is pointing to is empty)

Feel free to answer any of these questions :D

  1. Anyone know why this doesn't work?
  2. Anyone have a link explaining how to point to vectors correctly?
    'cause I've seen something like vector<string*> *ptr as well, and it looks confusing.
  3. Advice about vectors?
  4. Suggestions on how to further improve the code, giving the same result, but in a clear, clean and understandable (and perhaps, fast) way?

Recommended Answers

All 10 Replies

>>Anyone know why this doesn't work?
Because the vector is destroyed when function testor() returns to its caller. A better way is to pass the vector by reference to testor()

void testor(vector<string>& v)
{
	v.push_back("Lolzer");
}

int main()
{
    vector<string> v;
    testor(v);
}

Ah, but the way I introduce the function makes it an inefficient way. I'm training on making my own "handles".
I really want the function to be able to return a vector (pointer).
Using this function works, but, it's not as efficient:

vector<string> CreateHandle(int length, int height, char hframe, char vframe){
	vector<string> NewHandle;
	string topbot, middle;
        topbot.append(length,hframe);
	middle.append(length, ' ');
	middle.replace(0,1,1,vframe);
	middle.replace(length-1,1,1,vframe);
	NewHandle.push_back(topbot);
	for(int i=0; i<height-2;i++){
		NewHandle.push_back(middle);	
	}
	NewHandle.push_back(topbot);
	return NewHandle; //Here I just return the vector, and create a pointer to it after
}
int main(){
	vector<string> menu = CreateHandle(20,20, '=', '|');
	vector<string> *pMenu = &menu;
        cout<<pMenu->at(0);
        return R_OK;
}

If you want to brush up this piece of code, please do, and explain why your way is cleaner, and more efficient. :)
Focus on the main question though...

Edit: is there a way to keep the vector from being destroyed after the function's end, maybe like making it global (static?) or something? :S
or is there another approach to this, apart from sending it as a reference?

The code you posted is slightly different than the code in the origina post in that this new version returns a complete vector, not just a pointer. When CreateHandle() returns the compiler will duplicate the entire vector before leaving the function, then duplicate it all over again on line 16 for vector menu. For large vectors that can be very very time consuming which would not be acceptable in any professional program.

There are two alternatives:
1) make the vector static so that its contents are never ever destroyed until the program itself exists, then return a pointer to it.

2) pass it by reference as I previously mentioned.

Could you write me an example of 1. ?
I'm only used to using static variables inside structs, I'm unsure of how they work. If you'd rather link me to a site explaining static variables, please do so. Either way, thanks for your help. :)

By the by,
the whole idea of this was to make a function that returns a structure (handle), making it easy declaring new structures.
The structures should then be editable, and in those edit functions I pass it by reference.

just like your original post, but make the vector static static vector<string> v;

Ah, hahaha, just adding the 'static' made everything work.
So, a static variable is like declaring it in the global scope?
or what did you mean by, until the program exists?

(Maybe you meant, until the program exits... :P)

Ah, hahaha, just adding the 'static' made everything work.
So, a static variable is like declaring it in the global scope?

Yes, but when declared inside a function it only has scope within that function.

Ah, that explains alot! How about making a global variable static?... will that make the global only known within the... eh.. file?

Also, how do you destroy a static vector? or a vector in general for that matter?
(Thinking of adding a DestroyHandle() function)

Ah, that explains alot! How about making a global variable static?... will that make the global only known within the... eh.. file?

That's correct. The static keyword works a little differently when used within a c++ class. Here are some google links for more information.

Also, how do you destroy a static vector? or a vector in general for that matter?
(Thinking of adding a DestroyHandle() function)

Since main() has a pointer to it then just use the vector's erase() method to destroy it. menu->erase(); Do not attempt to delete that pointer!

Ok, I'll remember that.
Thanks for the "links", I saw MSDN was on top, they have the best documentation! :D

<marked as solved>

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.