1

Is it possible to allocate an array on heap even if it is scoped inside a function? Here is my C program:

    void SimpleTextEditor()
    {
        char textEditor[1000000];
        char operationText[1000002];
        //do something with the arrays
    }

This results in stackOverflow exception for obvious reason that I'm trying to allocate two big sized arrays. If I move any one of the arrays outside the function and make it global (file level variable) then it works as global variables are allocated on heap.

But I don't want to make my variables global. Is it not possible to allocate the memory dynamically for arrays using malloc and calloc?

RBT
  • 24,161
  • 21
  • 159
  • 240

3 Answers3

2

Is it not possible to allocate the memory dynamically for arrays using malloc and calloc?

Of course it is possible:

char *textEditor = malloc(1000000);
char *operationText = malloc(1000002);
//do something with the arrays
free(operationText);
free(textEditor);

(note: you could use 1000000*sizeof(char) if you want, but sizeof(char) is always 1)

user253751
  • 57,427
  • 7
  • 48
  • 90
2

If I move any one of the arrays outside the function and make it global (file level variable) then it works as global variables are allocated on heap.

That's false assumption. The array defined at file scope is likely to be placed within data segment, or more specifically within the .bss segment, since no explicit initializer was given.

But I don't want to make my variables global. Is it not possible to allocate the memory dynamically for arrays using malloc and calloc?

It is certainly possible to allocate the memory dynamically using malloc and friends. However, because the sizes are known at compile time, the better alternative may be declare both arrays as static:

void SimpleTextEditor()
{
    static char textEditor[1000000];
    static char operationText[1000002];
     //do something with the arrays
}
Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
  • So a static array variable even if it is local scoped to a function, it will NOT go to stack memory area. Is it? – RBT Feb 08 '17 at 02:03
  • @RBT: That's right. It will go to the data segment, just like the global variable. The only difference is about scope. – Grzegorz Szpetkowski Feb 08 '17 at 02:05
  • My application is a single threaded data structure related program. Should I really bother about @M.M 's concern for this solution not being thread-safe. How static variables are affected by the ability of a program to support re-entrancy? – RBT Feb 08 '17 at 06:52
  • @RBT: The static variables are "shared" between invocations. The same data is keeped between function calls. As long as you don't plan recursive calls (either direct or indirect) of `SimpleTextEditor`, then it won't matter in practice. – Grzegorz Szpetkowski Feb 08 '17 at 09:36
  • I get a warning when I try to free-up a static variable `warning: attempt to free a non-heap object ‘textEditor’ [-Wfree-nonheap-object]` The warning is obvious but I was just wondering how the allocated space in these special areas like data segment and bss get freed up once my function has finished executing. – RBT Feb 10 '17 at 02:59
  • @RBT: Formally, an object with static storage duration (such as static variable defined inside block) has lifetime of the whole program's execution. The data is freed automatically when program ends. The `.bss` segment is special, because it does not contribute as such as other global data to the executable file's size. See [this](http://stackoverflow.com/questions/9535250/why-is-the-bss-segment-required?noredirect=1&lq=1) for more information. – Grzegorz Szpetkowski Feb 10 '17 at 10:44
1

It is defineltly possible to use malloc() and calloc() to allocate memory dynamically. Just allocate them in functions or main() if you don't wish to make them global. When dealing with big data, it is best to allocate space on the heap, just incase you need to make more space for more data.

  • When using malloc():

    malloc() allocates requested memory on the heap, then returns a void* pointer to it.

    Example:

    char *textEditor = malloc(1000000);
    char *operationText = malloc(1000002);
    
  • When using calloc():

    calloc() allocates requested memory on the heap, then returns a void* pointer to it. Sets memory to 0.

    Example:

    char *textEditor = calloc(1000000, sizeof * textEditor);
    char *operationText = calloc(1000002, sizeof * operationText);
    
  • Be sure to safely free() these at the end.

    free() deallocates the memory previously allocated by malloc(), calloc() or realloc().

    Example:

    free(textEditor);
    textEditor = NULL;
    
    free(operationText);
    operationText = NULL;
    

Note: Both malloc() and calloc() can return NULL on error, so its best to check them. You check like this:

if (textEditor == NULL || operationText == NULL) {
    /* handle exit */
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
  • Being from C# background the allowed syntax for calloc `char *textEditor = calloc(1000000, sizeof * textEditor);` is really weird for me. I would have possibly written it as `char *textEditor = calloc(1000000, sizeof (char));` for clarity. – RBT Feb 09 '17 at 02:48
  • 1
    @RBT That is also fine. I am from a C background, so I find `char *textEditor = calloc(1000000, sizeof * textEditor);` nice. But they both do the same thing, no problem if you choose `sizeof(char)`. – RoadRunner Feb 09 '17 at 02:49