Like Tree3Likes
  • 1 Post By King Mir
  • 2 Post By laserlight

C++ Dynamic Arrays

This is a discussion on C++ Dynamic Arrays within the C++ Programming forums, part of the General Programming Boards category; Hey all. I'm trying to get dynamic memory working (im making a quick file-system based database system, and I need ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    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,021
    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
    21,451
    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.
    AndrewHunter and iMalc like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Which is why i have theCode:

    if(i>=aSize)...

    You write into the memory before that.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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, 11:53 AM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Dynamic Arrays
    By swayp in forum C++ Programming
    Replies: 5
    Last Post: 01-27-2005, 05: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21