STL Vector memory allocation issues

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kwisatz
    New Member
    • Jan 2007
    • 4

    STL Vector memory allocation issues

    HI All,

    have a struct which has a vector defined

    typedef struct X_
    {
    ...
    ..
    vector<int> y;
    ..
    } X;

    Now in my main program, I first allocate memory to X

    Xinstant = (X*) malloc(sizeof(X ));

    So do I need to allocate seperate space for the vector?

    When I try Xinstant->y.push_back(wh atever), I get runtime errors - seg faults.
    Any problem here?

    Thanks!
    -k
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I think your trouble is that you are using malloc, try using new.

    One of the things that new does (that malloc does not) is ensure that all the constructors for classes (and structures) are called correctly.

    The way your code currently is I do not think that the vectors constructor is being called.

    Comment

    • kwisatz
      New Member
      • Jan 2007
      • 4

      #3
      Thanks for the reply, but it *barely* worked.

      The issue is this:

      I have function fun1(){
      X* Xinstant;

      Xinstant = (X*) new X;
      ..
      ..
      fun2(Xinstant);
      }

      fun2(Xinstant)
      {
      Xinstant->push_back(what ever) <---- I get a "what(): St9bad_alloc error"

      }

      I don't see what the problem is..
      thanks!

      k

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Originally posted by kwisatz
        Thanks for the reply, but it *barely* worked.

        The issue is this:

        I have function fun1(){
        X* Xinstant;

        Xinstant = (X*) new X;
        ..
        ..
        fun2(Xinstant);
        }

        fun2(Xinstant)
        {
        Xinstant->push_back(what ever) <---- I get a "what(): St9bad_alloc error"

        }

        I don't see what the problem is..
        thanks!

        k
        First of all, the typecast you have for creating XInstant is unnecessary. The new operator automatically allocates memory for the object, calls its constructor, and returns a pointer to that object. In other words, the new operator is like a function that returns a pointer - in this case, a pointer of type X*. So you can redo this statement as

        Code:
        Xinstant = new X;
        Next, are you making sure that, when you push_back(whate ver), that 'whatever' is of type int? It would be bad if you were pushing back a double, for example, and this could produce a bad_alloc error (as the compiler allocating int memory, not double memory).

        Comment

        • kwisatz
          New Member
          • Jan 2007
          • 4

          #5
          yep. I do Xinstant->y.push_back((i nt)whatever)

          But now I get the following error:
          SIGSEGV :

          std::_Construct <int, int> (__p=0x4f000000 0b, __value=@0x7fff ffb2f9d8)
          at stl_construct.h :81
          81 ::new(static_ca st<void*>(__p)) _T1(__value);

          Clearly its a memory issue, but I can't seem to place why this is happening.
          Xinstant has other members as well, and they are all assigned. I get this problem with the vector only..

          Thanks much for the replies, much appreciated.

          -k

          Comment

          • kwisatz
            New Member
            • Jan 2007
            • 4

            #6
            ok solved the problem. I replaced all my mallocs with new and seems to be working now. Thanks much again!
            -k

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              I wasn't into programming when malloc() was standard, but I suppose mixing your mallocs and news can mess a lot of stuff up. Glad to see you got it working.

              Comment

              Working...