I've been trying to write a chat with C sockets and I've built the basic program (interaction with only 2 users, not using select)
Anyways, I've come across a weird situation...I was interacting with the client until I've got this error:
The code is this:Code:*** glibc detected *** ./server.o: malloc(): memory corruption (fast): 0x0804a020 ***
======= Backtrace: =========
/lib/libc.so.6[0xb7eb7007]
/lib/libc.so.6[0xb7eb9750]
/lib/libc.so.6(__libc_malloc+0x8d)[0xb7eba7ed]
./server.o[0x80487f6]
/lib/libc.so.6(__libc_start_main+0xe0)[0xb7e66450]
./server.o[0x8048611]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:03 483770 /home/executer/server.o
08049000-0804a000 rw-p 00000000 08:03 483770 /home/executer/server.o
0804a000-0806b000 rw-p 0804a000 00:00 0 [heap]
b7d00000-b7d21000 rw-p b7d00000 00:00 0
b7d21000-b7e00000 ---p b7d21000 00:00 0
b7e37000-b7e43000 r-xp 00000000 08:03 98656 /lib/libgcc_s.so.1
b7e43000-b7e44000 rw-p 0000b000 08:03 98656 /lib/libgcc_s.so.1
b7e4f000-b7e50000 rw-p b7e4f000 00:00 0
b7e50000-b7f7f000 r-xp 00000000 08:03 99969 /lib/libc-2.7.so
b7f7f000-b7f80000 r--p 0012f000 08:03 99969 /lib/libc-2.7.so
b7f80000-b7f82000 rw-p 00130000 08:03 99969 /lib/libc-2.7.so
b7f82000-b7f85000 rw-p b7f82000 00:00 0
b7f8e000-b7f92000 rw-p b7f8e000 00:00 0
b7f92000-b7fac000 r-xp 00000000 08:03 99966 /lib/ld-2.7.so
b7fac000-b7fae000 rw-p 00019000 08:03 99966 /lib/ld-2.7.so
bfa4d000-bfa62000 rw-p bfa4d000 00:00 0 [stack]
ffffe000-fffff000 r-xp 00000000 00:00 0 [vdso]
What's the problem? Why does this happen?Code:while(1)
{
char *receivedData = (char *)malloc(20);
int bytesRead = 0;
if( (bytesRead = recv(newfd, receivedData, 20, 0)) == -1)
exit(1);
receivedData[bytesRead] = '\0';
printf("Client says %s", receivedData);
char *sendData = (char *)malloc(20);
printf("Speak to client: ");
fgets(sendData, 20, stdin);
if(send(newfd, sendData, strlen(sendData), 0) == -1)
exit(1);
free(receivedData);
free(sendData);
}