Thread: using memcpy to fill a struct

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    2

    using memcpy to fill a struct

    I try to fill the parent structure with a memcpy. I work on embedded system (with Contiki OS). So I try to avoid using a malloc because I read that it is not recommended.

    I test the code on my computer and I get the error Segmentation fault (core dumped).


    On Contiki there are no errors but the values after the fill call are not the expected ones.


    I have no idea why this error occurs and I don't know another way to do this.


    Here is an example of the code that looks like what I'm trying to do:
    Code:
    typedef struct child{
        uint8_t age;
        uint16_t size;
    }child_t;
    
    
    typedef struct parent{
        child_t *child1;
        child_t *child2;
        bool has_child;
    }parent_t;
    
    
    void fill(parent_t *dest){
        parent_t parent;
        child_t child;
        
        uint8_t age=24;
        uint16_t size=180;
        
        child.age=age;
        child.size=size;
        
        printf("h2\n");
        memcpy(parent.child1, &child, sizeof(child_t));//Segmentation fault (core dumped)
        printf("h3\n");
    
    
        memcpy(dest, &parent, sizeof(parent_t);
    }
    
    
    int main(int argc, char *argv[]){
    
    
        parent_t parent;
        fill(&parent);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,609
    Well part of the reason for the current problem is that memcpy() expects "destination" to be passed by "reference" not by "value". However where are you allocating memory for those pointers in your parent_t?

  3. #3
    Registered User
    Join Date
    Apr 2021
    Posts
    18
    On top of that, it appears that you've copied your code incorrectly here, because I can see a missing ) on line 29.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    502
    Quote Originally Posted by leGrandNono View Post
    I try to fill the parent structure with a memcpy. I work on embedded system (with Contiki OS). So I try to avoid using a malloc because I read that it is not recommended.
    If parent_t contains a pointer to a child_t rather than an instance of a child_t, then you have to create the memory space for the child_t somehow. That can be by using malloc, or if you don't want to use malloc() you could have a global array of child_t's you allocate from using your own function.

    But you can't just leave the pointer dangling. It must either be NULL or point to valid memory.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,908
    Ask yourself what do the child_t pointers in parent_t point to when you try to copy data to it?

  6. #6
    Registered User
    Join Date
    May 2021
    Posts
    2
    Thanks for your answers


    I replaced the pointers in parent_t with values and the code works.


    If I understand correctly, it didn't work because no memory was allocated for the child_t pointers ?

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,908
    That is correct

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memcpy to struct
    By Ducky in forum C++ Programming
    Replies: 11
    Last Post: 07-29-2015, 09:47 PM
  2. Replies: 5
    Last Post: 04-03-2013, 10:03 PM
  3. fill in a struct with fscanf
    By GDB in forum C Programming
    Replies: 6
    Last Post: 06-24-2012, 11:41 AM
  4. memcpy with struct and pointer
    By neeraj_rn in forum C Programming
    Replies: 10
    Last Post: 08-28-2010, 07:46 AM
  5. Fill in a struct from file
    By MTK in forum C Programming
    Replies: 4
    Last Post: 06-07-2010, 03:32 PM

Tags for this Thread