0

I've been trying to use an array of pointers to point to vectors, which I have so far been able to implement, however, in trying to add an element to one of the sub-vectors, I repeatedly get an unknown error on run-time.

I have previously defined my array as so:

std::vector<std::string> *frequency_table[10000];

I then try to add an element to a specific one of the vectors. This is the line that causes the run-time error.

frequency_table[index]->push_back(value);

Any ideas?

3
  • 1
    You have an array of pointers - but there's no evidence that you've made those pointers actually point to valid vector instances. Where and how is frequency_table initialized? Commented May 13, 2016 at 4:24
  • It's hard to tell without context, but it sounds like something is going out of scope. Could you post some of your code? Commented May 13, 2016 at 4:25
  • It's more likely that Igor Tandetnik is correct and there was nothing to go out of scope in the first place, but that's moot. This question cannot be answered in it's current state. Best anyone can do is guess and play the odds. More information, preferably an MCVE, is required Commented May 13, 2016 at 4:49

3 Answers 3

3

At first glance the problem looks like you haven't allocated any memory for the pointer so it has nowhere to push the value to. Though i can't be sure without the error message.

If that is the case however you would need to use new to allocate the memory

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

2 Comments

I think you are right that the lack of initialization is the problem. Should I simply do std::vector<std::string> *frequency_table[] = new *std::vector<std::string>[10000];
@vtleavs that would be a syntax error. You have to decide whether you want to have a single block of vectors (in which case don't use pointers at all), or whether you want 10000 separate vectors that you will only create as you need them (in which case initialize the pointers to nullptr, and then call new for each one the first time you use it).
1

Your approach involves mixing vectors (and vectors are a good thing) and C-style arrays of pointers to objects (which betrays a mix-up since there are already vectors in your code).

If you want 10000 vectors of vectors of string, then just write

std::vector<std::vector<std::string> > frequency_table(10000);
. . .
frequency_table[index].push_back(value);

The first line declares a vector, each element of which is a vector<string>, allocates 10000 elements to it and initializes each element.

4 Comments

Your opening line is somewhat misleading. Nothing wrong with a statically sized array of vectors. Say you know you have 10000 lists of an unknown number of elements. It's the pointers to vectors that gets weird, and that's not a C-style thing. An experienced C programmer is just as likely as an equally experienced C++ programmer to look at that and go, "Arrays of pointers to lists? Uhhhh why?"
Basically you are saying I should use a vector of vectors instead of an array of vectors?
@user4581301: You have a point. I'll reword this sentence, thanks.
@vtleavs: You would be better off using STL containers, at least at this stage of your getting familiar with the language, if I am reading you right. Not that you should--it is just the least-error-prone thing you can do.
0

Using the input from you guys, I realized that my problem was the vectors were not being initialized, so I added this loop before the loop in which I populate the vectors, and it works now:

for(int i = 0; i < 5000; ++i)
{
    frequency_table[i] = new std::vector<std::string>;
}

The final code looks like this:

for(int i = 0; i < 5000; ++i)
{
    frequency_table[i] = new std::vector<std::string>;
}

for(auto itr = frequency_map.begin(); itr != frequency_map.end(); ++itr)
{

    std::string key = itr->first;
    double value = itr->second;


    frequency_table[(int)value]->push_back(key);
}

Thanks all!

ps I halved the 10000 for testing purposes

3 Comments

If you are always going to create all the vectors it would be simpler to use vector<string> frequency_table[10000]; and not use pointers or new at all
Well, once you new'd them, you need to delete them, or you are leaking memory. I can relate to your excitement, but there seem to be more problems. Just be careful.
Thanks for that, I was getting memory leaks last night and forgot deleting was a thing. In terms of just creating an array of vectors, there are so many vectors that I need, since I am using this as a pattern recognition frequency table, that I'm worried the nested structure method might cause memory issues.

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.