5

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?

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

And remember about delete v, when it is no longer needed.
Thank you for the response. In making v a pointer to info I deliberately added a '*' and forgot about the 'new'...
1

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;.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.