Malloc outside main () or any other function (i.e., globally)

I need a buffer allocated for the heap common to the class (for use as a notepad during calculations). At some point, I can free and then redistribute the buffer if it is not large enough. I wanted the buffer to exist without calling "myclass :: initialize ();" in main (); I came up with the following code that compiles and works well for my purpose.

My questions are: why is this code compiling correctly? Why is malloc () allowed to be outside of main () or any other function? Does the compiler interpret this somehow and remove malloc?

Code compiled on linux 64bit using "g ++ example.cpp" and tagged with valgrind

// example.cpp #include <cstdio> #include <cstdlib> class myclass { public: static char* pbuf; // buffer static unsigned int length; // buffer length const static unsigned int chunk_size; // allocation chunck size }; // set constants and allocate buffer const unsigned int myclass::chunk_size = sizeof(long unsigned int) * 8; unsigned int myclass::length = chunk_size; // start with smallest chunk char* myclass::pbuf = (char*)malloc(sizeof(char)*myclass::length); int main() { // write to buffer (0 to 63 on 64bit machine) for (int i = 0; i < myclass::length; i++) { *(myclass::pbuf+i) = i; } // read from buffer (print the numbers 0 to 63) for (int i = 0; i < myclass::length; i++) { printf("%d\n", *(myclass::pbuf+i)); } free(myclass::pbuf); // last line of program } 

Thanks for answers. That sounds more often than I thought. "Function permissions are allowed in static initializers." This leads me to a slightly modified version that breaks a possible malloc error:

 #include <cstdio> #include <cstdlib> class myclass { public: static char* pbuf; // buffer static unsigned int length; // buffer length const static unsigned int chunk_size; // allocation chunck size static void* malloc_buf(unsigned int); }; // set constants and allocate buffer const unsigned int myclass::chunk_size = sizeof(long unsigned int) * 8; unsigned int myclass::length = chunk_size; // start with smallest chunk //char* myclass::pbuf = (char*)malloc(sizeof(char)*myclass::length); char* myclass::pbuf = (char*)myclass::malloc_buf(sizeof(char)*myclass::length); void* myclass::malloc_buf(unsigned int N) { void* buf = malloc(N); if (!buf) exit(EXIT_FAILURE); return buf; } int main() { // write to buffer (0 to 63 on 64bit machine) for (int i = 0; i < myclass::length; i++) { *(myclass::pbuf+i) = i; } // read from buffer (print the numbers 0 to 63) for (int i = 0; i < myclass::length; i++) { printf("%d\n", *(myclass::pbuf+i)); } free(myclass::pbuf); // last line of program } 
+5
source share
2 answers

It just does static initialization (initialization is called before main is called). Static initializers are allowed to call functions.

+2
source

main() is another function - therefore, such special requirements are set on it to allow it to be called correctly.

Other things can and do happen before they are called. Static initialization among them.

+1
source

Source: https://habr.com/ru/post/1236192/


All Articles