Vector malloc

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cstriker
    New Member
    • Mar 2008
    • 3

    Vector malloc

    I have a vector

    std::vector<Cla ssA*> a_vector;

    I need to be able to hold a bunch of ClassA objects in it. I understand it is better to push adress of ClassA objects than the object itself. But i still end up with a bad_alloc since there are about 100,000 of them i need to hold in the vector.

    So my question is how can i malloc some space to a_vector based on the size of ClassA Address?


    I know this is wrong but , how do i correct the following

    ClassA* dummy = new ClassA();int maxEntries = 100000;
    std::vector<Cla ssA*> a_vector = (std::vector<Cl assA>*)malloc(m axEntries*sizeo f(dummy));
    delete dummy;

    Thank You
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    A vector can quite easily deal with 100,000 entries from a theoretical stand point and since you are storing pointers which are likely to be around 4 bytes in size that would be a total of 400,000 bytes which should be a problem for most systems today. Therefore I suspect the problem is something to do with how you are allocating instances of ClassA or the size of ClassA that is causing the problem.

    Comment

    • Studlyami
      Recognized Expert Contributor
      • Sep 2007
      • 464

      #3
      You shouldn't be using malloc the vector has a function called reserve() which will allow you to reserver a block of memory for the vector (you pass in the number of elements you want to reserve for the vector). You can also call the maxsize() function which will tell you the total number of elements that the vector can hold (usually it will be a really large number). Take a look here for a reference on a few of the vector functions.

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        I dont think so this line will compile....

        [code=cpp]
        std::vector<Cla ssA*> a_vector = (std::vector<Cl assA>*)malloc(m axEntries*sizeo f(dummy));

        [/code]

        as you are allocating memory for a stack object.


        Raghuram

        Comment

        • Laharl
          Recognized Expert Contributor
          • Sep 2007
          • 849

          #5
          Yeah, you'd need to cast it to a pointer, since malloc() returns a void*. Frankly, why aren't you using new, since this is C++, not C?

          Comment

          • cstriker
            New Member
            • Mar 2008
            • 3

            #6
            Found that creating instances of ClassA was using up all the memory and is the reason for bad_alloc. Looks like ClassA was heavy. So instead of having a collection of 100,000 objects, i gather all the data ClassA needs to use in a struct and create an object at run time.

            Thank You for all the replies. Appreciate it.

            V

            Comment

            • pootle
              New Member
              • Apr 2008
              • 68

              #7
              Just for interest, if you ever find that you want to use a different allocation scheme than the default, you can provide a custom allocator to standard library container classes. The definition of a vector is vector<T, Alloc>. The Alloc template parameter is used either to select one of the other allocator types, or you can specify your own.

              Regards,

              Comment

              Working...