Allocating an 2D array in C in heap instead of stack

Mojusko

I have not realized that there is a difference between stack an heap allocation in C. I have written a seriously big program in with stack allocated arrays, but obviously they are not sufficiently big to store the read data. Thus, I need to rewrite everything with malloc allocation. Is there a clever way how 2D arrays can be allocated dynamicaly to heap, and their usage in the code to be similar so the stack allocated, meaning that:

My code looks something like this:

int MM,NN;
float Edge[MM][NN];
Do_Something(MM,NN,Edge);

The procedure that is called is defined as:

void Do_Something(int MM,int NN,float Edge[MM][NN]);

I need to rewrite everything with malloc so that these definitions are still valid. Is this possible?

2501

Yes, use pointers to arrays:

int (*pa)[x] = malloc( sizeof(*pa) * y ) ;

Where x and y are inner and outer dimensions. This is is similar in usage to int pa[y][x]. You can use this pointer in your functions.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ allocating class in stack, not in heap

From Dev

Dynamically allocating a 2D array in C

From Dev

Heap array allocation instead of on stack

From Dev

Allocating on heap vs allocating on stack in recursive functions

From Dev

Allocating on heap vs allocating on stack in recursive functions

From Dev

Stack smashing detected when allocating array C

From Dev

c: issues when allocating 2d char array dynamically?

From Dev

C segmentation fault when dynamically allocating 2d array

From Dev

dynamically allocating my 2d array in c

From Dev

Avoiding stack overflows by allocating stack parts on the heap?

From Dev

Allocating a 2d Array

From Dev

C++ allocating large array on heap gives "out of memory exception"

From Dev

Allocate 2D Array in C (not array of pointers) in Heap

From Dev

Dynamically allocating a 2D string array

From Dev

Allocating 2D array dynamically

From Dev

Dynamically Allocating 2D Arrays in C

From Dev

Filling an array of structs and allocating memory on the heap

From Dev

Allocating 2D array with pointer to fixed-size array

From Dev

Runtime Error while allocating an array on the stack

From Dev

Allocating a 2D contiguous array within a function

From Dev

C arrays allocating on the stack with an extra pointer

From Dev

Array as template parameter: stack or heap?

From Dev

Global array allocation -- stack or heap?

From Dev

Allocating an array of a class c++

From Dev

Allocating an array of a class c++

From Dev

Allocating memory to array of strings in c

From Dev

Dynamically allocating an array in a function in C

From Java

Unexpected OutOfMemoryError when allocating an array larger than the heap

From Dev

What are the advantages of using an array of ints vs allocating memory on the heap with malloc?

Related Related

  1. 1

    C++ allocating class in stack, not in heap

  2. 2

    Dynamically allocating a 2D array in C

  3. 3

    Heap array allocation instead of on stack

  4. 4

    Allocating on heap vs allocating on stack in recursive functions

  5. 5

    Allocating on heap vs allocating on stack in recursive functions

  6. 6

    Stack smashing detected when allocating array C

  7. 7

    c: issues when allocating 2d char array dynamically?

  8. 8

    C segmentation fault when dynamically allocating 2d array

  9. 9

    dynamically allocating my 2d array in c

  10. 10

    Avoiding stack overflows by allocating stack parts on the heap?

  11. 11

    Allocating a 2d Array

  12. 12

    C++ allocating large array on heap gives "out of memory exception"

  13. 13

    Allocate 2D Array in C (not array of pointers) in Heap

  14. 14

    Dynamically allocating a 2D string array

  15. 15

    Allocating 2D array dynamically

  16. 16

    Dynamically Allocating 2D Arrays in C

  17. 17

    Filling an array of structs and allocating memory on the heap

  18. 18

    Allocating 2D array with pointer to fixed-size array

  19. 19

    Runtime Error while allocating an array on the stack

  20. 20

    Allocating a 2D contiguous array within a function

  21. 21

    C arrays allocating on the stack with an extra pointer

  22. 22

    Array as template parameter: stack or heap?

  23. 23

    Global array allocation -- stack or heap?

  24. 24

    Allocating an array of a class c++

  25. 25

    Allocating an array of a class c++

  26. 26

    Allocating memory to array of strings in c

  27. 27

    Dynamically allocating an array in a function in C

  28. 28

    Unexpected OutOfMemoryError when allocating an array larger than the heap

  29. 29

    What are the advantages of using an array of ints vs allocating memory on the heap with malloc?

HotTag

Archive