Mayank
Mayank

Reputation: 2210

Vector not displaying right elements

I have the below code where the elements which I have pushed into the vector are not same when I am printing.

#include<stdio.h>
#include<iostream>
#include<vector>

using namespace std;
int size = 0;
int main()
{
    int i,num1,num2,num;
    vector<char *>vec;
    for(i=0;i<7;i++)
    {
        char buffer[30];
        if(i%2==0)
        {
            strcpy(buffer,"hello");
        }
        else
        {
            strcpy(buffer,"bye");
        }
        printf("buffer has %s\n",buffer);
        vec.push_back(buffer);
    }

    for(i=0;i<vec.size();i++)
    {
        cout<<"CHECK vec["<<i<<"] has "<<vec[i]<<endl;

    }
    return 0;
}

When I executed I got the following result:

buffer has hello
buffer has bye
buffer has hello
buffer has bye
buffer has hello
buffer has bye
buffer has hello
CHECK vec[0] has hello
CHECK vec[1] has hello
CHECK vec[2] has hello
CHECK vec[3] has hello
CHECK vec[4] has hello
CHECK vec[5] has hello
CHECK vec[6] has hello

I could see the following via gdb:

(gdb) p vec
$5 = std::vector of length 1, capacity 1 = {**0x7fffffffe790** "bye"}
(gdb) n
11      for(i=0;i<7;i++)
(gdb) p vec
$6 = std::vector of length 2, capacity 2 = {**0x7fffffffe790** "bye", **0x7fffffffe790** "bye"}

The address of vector elements are not different for 1st and 2nd element and so on for other elements also. can anyone please explain how to get right elements in the vector and why this happened.

Upvotes: 0

Views: 92

Answers (3)

Inquisitive
Inquisitive

Reputation: 485

You are just storing the address of the buffer variable which always has the same memory location. Since i=6 is the last loop , buffer has the value "hello" and it will print that value when you try to print the vector.

Edit: While you print the vector, the buffer has gone out of scope and it may print anything, so undefined behavior.

Upvotes: 0

toutnom
toutnom

Reputation: 244

Use vector of strings. But if you really want to use vector of char*, this would do:

#include <stdio.h>
#include <iostream>
#include <vector>
#include <cstring>

using namespace std;

int size = 0;
int main()
{
  int i,num1,num2,num;
  vector<char *>vec;

  for(i=0;i<7;i++)
    {
      char *buffer = new char[30];
      if(i%2==0)
      {
          strcpy(buffer,"hello");
      }
      else
      {
          strcpy(buffer,"bye");
      }

      printf("buffer has %s\n",buffer);
      vec.push_back(buffer);
    }

  for(i=0;i<vec.size();i++)
    {
        char *buffer = vec[i];
        cout<<"CHECK vec["<<i<<"] has "<<buffer<<endl;
        delete [] buffer;
    }

    vec.clear();

  return 0;
}

Upvotes: 1

Alex
Alex

Reputation: 467

The problem is that you aren't pushing in the vector a string, but a pointer. A pointer hold an address to a portion of memory, in your case the buffer and as you saw when debugging, that pointer has the same value. So actually you are printing buffer and not a separate string from your vector.

My recommendation is to use the full power of the STL and use the provided string which will work as you expect.

Also note that the buffer array/string is deallocated at the end of each iteration of the for loop and so you enter the undefined behavior land.

Upvotes: 1

Related Questions