error: initializer element is not constant

3

I'm trying to declare this buffer on global and when compiling displays the following error

  

error: initializer element is not constant

char    *ls_buffer_PPouAUT =    malloc(5120*sizeof(char));

How can I resolve it?

    
asked by anonymous 22.07.2016 / 15:35

1 answer

4

Placing the variable inside a function.

Do not use anything global. It is unnecessary in almost all cases. In cases that may be helpful you need to know what you are doing, understand all of the implications. You are probably using this inadvertently.

Of course, you can still declare the global variable and initialize it within the function, ideally right at the beginning of main() not to be in invalid state. But avoid it, it's better. Create a local variable and go as an argument to other functions or store in a structure that makes sense.

Global variables are initialized during compilation. You can not execute the malloc() function at that time, during compilation it can only have constant values, so they can be placed inside the generated executable.

Because this value can only be known at runtime, it must be at one point within the application's run flow. Only the functions have run flow.

    
22.07.2016 / 15:46