Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to allocate (dynamically) 2d array?

Status
Not open for further replies.

electroRF

Member
Hi,
I looked over the web for how to dynamically allocate 2d array, but didn't find the answer I was looking for.

Could you please share a nice way to do that?

Thank you.
 
You can't dynamically allocate 2-dimensional array in C so that you could access it the traditional way like this:

value = array[x][y];

because this code is preprocessed by the compiler to this form:

value = *(array + ((x*NUMBER_OF_COLUMNS) + y));

The NUMBER_OF_COLUMNS is a constat. So, the compiler needs to know the exact dimensions of your array at compile time.

You can use a 1-dimensional array and a macro to mimic 2-dimensional array.
Here is an example for 16*5 array.

And.. to avoid the complexity of dynamic memory allocation, it is better to define one array that can hold the largest possible capacity you ever need.

C:
// This is the helper macro
#define index(x,y)  (((x)*y_size) + (y))

// reserve some memory for 1-dimensional array.
int array[128];

// we also need helper variables that define the array dimensions.
char x_size = 16;
char y_size = 5;

// using a macro to access this as 2-dimensional array.
array[index(15,3)] = something;


If you need many arrays, here is maybe the easiest way to allocate data for different kinds of 1-dimensional arrays, if you don't have malloc, and you do not want to go into details how the memory is organized or how to write a full memory managent system, you can reserve a "large enough" memory pool that you can use.

C:
// reserve some memory
unsigned char memory_pool[256]
int used_mem = 0;

// Pointer for mimiced 2-dimensional array
int *array1;
// Pointer for 1-dimensional array
char *array2;

// Get a chunk of that memory.. 2-dimensional 16*5 integer array
array1 = (int*)(&memory_pool[used_mem]);
used_mem += 16*5*sizeof(int); // simple memory book keeping

// Lets get another chunk.. 1-dimensional array with 128 elements
array2 = (char*)(&memory_pool[used_mem]);
used_mem += 128*sizeof(char);

// you can calculate how much memory you have left like this
int memory_left;
memory_left = sizeof(memory_pool) - used_mem;
If you need to re-allocate the array sizes or free memory etc. Then this becomes much more complicated.. you need a book-keeping system for all allocated memory blocks and functions to manipulate these chunks. It can get you head spinning, but the result is very efficient dynamic memory allocation system. And actually not very difficult system to understand on paper, but it looks messy in code.

Also you need to know the limits of your device and know your compiler. From your previous posts I assume that you use XC8 compiler, so you need to read section 3.5 in the documentation: https://ww1.microchip.com/downloads/en/DeviceDoc/52053A.pdf
I think my example overcomes all the possible issues explained in that documentation (well, you need to make sure that the memory size you reserve is not too big).
 
Last edited:
Yes and no.. I don't program any big things. I love C in the embedded environment. If I need to program something for PC, I use C# (.NET).
I don't think I could work as programmer in a software company. Well, maybe I could (in a small company), but I don't want to :)
 
cool.
Thanks for sharing.
What do you do for a living?

If you like programming, why not making a living out of it?
 
I teach embedded systems at my university. Part time job :)
I would like a job in a small company that does some kind of embedded systems, but the opportunity has not come yet. I will never work for a big company... I hate them.

And, if you want to know why I hate them, this guy explains it all. I agree with him 100%
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top