c - segmentation fault (core dumped) in simple assignment -


i'm using c , wrote code on freebsd system.

///// defines /////  #define cpucores                2 #define threadamount            cpucores - 1 #define nullptr                 null  ///// typedefs /////  typedef enum bool_e {     e_false = 0,     e_true = 1 }bool_t;  typedef struct newclient_s { int sdnewclient; struct sockaddr sdinclientip; socklen_t sdlenipsize; }newclient_t;  typedef struct clientthreadarg_s {     int iinternid;     newclient_t sincommingclient;     bool_t bhaskillsig;     bool_t bisshutdown; }clientthreadarg_t;  //------------------------------------ imagin main      clientthreadarg_t *splistofarguments;     size_t sizeindexi;     splistofarguments = (clientthreadarg_t *) malloc (threadamount * sizeof     (clientthreadarg_t));       (sizeindexi = 0; sizeindexi < threadamount; sizeindexi++) {        splistofarguments[sizeindexi].bhaskillsig = e_true;//heres error.... } 

this code snippet figured out responsible error, don't understand why. tried allocating mem double space required , still got error. allocated 100 elements appeared enough... didn't had error anymore. sizeof clientthreadarg_t suggested 36 , loop runnes 1 time. so, i'm doing wrong in way i'm allocating memory structure?

it's hard tell when can't run code, of it, might here

 splistofarguments = malloc (threadamount * sizeof(clientthreadarg_t));  

notice threadamount defined cpucores - 1 2 - 1

what means getting

 splistofarguments = malloc (2 - 1 * sizeof(clientthreadarg_t));  

and mallocing negative number return null


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -