# vector help

This topic is 4566 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

why is the whole vector being the last thing buffer was when i am pushing back something? push_back should add not same the same.
//example

vector<int> b;

b.push_back(0);
b.push_back(1);

for(int i = 0; i < b.size(); i++)
cout <<b<<endl;

//you should see
//0
//1


but when i do this,
ZeroMemory(&buffer,sizeof(buffer));
fscanf(file,"%s",buffer);
var.push_back(buffer);
ZeroMemory(&buffer,sizeof(buffer));

the last thing buffer was is "[END]"

so here is what var looks like

var[0] = "[END]"
var[1] = "[END]"
var[2] = "[END]"
var[3] = "[END]"

why?

it is supposed to be

var[0] = "height"
var[1] = "width"
var[2] = "refreshrate"
var[3] = "fullscreen"

why is it doing that?



##### Share on other sites
First off, your fscanf is not in a loop.
SO you are only reading in one thing, and pushing one thing into the vector.

That said, var[3] should be giving you an error.
But it isn't so it appears to be just returning the only element that is
in the vector.

how are you defining your vector in the second case?
vector<char *> or vector<string>?
The first one may cause a problem, since you are storing pointers to buffer, not the contence of buffer. But the second one shouldn't cause problems.
(though you do zero buffer.... so not sure if that is your problem)

Though i should also note, ZeroMemory takes a pointer, if buffer is defined as
char *buffer or as char buffer[256] , then you should be calling
ZeroMemory ( buffer, 256 );
^Notice the lack of & ? <---- almost sure this is correct, may be wrong though, i never used the function before. You should check that it acutally zeros out the buffer like you think.
^And notice the lack of "sizeof"
since sizeof() does NOT return the length of the buffer if buffer is on the heap. (ie, char *buffer = new char[256];)

##### Share on other sites
Quote:
 Original post by KulSeranFirst off, your fscanf is not in a loop.SO you are only reading in one thing, and pushing one thing into the vector.That said, var[3] should be giving you an error.But it isn't so it appears to be just returning the only element that isin the vector.

yes it is, it is in a loop. in fact it is in a while(!feof(file))
i didnt send the whole code

vector<char*>

oh, btw buffer is a char buffer[MAX_PATH] = {0}; above the while loop

##### Share on other sites
vector<char *>

you are storing a pointer to buffer.
so ever element has a pointer to buffer.
NOT the contense of buffer.

If you use a
vector<string>
that should remove the problem.

^also, note above, my rant about using sizeof(), it isn't safe to assume it gives you the length of an array.

##### Share on other sites
so use ZeroMemory(&buffer,MAX_PATH) ?

##### Share on other sites
Yes, that is the safer route.

##### Share on other sites
As KulSeran said, you should probably use a vector of strings. If you wanted to use a vector of char*'s, you would have to allocate a new buffer for each push_back, then remember to free them.

//read from file...//allocate new buffer of size read inchar* buf = new char[size];//copy to buffer...//add buffer to vectora.push_back(buf);

Then use it.. When you're done you'll have to free each element of a. I'd recommend just using the strings..

##### Share on other sites
thanks for your help. it works now.

• 12
• 40
• 15
• 10
• 23