C++ dynamic array causes segmentation fault at assigment
Posted
by
opc0de
on Stack Overflow
See other posts from Stack Overflow
or by opc0de
Published on 2012-09-15T15:35:11Z
Indexed on
2012/09/15
15:37 UTC
Read the original article
Hit count: 185
I am doing a application witch uses sockets so I am holding in an array the sockets handles.I have the following code:
while(0 == 0){
int * tx = (int*)(malloc((nr_con + 2) * sizeof(int)));
if (conexiuni != NULL)
{
syslog(LOG_NOTICE,"Ajung la eliberare %d",nr_con);
memcpy(&tx[0],&conexiuni[0],(sizeof(int) * (nr_con)));
syslog(LOG_NOTICE,"Ajung la eliberare %d",nr_con);
free(conexiuni);
}
conexiuni = tx;
syslog(LOG_NOTICE,"Ajung la mama %d",nr_con);
//The line bellow causes a segfault at second connection
if ((conexiuni[nr_con] = accept(hsock,(sockaddr*)(&sadr),&addr_size)) != -1)
{
nr_con++;
syslog(LOG_NOTICE,"Primesc de la %s",inet_ntoa(sadr.sin_addr));
syslog(LOG_NOTICE,"kkt %d",conexiuni[nr_con - 1]);
int * sz = (int*)malloc(sizeof(int));
*sz = conexiuni[nr_con - 1];
syslog(LOG_NOTICE,"after %d",*sz);
pthread_create(&tidi,0,&ConexiuniHandler, sz);
}
}
When I connect the second time when I assign the array the program crashes. What am I doing wrong? I tried the same code on Windows and it works well but on Linux it crashes.
© Stack Overflow or respective owner