1

To me it seems like it should be so simple, but I just feel like I'm missing something.

I have in my header file a private variable _stoplist When I declared it in the header file the code looks just like this.

private:
    std::string _stoplist[];

But when later in my function I decide to access this, it segfaults on anything.

_stoplist[_length];
//cout << _length << prints 104 (its a valid int and everything)
_stoplist[0] = "b";

Crashes in the std::string.assign() code with a segfault. I have a gut feeling that I'm missing something obvious here but I haven't quite found out what yet.

Thanks in advance!

EDIT: Ok, thanks for all the help. For anyone else who may read this, I would recommend to use one of the answers below since that is the smart way to do it. In my case though since I needed to dynamically allocate it without using vector I just used the following code.

private:
    std::string *_stoplist;

and then in my cpp file

_stoplist = new string[_length];

Yeah, turns out that it really was way simple, and I just was over looking that part.

1
  • 2
    I just wanted to note that variable length arrays like this aren't legal C++, they're a GCC extension. Commented Oct 21, 2011 at 3:47

2 Answers 2

2

You're getting an array out of bounds error because _stoplist doesn't have a size. You should either give it a size, and only access elements within that range, such as:

private:
    std::string _stoplist[100];

now you should be able to index _stoplist[0] through _stoplist[99]. However a better solution would probably to use std::vector instead, as it's a lot safer.

private:
    std::vector< std::string > _stoplist;

Then you can use its member functions such as resize() to grow it to whatever size you need.

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

5 Comments

As much as I'd like to use std::vector, this is a class assignment so I'm not allowed to. Maybe I should've been clearer in the question, but I do need this list to be dynamically sized, so does that mean that I need to declare it like this: std::string *_stoplist[] and then use _stoplist[] = new string[_length]?
But this is definitely the right answer for any other person who would have this problem.
@Bob: Tell your teacher he's an idiot, then rewrite a simplified version of vector yourself, if you can.
@GMan: That's a great way to start a good relationship with a teacher... I would just code the assignment the way it was asked, and then mention that std::vector is a better choice, not call the teacher names like a child.
@bstamour: I think it was clear I never actually intended for him to do that.
1

That's because a variable declared as std::string[] is basically just a pointer. To use it, you need to allocate memory for it. If you want to allocate fixed memory for it, try declaring it as e.g. std::string _stoplist[5]; instead.

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.