K
K
Kalombyr2017-10-07 16:43:24
C++ / C#
Kalombyr, 2017-10-07 16:43:24

What could be the reason for a broken pointer in dynamic memory allocation?

Good day.
I ran into a situation I didn't understand.
There is a static array of pointers in a structure that is created dynamically:

typedef struct {
    void * buf[100];
    ...
} QueueSteps ;

QueueSteps * m = (QueueSteps *) malloc(sizeof(QueueSteps));
if (m==null) {  errorHandler(); }

The array elements themselves are created dynamically in a similar way and placed in the array.
Sometimes it happens that one of the pointers in such an array turns out to be equal to anything, but not a valid pointer. I thought due to memory fragmentation, but errorHandler(); is not called in such cases (during debugging), i.e. all objects are always created successfully.
I would like to clarify two questions:
1. Does malloc handle all possible memory allocation errors (which ones? What does it not take into account?) and return null at the same time?
2. Do I understand correctly that since the array is declared statically, then even taking into account the fact that it is in the structure and memory is allocated dynamically, it will always be "linear" and not scattered in parts (that is, address arithmetic will work normally *(a+9), etc.)
3. If so, is it worth looking for other possible problems?
PS about the release of memory is not forgotten.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Armenian Radio, 2017-10-07
@Kalombyr

Three of your two questions are answered yes, according to the standard of the language. Look for errors elsewhere.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question