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:
k = (char*) malloc(10);
Here 10 is random number. Initial size.
If you want to increase the size later use realloc
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:
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.
0 Comments