Search code examples
carraysmemorymemory-managementprogramming-languages

Array in a memory-constrained system


Consider that my system has memory, but it is scattered in different places (fragmented). There are no four contiguous memory locations that are free. In that scenario, if I declare a character array of size 10 in the C language, what will happen ?


Solution

  • If "my system has memory, but it is scattered in different places(fragmented)" means, that heap virtual memory is fragmented, and "declare a character array of size 10" means, that you create character array of size 10 in stack memory:

    char str[10];
    

    , then array will be successfully created.

    If "declare a character array of size 10" means, that you allocate memory with malloc() (allocate in heap):

    char *str2;
    str2 = (char*) malloc(10 * sizeof(char));
    

    , then malloc() will return NULL.