473,385 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

allocate memory on the heap

38
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_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_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..
May 12 '10 #1
4 4817
whodgson
542 512MB
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
May 13 '10 #2
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?
May 13 '10 #3
jkmyoung
2,057 Expert 2GB
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?
May 13 '10 #4
weaknessforcats
9,208 Expert Mod 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. char* ptr = new char;
  2. delete ptr;
  3. ptr = 0;
  4. delete ptr;
May 13 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

37
by: Curt | last post by:
If this is the complete program (ie, the address of the const is never taken, only its value used) is it likely the compiler will allocate ram for constantA or constantB? Or simply substitute the...
4
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on...
4
by: marora | last post by:
I have created class definition which contains a charater pointer as one of it's data memeber. The objective is to read some data from a file, and assign it to a data member; Size of data is...
11
by: Divick | last post by:
Hi, can somebody help me figure out how can I make write a function which inturn uses malloc routine to allocate memory which is 2^k aligned? The condition is such that there should not be any...
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
14
by: raghu | last post by:
Hello , This is Raghu. I have a problem in declaring a structure. Consider struct hai{ int id; char sex; int age; }; here when a variable is instianted for this structure then immediately
5
by: raghu | last post by:
Hello , This is Raghu. I have a problem in declaring a structure. Consider struct hai{ int id; char sex; int age; }; here when a variable is instianted for this structure then immediately
1
by: Racerx | last post by:
Hi All: I use db2 v8.1 fixpack 3 on AIX 5.1. I used to get a memory heap error few months ago. I sorted it out by setting the NUM_POOLAGENTS and MAXAGENTS equal. Also there was an issue with...
5
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS...
0
by: Varav | last post by:
We have a simple web service for DB2 to consume. I have tested the SOAPHTTPV and it is pretty slick. It is great that a database can do this. I first tested single requests over and over by...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.