allocate memory on the heap

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mar11
    New Member
    • Sep 2009
    • 38

    #1

    allocate memory on the heap

    Hi all,

    I have problem by allocating a memory for my objects. Assuming this lines

    1- object* test;

    2- test = new object ;

    3- test->Attribute = true
    4- ....
    5- delete test;

    if i repeat this code several times then I get error by allocating the memory in line 2

    The error that i got is:
    test-project: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offse tof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__buil tin_offsetof (struct malloc_chunk, fd_nextsize))+( (2 * (sizeof(size_t) )) - 1)) & ~((2 * (sizeof(size_t) )) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.


    May someone clarify me why the simulation is aborted by allocating memory through new operator?

    thanks in advance..
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    Line 1 refers to 'object *test',which is a pointer named 'test' of type 'object' which returns a type object and is uninitialized. Line 2 tries to create a type on the heap with the pointer 'test'??? as far as I know you can't create types this way...you need to use a class named 'object'. I would be interested in reading a more lucid (and accurate) explanation from an expert

    Comment

    • iammilind
      New Member
      • Sep 2006
      • 14

      #3
      I don't see any problem in your code.
      What do you mean by "if i repeat this code several times, i get error". Please paste full code and what are you trying to do?

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        it's hard to tell, but when you are deleting the object, does the object's destructor clean up after itself, and delete its members?

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          When you say you are repeating the code several times, does this mean you delete your pointer several times? If so, that is your problem.

          A pointer can only be deleted once. What happens is that the new operator returns the address of your allocated memory but it also secretly allocated memory before your pointer. This header has the information about how big the allocation is so when you delete, the correct bytes get returned to the heap for reuse. The delete also marks the header bytes so that another delete will find the header format incorrect. This second delete will now crash the program.

          The easy fix is to set a deleted pointer to zero. The delete operator does nothing if the pointer is zero. This is OK:

          Code:
          char* ptr = new char;
          delete ptr;
          ptr = 0;
          delete ptr;

          Comment

          Working...