Does C provide a free method submit method?

I have a question about the dispatch method C.

int send (int socket, void *buffer, size_t size, int flags); 

My code is:

 char *buffer = (char *)malloc(100*sizeof(char)); send(s, buffer, 100*sizeof(char), MSG_NOSIGNAL); 

Now I was wondering if I should free the buffer myself or send the send method?

My code where I free the buffer is:

 char *buffer = (char *)malloc(100*sizeof(char)); send(s, buffer, 100*sizeof(char), MSG_NOSIGNAL); free(buffer); 

When I saw the error, I thought that I soon freed the buffer, and the send method still used memory.

Tell me.

+4
source share
5 answers

send does not free the buffer. You need to free him. Besides the fact that it just was not designed in such a way (socket functions are independent of heap functions), sending will not really โ€œknowโ€ if the memory should be freed, because it can be a pointer to the middle of the selected fragment or it can be pointer to stack data.

This must be โ€œsafeโ€ to immediately free the buffer after the send call because the function copied the data into its own internal buffers. The code you show will not cause corruption problems (although I suspect that more code is involved because the buffer is not initialized as shown). It is possible that the heap was damaged earlier in the process and did not raise an exception until a later time (for example, after sending).

The return value of send indicates the number of bytes sent successfully (although not necessarily successfully delivered). Therefore, although it would be โ€œsafeโ€ to free it, you would lose unsent data if it werenโ€™t all sent. If the entire buffer has not been sent, you will need to send the package again with the remaining amount.

+4
source

You can free save the buffer after calling send by noting the following:

  • The entire buffer may not have been sent.
  • The call returns the number of bytes that the kernel copied to its own buffers. You must save the rest.

This code can help you understand what is happening, but in no case should you base yours on this:

 char *buf; // sizeof(*buf) must be 1 for pointer arithmetic ssize_t sent = send(fd, buf, len, 0); if (sent != -1) { // no error // move the unsent bytes to the start of the buffer memmove(buf, buf + sent, len - sent); // resize the buffer to fit the left over bytes buf = realloc(buf, len - sent); } 
+1
source

send () does not free the buffer. If you have problems with double freedom, you will probably see where you will release it and see how you got there.

0
source

in general, it would be very unusual if the api function freed a buffer. the only function that I know that frees a buffer is "free" ...

sometimes functions allocate memory that you should free after using them, for example. strdup

There are some functions in which you cannot release the buffer immediately after using them. for example, in .Net, you cannot dispose of the bitmap data pointer if it is still used in the wrapper of the bitmap.

0
source

It sounds from your description as if you were suffering from your code using the pointer after it was released. There are good, reliable ways to catch this type of problem, and then there are quick hacks. Here is a quick hack:

 #define free(p) do { (free)(p); p=(void*)1; } while (0) 

This macro, which you want to include in your header file or in the first line of your project, replaces your calls with free ones to:

  • The memory is still free() 'd, the (free)(p) notation makes the C compiler evaluate (free) as a function symbol, not a macro extension.
  • The pointer is set to an unavailable value, 1. NULL (0) was not used because free() would not complain if NULL passed. Passing a "pointer value" of 1 should cause an error report.

You may find that after using this macro, your code overrides the deprecated pointer. When you have free'd memory, pointers to it will point to the same memory and temporarily point to the same values โ€‹โ€‹in memory - until the second use of this memory leaks out of these values. Because of this, your program may run on multiple lines using obsolete pointers. Changing each pointer to 1 when it becomes deprecated will immediately catch that.

0
source

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


All Articles