4

I have to insert elements into a pointer to a vector.I have written the following code but it is giving segmentation fault. Could someone please point out what are the errors in this code or how can we do this alternatively.

int main()
{
     vector<int> *te;
     te->push_back(10);
     cout<<te->size()<<endl;
     return 0;
 }
Mukesh
  • 117
  • 1
  • 1
  • 6
  • 6
    Why do you need a pointer to a vector? Perhaps we can help better if we know the motivation behind this. – Alok Save Apr 01 '12 at 09:51
  • Actually I have to implement a simple C++-template class for managing a smart pointer to a vector.Can you please suggest me how to do this? – Mukesh Apr 01 '12 at 09:58
  • 3
    Why don't you simply use a smart pointer to a vector then? I.e. `shared_ptr >`... – enobayram Apr 01 '12 at 10:07
  • Why don't you simply use the `shared_ptr` **as is** ? Your insistence on defining your own class leads me to infer that this is a homework question. – enobayram Apr 01 '12 at 10:21
  • @AlokSave we need a pointer to a vector because pointer is one of building blocks of C++. It should stand for its definition. If not please enlighten me! – Necktwi Nov 12 '13 at 15:27
  • @neckTwi: Just because a programming language offers a feature does not mean you must use it.You need to choose a feature that is suited to the job you want to get done.It is more of horses for courses really.Using a feature without apt reasoning is not acceptable.The snippet OP shows does not require the use of pointer to vector,a simple vector should suffice and hence my comment.As for, enlightening you,please free to do so yourself by picking up a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Also,if you need to ask this Q you **must** do so. – Alok Save Nov 12 '13 at 15:51
  • @AlokSave yeah I'm reading about `RAII` but its radical to have a reference point to build something :) stackoverflow points are worthy than facebook likes :) – Necktwi Nov 13 '13 at 04:41
  • @AlokSave can we create a heap object of a `vector` wrapper class? If we can't then any class that has a direct or indirect `vector` member can't be instantiated as a heap object? – Necktwi Nov 13 '13 at 06:46
  • @neckTwi: You can but it is not necessarily a good idea to do so. – Alok Save Nov 13 '13 at 15:10

5 Answers5

19

You never allocate the vector:

vector<int> *te  = new vector<int>;

Also, you don't need dynamic allocation. A cleaner way is with automatic storage:

int main()
{
     vector<int> te;
     te.push_back(10);
     cout<<te.size()<<endl;
     return 0;
}
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 1
    Thanks!! Actually I have to implement a simple C++-template class for managing a smart pointer to a vector.Can you please suggest me how to do this? – Mukesh Apr 01 '12 at 10:05
10
 vector<int> *te;
 te->push_back(10);

You have declared a pointer to a vector; you have not initialized it to point to a valid piece of memory yet. You need to construct a new vector using new.

vector<int> *te = new vector<int>();

You should however not do this. There are very few reasons to maintain a pointer to a vector, and you just made it completely ineffective at managing its own internal memory.

Read a little bit about RAII. This is a technique used to manage dynamically allocated memory via the lifetime of an object. The vector uses this method to clean up its internal, dynamically allocated storage when it goes out of scope.

Maintaining a pointer to the vector prevents it from working correctly, relying on you to call delete yourself, essentially nullifying one major benefit of using a vector over an array in the first place.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • @neckTwi: Like `unique_ptr` or `shared_ptr`? Still don't understand why you'd want a pointer to a vector when a vector is pretty small. – Ed S. Nov 13 '13 at 07:18
  • should we not create a `vector` heap object or should we not create a pointer to `vector`? Can I create a stack object and pass its pointer to other functions? I want to have a pointer to `vector` for the same reason why we want a pointer to any other type. Especially unions support pointers but not references. – Necktwi Nov 13 '13 at 10:29
  • @neckTwi: Pass a reference. Why pass a pointer when you don't need to? I'm not saying there is never ever a good reason to do so, but it's extremely rare. I write a lot of c++ code and I can't say I've ever taken a pointer to a vector or used a vector in a union. – Ed S. Nov 13 '13 at 19:53
1

you have to first allocate place for the pointer before starting to use it .

vector<int> *te = new vector<int>();

insert this line into your code just after the vector<int> *te;

Note that

If you were to trying to access already defined vector with an pointer you would not have to allocate place for the pointer.

For example;

vector<int> foo={1,2,3,4,5};
vector<int> * te=&foo;
te->push_back(6); // its legal.
eday
  • 33
  • 9
1

Completing @eday solution to access elements

vector<int> foo={1,2,3,4,5};
vector<int> * te=&foo;
te->push_back(6);

cout << (*te)[2] << endl; // > 3 
Luc-Olivier
  • 3,715
  • 2
  • 29
  • 29
0

Your problem is that you created a pointer to a vector without creating a vector to which it points. Here's a small example where you create a vector, then use the pointer to do something to it, and then check the original vector to see the result:

#include <iostream>
#include <vector>

using namespace std;

int main(){
    vector<int> myvector;
    vector<int>* te = &myvector;
    int N = 5;
    for(int i = 0; i < N; i++){
        te->push_back(N - i);
    }
    cout<<"te->size() "<<te->size()<<endl;
    for(int i = 0; i < N; i++){
        cout<<"myvector["<<i<<"] = "<<myvector[i]<<endl;
    }
    return 0;
}
Qwert Yuiop
  • 105
  • 1
  • 11