Skip to content
Advertisement

debugging c using heap memory [closed]

I am working with heap memory and I wrote an example below:

JavaScript

I execute is as : ./heap test and the output is:

JavaScript

I think something wrong is happening with “Distance between” \ max length of argv[1] in int type is “31” of integers\ and max length of argv[1] in char type is “58” of characters\ for example:

JavaScript

after that I face an open error… why -32 is happening for distance between?

Advertisement

Answer

There is nothing wrong here. The pointers are assigned by the malloc() function, which can and does assign fairly arbitrary values to the pointers. A normal malloc() implementation will reserve a few bytes to mark the length of the allocated buffer, as well as taking advantage of existing buffer sizes on the free list. So if you allocate two buffers, as you have done, there is nothing saying they have to be contiguous in heap space. In fact, they will not be.

I don’t know what you are trying to do here, but the ‘distance between’ two malloc() pointers isn’t going to be the exact length of the buffers, ever.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement