Shivakumar
Shivakumar

Reputation: 477

Memory allocation failure using new

I have allocated memory using operator new. The type of data to which memory needs to be allocated is 'uint8_t' and I am using 'uint32_t' type for the size . For example,

ptr = new uint8_t[size];

where ptr is of type uint8_t and size is of type uint32_t.

Now, we have got memory crash which points to this type of allocation in our code and the error message thrown was:

"_int_malloc: Assertion (unsigned long)(size) >= (unsigned long)(nb) failed."

One of our team member suggests that mismatch of ptr and size is the reason for the crash and I disagree with him.

Please explain if this could be the reason for the crash and if so, how ?

Also, Please explain what are the cases where we get the above error message.

Upvotes: 1

Views: 617

Answers (1)

bames53
bames53

Reputation: 88235

The type of size doesn't really matter: whatever type it is, it's essentially being passed as a parameter to a function that takes size_t.

uint32_t size = 10;
uint8_t *ptr = new uint8_t[size];

This is perfectly valid code and there's no need for the type of ptr and size to be coordinated.


The error you're encountering at this line is indicating an internal error in the memory allocation library. It can appear to be caused by legal code when your program has elsewhere done something illegal that corrupts the state of the program.

You may be able to find the initial cause by using some other analysis tools such as valgrind, ubsan, or perhaps a static analyzer.

Upvotes: 3

Related Questions