1

I have the following struct:

struct message {
    int id;
    int ack;
    int data_len;
    char *data;
    time_t timer;
} *messages, *temp;

I am allocating a chunk of memory to hold upto numMessages amount of these structs:

messages = malloc(sizeof(messages)*numMessages);

And then n messages can be added by calling the function ReadFromFile(int), with count initialized to 0

void readFromFile(int n) {

    char input_buff[4096];
    int size = sizeof(struct message);
    for (int i = 0; i < n; i++) {
        bzero(input_buff, sizeof(input_buff));
        int nread = fread(input_buff, 1 , msgSize, fp);

        if (nread > 0) { 
           printf("adding message: %d\n", count);

           temp = (struct message *) malloc (sizeof (struct message)); 
           temp->data_len = nread;
           temp->id = count; // set integer id
           temp->data = malloc( sizeof(char) * ( nread ));
           temp->ack = 0;

           memcpy(temp->data, input_buff, nread); 
           memcpy(&messages[count],temp,sizeof(temp));

           count++;
           free(temp);

        } 

        if (nread < sizeof(input_buff)) {
            if (feof(fp))
            printf("End of file\n");
            free(filename);
            close(fp);
            break;
        }

        if (ferror(fp)) {
            printf("Error reading\n");
            break;
        }
    }
}

HOWEVER, messages[count].data isn't being stored. However, if I swap the lines:

temp->data_len = nread; 

and

temp->data = malloc( sizeof(char) * ( nread ));

The data is properly stored, but now data_len isn't stored? What am I doing wrong? Besides the fact I'm sure that having a pointer temp and than coping the memory is redundant...

Thank you!

mdibound
  • 101
  • 1
  • 11

1 Answers1

2

messages is a pointer to struct message, its size is not the same as the struct, change to this:

messages = malloc(sizeof(struct message) * numMessages);
fluter
  • 13,238
  • 8
  • 62
  • 100
  • Thank you! However, it's still not properly saving. when I add some print statements I get: adding message: 0, data_len: 0, nread 4096, Saved data: (null), Saved data_len: 0, adding message: 1, data_len: 0, nread 4096, Saved data: (null), Saved data_len: 0, adding message: 2, data_len: 53, nread 2048, Saved data: (null), Saved data_len: 53, End of file – mdibound May 06 '16 at 01:55
  • @mdibound what's the expected behavior? – fluter May 06 '16 at 02:26
  • each struct should store the data that is read from disk to input_buffer, as well as store the value nread as data_len so that messages[0].data contains the first chunk read from file, and messages[0].data_len = the number of bytes read... however neither of these are being saved. – mdibound May 06 '16 at 02:34
  • @mdibound as @immibis pointed, please change to `memcpy(&messages[count],temp,sizeof(temp));` and try again to see if it solved the problem. – fluter May 06 '16 at 02:39
  • strangely, after memcpy(temp->data, input_buff, nread); messages[count].data is still NULL in all cases :/ – mdibound May 06 '16 at 04:24
  • How is 'count' defined? Also check it after the loop? – fluter May 06 '16 at 04:26