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!

3
  • don't cast the result of malloc Commented May 6, 2016 at 1:37
  • memcpy(&messages[count],temp,sizeof(temp)); should also be sizeof(*temp) Commented May 6, 2016 at 2:33
  • "I have the following struct:" - No! You have pointers to that struct Commented May 6, 2016 at 3:23

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

6 Comments

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 what's the expected behavior?
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 as @immibis pointed, please change to memcpy(&messages[count],temp,sizeof(temp)); and try again to see if it solved the problem.
strangely, after memcpy(temp->data, input_buff, nread); messages[count].data is still NULL in all cases :/
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.