Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9237163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:28:23+00:00 2026-06-18T07:28:23+00:00

I’m not very experienced programmer in C++ and I have a problem which I

  • 0

I’m not very experienced programmer in C++ and I have a problem which I can’t resolve. The project on which I’m working is quite big so I can’t post here all codes. It is too much code and too much explanation. I write just little part of code, the part which causes me problem, so I hope it is enough. Sorry for the long of my question but I want explain all posted code. Maybe this part of code isn’t enough to solve the problem but I want to try it.

First I have a struct called “record”:

struct record {
    vector<string> dataRow;
    vector<string *> keys;
    vector<string *> values;

    void setDataRow(vector<string> r) {
         dataRow = r;
    }
}

Some of string data are marked as keys and others as values. I next processing is better for me to have all string data in one vector, so that’s the reason why I don’t have two vectors of string (vector keys, vector values).

Then I have this:

vector< vector<record> > resultSet;

vector is like data table – set of lines with string data. I need specific count of these tables, therefore vector of vectors of records. The count of tables is optional, so when I set table count I prepare tables by reserve function:

resultSet.reserve(count);
for(unsigned int i = 0; i < count; i++) {
    vector<record> vec;
    resultSet.push_back(vec);
}

When I want add new record to resultSet I know the number of table to which I need insert record. After resultSet[number].push_back(rec) I need change pointers in vectors “keys” and “values” because push_back() creates new copy of “rec” with values of “dataRow” in other memory addresses, right? So I have this function which does push_back and updates pointers:

void insert(int part, vector<string> & dataRow) {
    record r;
    r.setDataRow(dataRow);

    resultSet[part].push_back(r);
    int pos = resultSet.size() - 1; // position of last record
    resultSet[part].at(pos).values.clear();
    resultSet[part].at(pos).keys.clear();

    for(unsigned int i = 0; i < dataRow.size(); i++) {
        record * newRec = &resultSet[part].at(pos);
        if(isValue(dataRow[i])) {
            newRec->values.push_back(&(newRec->dataRow.at(i)));
            // control cout...
        } else {
            newRec->keys.push_back(&(newRec->dataRow.at(i)));
            // control cout...
        }
    }
}

This is working. After push_back in newRec I did control cout of inserted pointers and their referenced values, and everything was ok.

But! After some inserts I call function processData(resultSet), which has to process all data in resultSet. Before implementing processing od data I just wanted print all keys for control to find out if everything is alright. This code:

for(unsigned int i = 0; i < resultSet.size(); i++) {
    for(unsigned int j = 0; j < resultSet[i].size(); j++) {
        cout << "keys: ";
        for(unsigned int k = 0; k < resultSet[i].at(j).keys.size(); k++) {
            cout << *resultSet[i].at(j).keys.at(k) << ", ";
        }
        cout << endl;
    }
}

is bad (Same problem with printing values vector of record). It throws exception of Access violation reading. I know that this exception is thrown when I want to read unaccessible memory, right? Please, tell me that I have mistake in code written above because I really don’t know why it doesn’t work. Before processing resultSet I do nothing with resultSet except some count of inserts.

Thank you for reading and possible answers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:28:24+00:00Added an answer on June 18, 2026 at 7:28 am

    If I have understood your question correctly, the reason for all this is a fundamental misconception in the way vectors behave.

    Your code stores pointers in a vector that points to memory locations allocated by another vector. That would be fine if the vectors didn’t change.

    The reason for this is that a std::vector is a container that makes a guarantee – all the data it contains will be allocated in a contiguous block of memory.

    Now, if you insert an element into a vector, it may move memory locations around. Hence, one of the things you should know is that iterators need to be considered invalid when a vector changes. Iterators are sort of a generalized pointer. In other words, pointers to the locations of elements inside a vector become invalid too.

    Now, let’s say you updated all your pointers, everywhere, when any of the vectors involved changed. You would then be fine. However, you’ve now got a bit of an uphill battle on your hands.

    As you’ve said in your comments, you’re using pointers because you want efficiency. Your struct is essentially a collection of three strings. Instead of using your own struct, typedef a std::tuple (you will need a C++11 compiler) of 3 std::strings.

    Finally, when you need to access the data within, do so by const reference and const_iterator unless you need to modify any of it. This will ensure that

    1. You don’t have duplication of data
    2. You’re making maximum use of the STL, thereby minimizing your own code and the possible bugs
    3. You’re relying on algorithms and containers that are already really efficient
    4. You’re using the STL in a way it was meant to be used.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array which has BIG numbers and small numbers in it. I
I have a jquery bug and I've been looking for hours now, I can't
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a text area in my form which accepts all possible characters from
I have been unable to fix a problem with Java Unicode and encoding. The
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.