简体   繁体   中英

Is it safe to use head segment of dynamic array in a multi threaded program?

When we defined dynamic array in C/C++ it's using head segment to keep track of number of elements in the array(in heap). for example :

int* mem = new int[8];

compiler will allocate sizeof(int)*8 bytes.

int *temp = malloc(sizeof(int)*9)

Will store "8" in the first sizeof(int) bytes and it goes like this

*temp = 8;

and then set mem address with next consecutive address respect to temp

mem = temp + 1;

Therefore, mem will points to an array of 8 elements, not 9.

And when deletion happens compiler will use mem in reverse process respect to above to deallocate memory in that heap block

delete[] mem;

My question is
If we allocate a dynamic memory which will be used in different modules in run-time and we managed to retrieve number of elements using head segment of allocated heap memory,Is it safe to use in Multi-threaded environment?

(Please assume that in each module by program design,no helper function or attribute provided to retrieve number of elements( size ) in defined dynamic array.Each module only passing address(pointers) to array but not its size)

No, it is not safe in any environment. You can't rely on the compiler storing the number of elements in the array before the allocated memory, as it is not defined behavior. You should never try to get to this value.

Instead, do not even use dynamic arrays, but opt for std::vector .

Accessing mem - 1 has undefined behaviour.

compiler will ...

If you target that version of compiler, and that version only, and the compiler vendor documents the behaviour, then perhaps you can trust that the version of the compiler indeed does as you have observed.

But a program that depends on it will be non-portable to other compilers, and possibly other versions of that same compiler, and possibly to other platforms that use same version of the same compiler.

This is all regardless of multi vs single thread.


If we allocate a dynamic memory which will be used in different modules in run-time and we managed to retrieve number of elements using head segment of allocated heap memory,Is it safe to use in Multi-threaded environment?

It is safe to use dynamic memory in a multi threaded program as much as it is safe to use any memory. Of course, multi threaded implies the potential for data races. You must synchronize modifications to any objects that may simultaneously be accessed by other threads. Whether the objects are in dynamic memory makes no difference.

I tested this with Visual Studio release build:

int * mem = new int[8];

results in a call to malloc equivalent to

int * mem = malloc(32);    // 32 = 8 * sizeof(int)

The size of the integer array is not stored anywhere, (including the malloc header as far as I can tell, the malloc header just has some type of link/flag used to recombine blocks allocated (and virtually mapped) from the heap).

Using undocumented system information is in principle out of question, even if it seems to work: it might behave differently in unexpected circumstances and there is no guarantee of portability, even across the same manufacturer over time.

If you want to play with matches anyway, make sure that no other thread performs a memory-allocation-related operation between the moment you read the size and the moment you access the block; you need to implement a critical section for this purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM