Discussion point: Question 1 (Declaring array in C)

Discussion point: Question 1 (Declaring array in C)

If we have to declare an array whose usage changes dynamically, what is the most preferred declaration in embedded systems?

char k[];

or

char k[1000]

Note: 1000 here is just random size used by coder is unsure and wanted to have some size

Responses:

  • Array is static and we should use linked lists
  • Memory allocation can be done in the below manner:
char *k;
k = (char*) malloc(10);
Here 10 is random number. Initial size.
If you want to increase the size later use realloc

  • Even though both char [] and dynamic memory allocation does the job, it is efficient to use dynamic memory allocation as the programmer has control over the way memory is allocated.
  • If the programmer wants to keep it simple without much complexity, it is better to declare a maximum array size and use the error checking mechanism to verify the array size.

Post a Comment

0 Comments