c - char array vs. char pointer -


While retrieving data through a socket, I have found that:

four buffers [4] ]; Memeset (buffer, 0, 4); Recv (socket, buffer, 4, 0);

I got

mesgx

"mesg" I added, with some random characters added.

If I use

 char * method = (char *) malloc (4); Memeset (buffer, 0, 4); Recv (socket, buffer, 4, 0); Instead, I get 

mesg

, so there is no random thing related to my string. I came to know that if I use four [5] instead it works well, but I really do not understand why malloc (4) actually allocates 5 bytes, the fifth one NUL?

call maulok (4) actually divides only four bytes The only coincidence was that the next byte became a NULL in memory, which shut down your string for you.

In the situation where you allocated four buffers [4] on the stack, the next byte was in the form of a 'x' some other stuff Afterwards, your string still continues until it found the next NUL byte

The socket function only works with the bytes, and do not consider the bytes as wire with trailing NUL or some extra. You really ask exactly what you want.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -