-1

I have the following struct -

struct data
{
unsigned char r;
int f;
};

I then try to print it but get a segmentation fault at the print statement. What am I doing wrong and how can I do the memcpy and print ?

struct data *data1;
char temp[10];
data1->r = 1; data1->f = 2;                                                           
memcpy(temp,(char *)(struct data *)data1, sizeof(struct data));
printf("buffer is %s\n",temp );
jackmack
  • 3
  • 2

1 Answers1

0

You are missing the allocation of the memory. Check this code and let me know any questions:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

struct data{
    unsigned char r;
    int f;
};

int main(){
    struct data *data1=(struct data*)malloc(sizeof(struct data));
    char temp[100];

    data1->r = 255; data1->f = 1;                                                           
    memcpy(temp, data1, sizeof(struct data));

    printf("size of struct: %d\n", (int)sizeof(struct data));
    printf("buffer is: \n");
    //code to print the buffer in binary in chunks of 4
    for(int i=0; i<sizeof(struct data); i++){
        char v=temp[i];
        for(int j=0; j<8*sizeof(char); j++){
            if(v & 1)
                printf("1");
            else
                printf("0");
            v>>=1;
        }
        if((i+1)%4==0) printf("\n");
        else printf(" ");
    }
    printf("\n");
};

the output:

size of struct: 8
buffer is:
11111111 00000000 00000000 00000000 //r
10000000 00000000 00000000 00000000 //f

The data is actually copied

Ivan Gonzalez
  • 446
  • 1
  • 5
  • 14
  • in C, do not cast the returned value from calls to the heap allocation functions: `malloc()`, `calloc()`, and `realloc()` – user3629249 Jun 21 '19 at 23:04
  • the posted data will only be correct if the system it is run on is `little endian` I.E. A 'big endian' system will show drastically different (ordering) of the result data – user3629249 Jun 21 '19 at 23:06
  • regarding: `for(int j=0; j<8*sizeof(char); j++){` the C standard defines `sizeof(char)` as 1. Therefore `8*1` is still 8 – user3629249 Jun 21 '19 at 23:07
  • the output: `size of struct: 8` will only be true if the underlying hardware architecture is 32 bit. If the underlying hardware architecture is 64 bit, then value will be 16 and only the first 1/2 of the data will be output – user3629249 Jun 21 '19 at 23:10
  • I'm adding 8*sizeof(char) for readabilty. Why the casting is not recommended?. The data i'm printing is just for reference, i wasn't intended for the right order only to display some reference. – Ivan Gonzalez Jun 21 '19 at 23:10
  • regarding: `if(v & 1) printf("1"); else printf("0"); v>>=1;` this is printing the LSB bit first, so the output will be backwards from the reality – user3629249 Jun 21 '19 at 23:12
  • the returned type from the heap allocation functions is `void*` which can be assigned to any pointer. Casting just clutters the code, is error prone, especially when performing debugging and/or maintenance – user3629249 Jun 21 '19 at 23:23