Thread: Memory problem with structure and strings

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    10

    Memory problem with structure and strings

    I've searched for some solutions and tried some different variations, but I'm having a lot of trouble assigning a value to a string in a structure. I'm using the following structure:
    Code:
    struct database
        {
            string name;
            string site;
            int coun;
        }data[4557];
    
    string holder;
    string first = "<1>";
    i = 0;
    
    while(getline(calibrate, holder))
    {
            if (!holder.find(first, 0))
            {
                holder.erase(0, 3);
                data[i].name = holder;
                ++i;
            }
    }
    Running valgrind shows that the problem occurs at the line where data[i].name is assigned the value of holder, specifically that there is an uninitialized value starting when main() is declared.
    Do I need to allocate memory for name, site, and coun when I declare them? What exactly is going on here?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you post the smallest and simplest compilable program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2016
    Posts
    10
    This is the simplified program
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        ifstream calibrate ("withrefm");
        if (!calibrate.is_open())
        {
            cout<<"Error opening file\n";
        }
    
    
        struct database
        {
            string name;
            string site;
            int coun;
        }data[4557];
    
        int i;
        string first = "<1>";
        string holder;
    
        //enzyme* enz = new enzyme[4557]; // current structure can handle up to 4557 enzymes at once
    
        i = 0;
        while(getline(calibrate, holder))
        {
            if (!holder.find(first, 0))
            {
                holder.erase(0, 3);
                data[i].name = holder;
                ++i;
            }
    
    return 0;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should check that i < 4557 in the while loop before the getline call, and presumably use a named constant instead of 4557.

    Also, this is probably wrong:
    Code:
    if (!holder.find(first, 0))
    Perhaps it should be:
    Code:
    if (holder.find(first) == string::npos)
    Oh, and since you are going to overwrite holder on the next iteration anyway, I would rather change this:
    Code:
    holder.erase(0, 3);
    data[i].name = holder;
    to:
    Code:
    if (holder.length() > 3)
    {
        data[i].name = holder.substr(3);
    }
    data[i].name should be an empty string to begin with, and this way seems a bit more intuitive, i.e., copy a substring rather than shorten a string and then copy it.

    Personally, I would move the struct database definition to file scope, but still declare data to be local to main.
    Last edited by laserlight; 07-15-2016 at 11:21 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2016
    Posts
    10
    Thanks for the advice, turns out that I simply needed to make i < 4557 another condition of the while loop. Now it works perfectly.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps you could consider this instead.
    std::vector<database> data;

    Arrays with magic lengths like 4557 are just old school.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-19-2015, 05:51 PM
  2. Replies: 7
    Last Post: 11-05-2011, 05:11 PM
  3. Problem with scanf, strings and structure
    By teng40z in forum C Programming
    Replies: 2
    Last Post: 12-27-2010, 12:13 AM
  4. Replies: 4
    Last Post: 04-25-2010, 10:57 AM
  5. Problem with pointers/strings/memory
    By Mitch in forum C Programming
    Replies: 16
    Last Post: 01-27-2005, 07:36 PM

Tags for this Thread