Thread: C++ Dynamic Arrays

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    62

    Question C++ Dynamic Arrays

    Hey all. I'm trying to get dynamic memory working (im making a quick file-system based database system, and I need the dynamic alloc in there) but it doesnt seem to be working...
    Right now im using this code to test (with NetBeans g++):
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main() {
        int aSize = 5;
        string* dmt = new string[aSize];
        string* temp = NULL;
        int i = 0;
        int ii = 0;
        ostringstream oss;
        while(i<=100)
        {
            oss << i;
            dmt[i] = oss.str();
            oss.str("");
            if(i>=aSize)
            {
                aSize = aSize * 2;
                temp = new string[aSize];
                while(ii<=i)
                {
                    temp[ii] = dmt[ii];
                    ii++;
                }
                delete dmt;
                dmt = temp;
                temp = NULL;
            }
        }
        for(int iii=0; iii<=100; iii++)
            cout << dmt[iii] << endl;
        return 0;
    }
    and this leads to the cmd to instantly crash (no error flag of a bad allocation has been sent)...
    can anyone say what I have done wrong and how to fix it?

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    dmt points to an array of std::strings of size 5.

    But your while loop tries to use values beyond dmt[4] , which causes undefined behavior.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You need to set ii to 0 before your second while loop.

    The usual convention with loop variables is to use i, then j, then k, not i, then ii, then iii.

    But you're error is likely caused by the fact that you write to dmt[aSize] each time you reach it, just before making the array larger. That's writing past the end of the array.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    62
    Quote Originally Posted by manasij7479 View Post
    dmt points to an array of std::strings of size 5.

    But your while loop tries to use values beyond dmt[4] , which causes undefined behavior.
    Which is why i have the
    Code:
    if(i>=aSize)...
    part...

    Quote Originally Posted by King Mir View Post
    You need to set ii to 0 before your second while loop.

    The usual convention with loop variables is to use i, then j, then k, not i, then ii, then iii.

    But you're error is likely caused by the fact that you write to dmt[aSize] each time you reach it, just before making the array larger. That's writing past the end of the array.
    I have added
    Code:
    ii = 0;
    right after the second while loop, and moved the if block to the top of the main loop. Now it is no longer crashing, but instead gives absolutely no output, as if it was in an infinite loop, but without being as resource-hungry as some things (1st processor core passes from 20% to 76% on execution, and stays there, but no overloading)... it seems that there is still some problem, here is the updated code:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main() {
        int aSize = 5;
        string* dmt = new string[aSize];
        string* temp = NULL;
        int i = 0;
        int ii = 0;
        ostringstream oss;
        while(i<=100)
        {
            if(i>=aSize-1)
            {
                aSize = aSize * 2;
                temp = new string[aSize];
                while(ii<=i)
                {
                    temp[ii] = dmt[ii];
                    ii++;
                }
                ii = 0;
                delete dmt;
                dmt = temp;
                temp = NULL;
            }
            oss << i;
            dmt[i] = oss.str();
            oss.str("");
        }
        for(int iii=0; iii<=100; iii++)
            cout << dmt[iii] << endl;
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You never increase i.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    62
    Quote Originally Posted by King Mir View Post
    You never increase i.
    oh WOW, how did I miss that @_@.
    thanks XD, in the earlier builds i was increasing it, but it seems it got removed at some point in time... probably when i was putting in the ostringstream instead of using atoi. Thank you ^^ *Like*.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Shingetsu Kurai
    I'm trying to get dynamic memory working (im making a quick file-system based database system, and I need the dynamic alloc in there)
    It sounds like you are not using manual memory management because you have to or for the sake of learning; rather, you are using it because you need dynamically allocated memory.

    Therefore, #include <vector> and use a std::vector<std::string> instead.
    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

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Which is why i have theCode:

    if(i>=aSize)...

    You write into the memory before that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d dynamic arrays
    By zbest in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2008, 12:53 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Dynamic Arrays
    By swayp in forum C++ Programming
    Replies: 5
    Last Post: 01-27-2005, 06:18 AM
  4. Dynamic Arrays?
    By leeor_net in forum C Programming
    Replies: 2
    Last Post: 06-11-2004, 10:24 PM
  5. Dynamic arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 05-13-2002, 05:51 PM

Tags for this Thread