Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Writing the actual addresses of a struct in memory???

Writing the actual addresses of a struct in memory???

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresperformancequestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    johnstonsk
    wrote on last edited by
    #1

    Let me see if I can explain this with shortness and clearity. I am having to hard code the layout of a struct in memory. Meaning that I have to hard code the actual memory location of each element in the struct. Lets just say for simplicity that I have to start my first element at 0x0000. The struct is layed out like this: struct data { double min[4]; double max[4]; char name[4]; int count; } In order to create the struct in memory I think I need to create the layout of a byte(short word) like so: typedef union u_do_word{ unsigned short word; struct do_word_bits{ unsigned pad : 8; unsigned bit7 :1; unsigned bit6 :1; unsigned bit5 :1; unsigned bit4 :1; unsigned bit3 :1; unsigned bit2 :1; unsigned bit1 :1; unsigned bit0 :1; }bits; } do_word_t; And create the pointer and variables for the elements I put the hex address in the name. start of the min array static do_word_t word_0x00; static unsigned short *vme_word_0x00; //1st double in the min array static do_word_t word_0x20; static unsigned short *vme_word_0x20; //2nd double in the min array static do_word_t word_0x40; static unsigned short *vme_word_0x40; //3rd double in the min array static do_word_t word_0x80; static unsigned short *vme_word_0x80; //4th double in the min array start of the max array static do_word_t word_0xA0; static unsigned short *vme_word_0xA0; //1st double in the max array static do_word_t word_0xC0; static unsigned short *vme_word_0xC0; //2nd double in the max array static do_word_t word_0xE0; static unsigned short *vme_word_0xE0; //3rd double in the max array static do_word_t word_0x100; static unsigned short *vme_word_100; //4th double in the max array start of the char array This is the part that I am not sure about. I believe that an unsigned short will give me the correct size for a double, but how should I do the char array. Is this correct??? static do_word_t word_0x120; static unsigned char *vme_word_0x120; //1st char in the name array static do_word_t word_0x**?????**; (?? 0x124 ??) static unsigned char *vme_word_0x?**????**; //2nd char in the name array static do_word_t word_0x**?????**; (?? 0x128 ??) static unsigned char *vme_word_0x**?????**; //3rd char in the name array static do_word_t word_0x**?????**; (?? 0x12C ??)

    J 1 Reply Last reply
    0
    • J johnstonsk

      Let me see if I can explain this with shortness and clearity. I am having to hard code the layout of a struct in memory. Meaning that I have to hard code the actual memory location of each element in the struct. Lets just say for simplicity that I have to start my first element at 0x0000. The struct is layed out like this: struct data { double min[4]; double max[4]; char name[4]; int count; } In order to create the struct in memory I think I need to create the layout of a byte(short word) like so: typedef union u_do_word{ unsigned short word; struct do_word_bits{ unsigned pad : 8; unsigned bit7 :1; unsigned bit6 :1; unsigned bit5 :1; unsigned bit4 :1; unsigned bit3 :1; unsigned bit2 :1; unsigned bit1 :1; unsigned bit0 :1; }bits; } do_word_t; And create the pointer and variables for the elements I put the hex address in the name. start of the min array static do_word_t word_0x00; static unsigned short *vme_word_0x00; //1st double in the min array static do_word_t word_0x20; static unsigned short *vme_word_0x20; //2nd double in the min array static do_word_t word_0x40; static unsigned short *vme_word_0x40; //3rd double in the min array static do_word_t word_0x80; static unsigned short *vme_word_0x80; //4th double in the min array start of the max array static do_word_t word_0xA0; static unsigned short *vme_word_0xA0; //1st double in the max array static do_word_t word_0xC0; static unsigned short *vme_word_0xC0; //2nd double in the max array static do_word_t word_0xE0; static unsigned short *vme_word_0xE0; //3rd double in the max array static do_word_t word_0x100; static unsigned short *vme_word_100; //4th double in the max array start of the char array This is the part that I am not sure about. I believe that an unsigned short will give me the correct size for a double, but how should I do the char array. Is this correct??? static do_word_t word_0x120; static unsigned char *vme_word_0x120; //1st char in the name array static do_word_t word_0x**?????**; (?? 0x124 ??) static unsigned char *vme_word_0x?**????**; //2nd char in the name array static do_word_t word_0x**?????**; (?? 0x128 ??) static unsigned char *vme_word_0x**?????**; //3rd char in the name array static do_word_t word_0x**?????**; (?? 0x12C ??)

      J Offline
      J Offline
      John M Drescher
      wrote on last edited by
      #2

      The size of a double is 8 bytes. Use the sizeof(double) to see that your self. I have no clue why you would want to do this. Also if you don't pack the data using #pragma pack(push,1) before your struct and #pragma pack(pop) after your struct you will get unexpected results. John

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups