char * buf = (char*)calloc(0,0); buf = "vygbubinon"; printf("%s\n", buf); 

"Vygbubinon" is displayed. Although the idea of ​​memory is not enough allocated

    3 answers 3

    Let's take a look at the sentences of the code fragment you specified. In the first sentence

     char * buf = (char*)calloc(0,0); 

    memory of zero size is allocated. According to the C language standard (7.22.3 Memory management functions)

    If the size is not true, it’s not necessary to make a note. .

    That is, you can request a memory allocation of zero size, but the result will depend on the platform where the program is compiled and executed. Either a NULL value or some valid memory address will be returned, however you cannot access the object. So this sentence is correct.

    Further in the next sentence, regardless of what the result of the previous sentence was (either NULL or some valid address), the pointer buf reassigned and gets the address of the first character of the string literal "vygbubinon" .

     buf = "vygbubinon"; 

    String literals have static memory. Therefore, the pointer receives the address of the first character of the string literal located in the static memory region. Memory for string literals is reserved by the compiler at compile time. (Compilers usually collect all the literals present in the program into a certain pool of literals.) There is nothing incorrect in this sentence. The only problem associated with this proposal is that if the previous sentence allocated memory, then you can not delete it, because the address of this memory due to the reassignment of the pointer was lost. A memory leak will occur, but there will be no compile time or runtime error.

    Well, and finally, this literal is displayed on the console.

     printf("%s\n", buf); 

    So the code snippet is completely correct except for a possible memory leak, which is only a logical error.

      insufficient memory allocated

      No, that's enough. Here is the code:

       buf = "vygbubinon"; 

      does not copy the string to memory pointed to by buf . It only assigns the buf pointer the address of the string "vygbubinon" , lying somewhere in the data segment.

      If it is clearer, then to the level below it might look something like this:

       .data ; ... _some_static_string db "vygbubinon", 0 buf dd ? ; ... .code ; ... mov buf, offset _some_static_string 

      If you need to copy a string to the memory area whose address is contained in the pointer, you need to use the functions of the strcpy () , memcpy () and other families.

        char *buf is just a pointer variable. In fact, it is simply the address of some memory cell. In the case of strings, this is the address of the very first character. Therefore, in the first line you assign the address of the allocated memory to the variable buf , but not the memory itself. And in the next line you assign the buf value from the program data area, i.e. your "vygbubinon" . It should be borne in mind that if you do not free a piece of memory allocated in the heap by calling malloc (if you did not allocate 0 ) and overwrite the pointer to it, then a memory leak occurs

        • "memory leak" - in this case it will not. - PinkTux
        • In this - yes, because 0 stands out, but this is just in case, because with this, too, there are errors :) - selya