Defining STructures

This is a discussion on Defining STructures within the C Programming forums, part of the General Programming Boards category; Code: struct Ethfrm_t { struct FCfrm_t* ptr; unsigned char DestEthAdr[6]; unsigned char SrcEthAdr[6]; UINT32 VLAN_HdrSave; UINT32 reserved; UINT32 pad; unsigned ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    Defining STructures

    Code:
    struct Ethfrm_t
    {
    	struct FCfrm_t* ptr;
    	unsigned char DestEthAdr[6];
    	unsigned char SrcEthAdr[6];
    	UINT32  VLAN_HdrSave;
    	UINT32  reserved;
    	UINT32  pad;
    	unsigned char Ethbuffer[BUF_SIZE];
    }  __attribute__ ((packed));
    
    Ethfrm_t * Ethfrm;
    Ethfrm = malloc(sizeof(*Ethfrm));
    I'm not really sure how this works, but so if i define a structure with a pointer. Theres somewhere in memory that should have the size of this huge data. Because when i do sizeof(Ethfrm) I get 8 because its the size of the pointer. So if this was the way to create the struct. Is there a way to make a copy of this Struct but using new memory.

    Code:
    Ethfrm_t * newframe;
    newframe = malloc(sizeof(*newframe));
    newframe = Ethfrm;
    Does this copy all the data in the first struct into the second struct?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    newframe = malloc(sizeof(Ethfrm_t));
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Quote Originally Posted by kiros88 View Post
    Code:
    struct Ethfrm_t
    {
        struct FCfrm_t* ptr;
        unsigned char DestEthAdr[6];
        unsigned char SrcEthAdr[6];
        UINT32  VLAN_HdrSave;
        UINT32  reserved;
        UINT32  pad;
        unsigned char Ethbuffer[BUF_SIZE];
    }  __attribute__ ((packed));
    
    Ethfrm_t * Ethfrm;
    Ethfrm = malloc(sizeof(*Ethfrm));
    I'm not really sure how this works, but so if i define a structure with a pointer. Theres somewhere in memory that should have the size of this huge data. Because when i do sizeof(Ethfrm) I get 8 because its the size of the pointer. So if this was the way to create the struct. Is there a way to make a copy of this Struct but using new memory.

    Code:
    Ethfrm_t * newframe;
    newframe = malloc(sizeof(*newframe));
    newframe = Ethfrm;
    Does this copy all the data in the first struct into the second struct?
    No, that just reassigns the pointer (losing track of the memory allocated by malloc in the process). You need to dereference the pointers to copy the actual data (eg: *newframe = *Ethfrm).

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    so does that mean
    *newframe = *Ethfrm
    will newframe and Ethfrm structs be seperated so i can adjust newframe without affecting Ethfrm.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by kiros88 View Post
    so does that mean
    *newframe = *Ethfrm
    will newframe and Ethfrm structs be seperated so i can adjust newframe without affecting Ethfrm.
    yes this assignment copies each membr of Ethfrm to newframe.

    modification made to newframe afterwards will not affect Ethfrm
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    Okay so I'm going to ask a new question but its related. *new frame and *Ethframe im assuming is different data like the whole new struct different data in memory. Which I'm not sure is right.

    But if i did
    Code:
    while(1){
    Ethfrm_t * newframe;
    newframe = malloc(sizeof(*newframe));
    newframe = Ethfrm;
    }
    Will this continiuosly make a ton of new frames with new memory or does it just redeclare the same memory space that newframe was the first time it was malloc.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    >> Will this continiuosly make a ton of new frames with new memory or does it just redeclare the same memory space that newframe was the first time it was malloc.
    It makes new frames with new memory each time. Each call to malloc() should eventually be paired with a call to free() to ensure there are no memory leaks.
    bit∙hub [bit-huhb] n. A source and destination for information.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    i tried this using int. it didnt work. why is that?
    Code:
    main(){
        int num1=9, num2, *ptr1, *ptr2;
        
        ptr1 = num1;
        ptr2 = num2;
        
        *ptr2 = *ptr1;
        printf("ptr1 is %d.\nptr2 is %d", ptr1, ptr2);
        system("pause");
    }

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by eleven View Post
    i tried this using int. it didnt work. why is that?
    Code:
    main(){
        int num1=9, num2, *ptr1, *ptr2;
        
        ptr1 = num1;
        ptr2 = num2;
        
        *ptr2 = *ptr1;
        printf("ptr1 is %d.\nptr2 is %d", ptr1, ptr2);
        system("pause");
    }
    Because you can't assign a pointer to an integer. A pointer must point to an address.

    Code:
        ptr1 = &num1;
        ptr2 = &num2;
    Also, you probably want to print what the pointers point to, not the pointer addresses themselves.
    bit∙hub [bit-huhb] n. A source and destination for information.

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    Code:
    main(){
        int num1=9, num2, *ptr1, *ptr2;
        
        ptr1 = &num1;
        ptr2 = &num2;
        
        *ptr2 = *ptr1;
        printf("ptr1 is %d.\nptr2 is %d", *ptr1, *ptr2);
        system("pause");
    }
    worked! thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21