cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
General C++ Programming
Vector pointer problem
Vector pointer problem
May 28, 2011 at 10:58pm UTC
ryf123
(4)
#include <iostream>
#include <vector>
using namespace std;
main(void){
vector<int> *a;
a->at(0)=0;
printf("%d\n",a->at(0));
}
Something wrong with this code.
It results in segmentation fault.
Could anyone help?
May 28, 2011 at 11:12pm UTC
quirkyusername
(792)
no memory has been allocated to your pointer a. This is how you should initialise a:
vector<
int
> *a =
new
vector<
int
>();
Read up on pointers and dynamic memory if you don't understand what this means. This site is a good place to start
http://www.cplusplus.com/doc/tutorial/pointers/
http://www.cplusplus.com/doc/tutorial/dynamic/
Also I notice that main has no return type here, it should return an int, I'm surprised that this even compiles, which compiler are you using?
Topic archived. No new replies allowed.