Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Segmentation fault when initialization array
- typedef struct {
- char *s;
- int len;
- } string_t;
- typedef struct {
- uint32_t numb;
- } msg_t;
- void myfunct()
- {
- msg_t msg;
- memset(&msg, 0, sizeof(msg));
- msg.numb = 1;
- char *ClientSendBuf[sizeof(msg)];
- string_t buffer = {ClientSendBuf[sizeof(msg)],strlen(ClientSendBuf[sizeof(msg)])};
- }
- void myfunct()
- {
- msg_t msg;
- memset(&msg, 0, sizeof(msg));
- msg.numb = 1;
- char ClientSendBuf[] = "This is my message";
- string_t buffer = {
- strdup(ClientSendBuf), /*Can return NULL so add error check*/
- strlen(ClientSendBuf)
- };
- /** Or **/
- string_t buffer;
- buffer.s = malloc(strlen(ClientSendBuf)+1);
- if(NULL == buffer.s)
- {
- /* Memory allocation failed. Handle error.*/
- }
- /* Zero fill */
- memset(buffer.s, 0, strlen(ClientSendBuf)+1);
- strcpy(buffer.s, ClientSendBuf);
- buffer.len = strlen(ClientSendBuf);
- /*Opeartions with buffer*/
- /*Call free in both cases !*/
- free(buffer.s);
- }
- char *ClientSendBuff[sizeof(msg)];
- string_t buffer[sizeof(msg)];
- int i;
- for(i = 0; i < sizeof(msg); i++) {
- ClientSendBuff[i] = malloc(100); // you have to initialize (and free when
- buffer[i].s = ClientSendBuff[i]; // you don't need them anymore) every pointer
- buffer[i].len = 100;
- }
Add Comment
Please, Sign In to add comment