terminate called after throwing an instance of 'std::bad_alloc' what(): St9bad_all

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • upadhyad
    New Member
    • Jul 2008
    • 15

    terminate called after throwing an instance of 'std::bad_alloc' what(): St9bad_all

    Hi everyone !!

    i have an interesting problem...hope you people can help.
    I am coding in C.
    the code is posted below:
    here I have a structure with two character vectors.
    I have to characters. in them depending on a text file. Arround 76,000 characters in each of them one after the other....But i am not able to do so...the program runs fine for small data...but with these 76,000 characters pushed in each vector one after the other causes the following error while runing
    =============== =====
    terminate called after throwing an instance of 'std::bad_alloc '
    what(): St9bad_alloc
    Aborted
    =============== ========
    am I running out of RAM .. what is the problem???
    please help it is urgent!!!

    Code removed, please read the posting guidelines
    Last edited by sicarie; Jul 9 '08, 08:23 PM. Reason: Posting Guidelines
  • upadhyad
    New Member
    • Jul 2008
    • 15

    #2
    why was my code removed???

    Comment

    • upadhyad
      New Member
      • Jul 2008
      • 15

      #3
      The code is here::::Please do not remove this time it is urgen if you keep removing nobody will be able to see it ...thanks;;;;;; ;;;,
      [code=c]Removed as per posting guidelines
      [/code]

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by upadhyad
        The code is here::::Please do not remove this time it is urgen if you keep removing nobody will be able to see it
        And if you read the posting guidelines as suggested in the original edit to your post you would know why your code keeps getting removed.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          It rather depends on the platform you are using but 2 lots of 76000 characters is 152000 bytes of data. This is certainly within the constraints of a modern PC so that suggests that there is another problem with your code.

          Before I removed your code I noticed you have vectors of char *, do you allocate memory for all those pointers before using them or at least point them at a buffer somewhere?

          Comment

          • gpraghuram
            Recognized Expert Top Contributor
            • Mar 2007
            • 1275

            #6
            Hi,
            Dont post the whole code...
            Post the part how u have declared the vector and how are u pushing into it.


            Raghu

            Comment

            • upadhyad
              New Member
              • Jul 2008
              • 15

              #7
              Hi all !!
              Now I understand ....I am really sorry ........afteral l i am a newbie.
              Ok
              here is how I define my structure....
              [Code=C]
              struct IDnTime
              {
              vector<char *> s;
              vector<char *> g;
              struct tm theTime;
              struct tm *ptr;
              time_t t;
              } IDnTimeArray[50];
              [/Code]

              Next I inititalize few char variables and then point them with the pointers like this:(this I did to avoid pushing characters themselves in the vectors....now this should allow me to push the addresses of the characters or the poiters to thode characters in the vector)
              [Code=C]

              char Walk ='w',Bike ='b',Car ='c',UrbanPuT ='u',Rail ='r',Notdecided ='n',Activity ='a';
              char *walk =&Walk, *bike =&Bike, *car =&Car, *urbanPuT =&UrbanPuT, *rail =&Rail, *notdecided =&Notdecided, *activity =&Activity;
              [/Code]

              Then i try to push back like this
              [Code=C]
              IDnTimeArray[counter1].s.push_back(&A ctivity);
              IDnTimeArray[counter1].s.push_back(&W alk);
              [/Code]
              and so on.......76,000 character addresses in the vector 's'..and similarly for the vector g as well
              [Code=C]
              IDnTimeArray[counter1].g.push_back(&A ctivity);
              IDnTimeArray[counter1].g.push_back(&W alk);
              [/Code]

              the program compiles fine...gr8..the n I run it ..it runs for 2 or three seconds ...gr8...then as the pushing back operation in the 's' vector of the array of the structures is completed and the g vector is started it stops .....when I run it on my computer the process it terminated here is

              terminate called after throwing an instance of 'std::bad_alloc '
              what(): St9bad_all
              Aborted



              i dont get this .....PLEASE HELPi dont get this .....PLEASE HELP

              Also few may ask whether I try to fill all the 's' and 'g' vectors of the 50 structure array locations together.......
              the answer is NO!...first I approach each structure array element and then i try to fill the s and g vectors of that location(whch is not allowed and the message displayed i mentioned above)...then as i finsh my desired function with these vectors I clear these vectors by
              [Code=C]
              IDnTimeArray[counter1].s.clear();
              IDnTimeArray[counter1].g.clear();
              [/Code]
              So the memory is freed......

              HELP HELP

              Comment

              • upadhyad
                New Member
                • Jul 2008
                • 15

                #8
                It rather depends on the platform you are using but 2 lots of 76000 characters is 152000 bytes of data. This is certainly within the constraints of a modern PC so that suggests that there is another problem with your code

                the platform used is ubuntu terminal

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  A bad_alloc() us an an exception thrown by C++ when it runs out of heap memory.

                  Comment

                  • upadhyad
                    New Member
                    • Jul 2008
                    • 15

                    #10
                    whn I try to push back just one of the vectors and comenting out the pushing back in the g vector the process works fine....and similarly for the g vector.
                    where can be the problem ???
                    I tried it with the vector resize option also....but it did not work either

                    Comment

                    • gpraghuram
                      Recognized Expert Top Contributor
                      • Mar 2007
                      • 1275

                      #11
                      Why cant u change the vector<char*> to vector<char> as u are pushing only the character.
                      Try again after changing this...


                      Raghuram

                      Comment

                      • upadhyad
                        New Member
                        • Jul 2008
                        • 15

                        #12
                        Originally posted by gpraghuram
                        Why cant u change the vector<char*> to vector<char> as u are pushing only the character.
                        Try again after changing this...


                        Raghuram

                        that is how tried it in the first place...it did not work either


                        I found an interesting thing that if i push back in g first and comment out pushing back in s it works
                        if i push back in s first and comment out pushing back in g it works
                        if I push s after g it doesn't work for g but works for s
                        if I push g after s it doesn't work for g but works for g

                        i am getting sick!!

                        Comment

                        • gpraghuram
                          Recognized Expert Top Contributor
                          • Mar 2007
                          • 1275

                          #13
                          Originally posted by upadhyad
                          that is how tried it in the first place...it did not work either


                          I found an interesting thing that if i push back in g first and comment out pushing back in s it works
                          if i push back in s first and comment out pushing back in g it works
                          if I push s after g it doesn't work for g but works for s
                          if I push g after s it doesn't work for g but works for g

                          i am getting sick!!
                          I think this shuld be some memory issue....


                          raghu

                          Comment

                          • hdanw
                            New Member
                            • Feb 2008
                            • 61

                            #14
                            I hate to correct you'll but...


                            Banfa
                            AdministratorVo R

                            but 2 lots of 76000 characters is 152000 bytes of data
                            2 lots of ADDRESSES to 76000 characters is only 608K on x86 and 1.2Mb on an a64

                            But the allocation granularity may consume more than that.
                            You cannot allocate a space that is in 1 byte chunks! So pushing char instead of char * wont make much difference.


                            weaknessforcats
                            Moderator
                            Re: terminate called after throwing an instance of 'std::bad_alloc ' what(): St9bad_all
                            --------------------------------------------------------------------------------

                            A bad_alloc() us an an exception thrown by C++ when it runs out of heap memory.
                            Is not intirely correct. When a program starts it is given a process heap, the C Run Time creates a second heap that it manages internally. When the new operator finds that it does not have enough memory to serve the request it calls HeapAlloc() in the Kernel32.dll to get more.

                            To Quantify, The C run Time throws an exception when HeapAlloc() fails, for whatever reason. The issue is that the HeapAllocation tries to keep a contiguous block of physical ram. Fragmentation of physical ram can cuase this exception long before you run out of memory.

                            In addition, Windows uses LowFragmentatio nHeaps(), where different size allocations go in different sections of the heap, and are managed by seperate lists to improve performance and decrease "Heap" Fragmentation.

                            http://msdn.microsoft. com/en-us/library/ms810603.aspx

                            The default and dynamic heaps are basically the same thing, but the default heap has the special characteristic of being identifiable as the default. This is how the C run-time library and the Win32 subsystem identify which heap to allocate from. The GetProcessHeap function returns a handle to the default heap for a process. Since functions such as GlobalAlloc or malloc are executed within the context of the thread that called them, they can simply call GetProcessHeap to retrieve a handle to the default heap, and then manage memory accordingly.
                            You can specify a reserve size for the Heap that is well above the 1mb default in visaul studio use the /HEAP option.

                            Good Luck!


                            Dan Hollingsworth
                            Owner
                            Business, Information, and Technology

                            Comment

                            • Banfa
                              Recognized Expert Expert
                              • Feb 2006
                              • 9067

                              #15
                              Originally posted by hdanw
                              2 lots of ADDRESSES to 76000 characters is only 608K on x86 and 1.2Mb on an a64

                              But the allocation granularity may consume more than that.
                              You cannot allocate a space that is in 1 byte chunks! So pushing char instead of char * wont make much difference.
                              The first bit is true I got that wrong, but the second is not. A vector guarantees contiguous memory allocation exactly like that you would get for an array of the same type. In this case where the size is so large while there may be a few bytes allocation granularity make cause a few extra bytes to be allocated this will be insignificant compared to the memory allocated for memory storage. Therefore using char instead of char * will result in sizeof(char *)/sizeof(char) (==sizeof(char *)) times less memory used.

                              Your discussion of how it works on Windows is interesting but unfortunately the OP has already said they're using Ubuntu :D

                              Comment

                              Working...