Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Keil compiler uses malloc for local variables,why?

I had a problem somewhere in my code in a function that I wanted to declare an array but it failed. after some debugging I found out that it uses malloc in disassembly window so I increased the heap size and it works fine!

So my question is why keil uses Heap for local variable?

Here is the variable declaration code:

uint8_t result[data->capacityBytes];
memset(result, 0, sizeof(result));

I've added flag C99

like image 416
Alireza Avatar asked Dec 23 '22 19:12

Alireza


1 Answers

Your array has a dynamic size, i.e. the compiler does not know how big it will be until runtime. This is a feature introduced in C99 called variable length arrays (VLA).

According to Keil's documentation (see the Note), such arrays are allocated on the heap by this compiler. (Others might allocate on the stack. Others might not implement this feature at all - it became optional in C11.)

like image 180
Mat Avatar answered Dec 29 '22 08:12

Mat