Does dynamic memory allocation decrease?

I watched a video by Bjarne Struustrup where he explains why avoiding linking to lists.

Basically, when memory is dynamically allocated using pointers, the number of misses in the caches is greater, which reduces performance.

But, if the same thing applies to non-linear data structures, such as trees and graphs, does the same?

Because the trees also have two pointers for each node, and again there will be random pointers moving, leading to misses in the cache.

But trees have proven that they work better than linear data structures. Of course, trees can also be implemented using arrays, but again, there is huge memory consumption.

My question is: Is dynamic memory allocation good or not?

+4
source share
3 answers

He tried to point out that multilateral feedback between small objects tends to decrease the effectiveness of code caching. You cannot completely avoid related structures, but in some cases you may prefer a “flat” layout, which, for example, is suitable for the entire cache line.

- , , "", O(n), next . , n , log(n) .

, :

struct point {
    int x, y;
};

struct rect {
    struct point origin;
    struct point size;
};

rect point, rect "", , . - , rect , , , origin.x .

, "" , rect , . , , , , , .

+7

. , , ( std).

, , ( ), - , .

, , , ( - , - - ).

, , . , . , , . . . , , .

+2

If you want to allocate memory dynamically or on the stack, it completely depends on your implementation. In general, you use the stack if the variable or object lives only inside the function, and you use the heap if the variable or object must live inside the entire program and be controlled by several functions.

0
source

All Articles