Reputation: 57
I am new to C++ and programming and I'm writing a small program as part of an assignment and got it to work, but I am trying to make the code run faster, so i'm trying to get my vector to store a pointer to a struct.
The struct is
struct info {
string all;
string word;
}
And what I did was, trying to assign the string 'all' a value;
info* v;
v->all = str;
And str is defined as
string str = "Hello";
The error that I got upon running GDB was;
File "/usr/share/gdb/python/libstdcxx/v6/printers.py", line 469, in to_string
return self.val['_M_dataplus']['_M_p'].string (encoding, length = len)
OverflowError: signed integer is greater than maximum
Any clue as to what might be causing this?
Upvotes: 5
Views: 2472
Reputation: 11787
You cant use a memory location unless its allocated. info *v
creates only a pointer to the memory location and at present its pointing to junk. you have to allocate memory to it using new
. after new
ing you acn use the str
to assign value to it.
or you can also use static memory allocation
Upvotes: 0
Reputation: 11232
info* v;
just defines pointer which is pointing to some random memory location , to use it you need to allocate a memory for info
and make this pointer point to this memory. You can do it using new
like this: info* v = new info();
. Note that you need to release the memory yourself by doing delete v;
.
Upvotes: 1
Reputation: 23324
v
is not initialized.
Make it
info* v = new info;
v->all = str;
But you really should show more code. It's not clear, what you are trying to do and how storing a pointer will make the code run faster.
Upvotes: 2