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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Structure Value is not getting updated

Structure Value is not getting updated

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpdatabasequestion
10 Posts 8 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.
  • S Offline
    S Offline
    sakthii
    wrote on last edited by
    #1

    I created a form application File->New->Project->VC++->Windows Form Application. I created couple of .Cpp files and .h files. I have structure defined in .h file typedef struct _PHB_ENTRY { unsigned char name[30]; unsigned char add[50]; int checksum; int lenght; int reserverd1; int reserved2; } PHB_ENTRY; I have declared a variable in .cpp file, which is nothing but a 'C' code. PHB_ENTRY myEntry[50]; First i have init function, thought to initialize the struc with null, so i used memset memset(myEntry,NULL,50); Problem no #1: I cant see index 0 is intialized to NULL ! then i started initializing the struct strcpy((char*)myEntry[1].name,"STEFY"); Probmelem no #2: I can see STEFY being copied from myEntry[1].name[4], first for bytes are empty , that is myEntry[1].name[0]...myEntry[1].name[3] are empty!! i assigned other values. myEntry[1].checksum = 13; myEntry[2].length = 4; Problem no: #3: I see 13 is getting updated in myEntry[1].resevered1 variable and i cant see myEntry[2].length getting updated! can some one help me out on this strange problem?! Just a simple C code though!!!

    _ S R L D 5 Replies Last reply
    0
    • S sakthii

      I created a form application File->New->Project->VC++->Windows Form Application. I created couple of .Cpp files and .h files. I have structure defined in .h file typedef struct _PHB_ENTRY { unsigned char name[30]; unsigned char add[50]; int checksum; int lenght; int reserverd1; int reserved2; } PHB_ENTRY; I have declared a variable in .cpp file, which is nothing but a 'C' code. PHB_ENTRY myEntry[50]; First i have init function, thought to initialize the struc with null, so i used memset memset(myEntry,NULL,50); Problem no #1: I cant see index 0 is intialized to NULL ! then i started initializing the struct strcpy((char*)myEntry[1].name,"STEFY"); Probmelem no #2: I can see STEFY being copied from myEntry[1].name[4], first for bytes are empty , that is myEntry[1].name[0]...myEntry[1].name[3] are empty!! i assigned other values. myEntry[1].checksum = 13; myEntry[2].length = 4; Problem no: #3: I see 13 is getting updated in myEntry[1].resevered1 variable and i cant see myEntry[2].length getting updated! can some one help me out on this strange problem?! Just a simple C code though!!!

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      The memset is has to be done as memset(myEntry, 0, sizeof(PHB_ENTRY) * 50); Rest of your code looks ok. Probably something wrong in how you are validating it.

      «_Superman_» I love work. It gives me something to do between weekends.

      1 Reply Last reply
      0
      • S sakthii

        I created a form application File->New->Project->VC++->Windows Form Application. I created couple of .Cpp files and .h files. I have structure defined in .h file typedef struct _PHB_ENTRY { unsigned char name[30]; unsigned char add[50]; int checksum; int lenght; int reserverd1; int reserved2; } PHB_ENTRY; I have declared a variable in .cpp file, which is nothing but a 'C' code. PHB_ENTRY myEntry[50]; First i have init function, thought to initialize the struc with null, so i used memset memset(myEntry,NULL,50); Problem no #1: I cant see index 0 is intialized to NULL ! then i started initializing the struct strcpy((char*)myEntry[1].name,"STEFY"); Probmelem no #2: I can see STEFY being copied from myEntry[1].name[4], first for bytes are empty , that is myEntry[1].name[0]...myEntry[1].name[3] are empty!! i assigned other values. myEntry[1].checksum = 13; myEntry[2].length = 4; Problem no: #3: I see 13 is getting updated in myEntry[1].resevered1 variable and i cant see myEntry[2].length getting updated! can some one help me out on this strange problem?! Just a simple C code though!!!

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        sakthii wrote:

        Problem no #1: I cant see index 0 is intialized to NULL !

        memset's third parameter is a byte count. Your array is 50 elements long, not 50 bytes. Try something like this:

        memset(myEntry, 0, sizeof(myEntry));

        Steve

        1 Reply Last reply
        0
        • S sakthii

          I created a form application File->New->Project->VC++->Windows Form Application. I created couple of .Cpp files and .h files. I have structure defined in .h file typedef struct _PHB_ENTRY { unsigned char name[30]; unsigned char add[50]; int checksum; int lenght; int reserverd1; int reserved2; } PHB_ENTRY; I have declared a variable in .cpp file, which is nothing but a 'C' code. PHB_ENTRY myEntry[50]; First i have init function, thought to initialize the struc with null, so i used memset memset(myEntry,NULL,50); Problem no #1: I cant see index 0 is intialized to NULL ! then i started initializing the struct strcpy((char*)myEntry[1].name,"STEFY"); Probmelem no #2: I can see STEFY being copied from myEntry[1].name[4], first for bytes are empty , that is myEntry[1].name[0]...myEntry[1].name[3] are empty!! i assigned other values. myEntry[1].checksum = 13; myEntry[2].length = 4; Problem no: #3: I see 13 is getting updated in myEntry[1].resevered1 variable and i cant see myEntry[2].length getting updated! can some one help me out on this strange problem?! Just a simple C code though!!!

          R Offline
          R Offline
          Roger Stoltz
          wrote on last edited by
          #4

          There seems to be two issues here:

          1. The initialization of the myEntry array.
          2. Assigning values to the members end up at erroneous locations.

          #1 has already been addressed by others and should be

          memset( myEntry, 0, sizeof( PHB_ENTRY ) * 50 );
          // or
          memset( myEntry, 0, sizeof( myEntry ) );

          The second one seems a little trickier, but for some reason the compiler seems to think that the members of the structure starts four bytes later. Have you possibly removed a four-byte member from the beginning of the structure? Try and rebuild the complete application as there seems to be a modification done but the object file has not been rebuilt and rebuilding usually fixes these kind of problems.

          "It's supposed to be hard, otherwise anybody could do it!" - selfquote
          "High speed never compensates for wrong direction!" - unknown

          1 Reply Last reply
          0
          • S sakthii

            I created a form application File->New->Project->VC++->Windows Form Application. I created couple of .Cpp files and .h files. I have structure defined in .h file typedef struct _PHB_ENTRY { unsigned char name[30]; unsigned char add[50]; int checksum; int lenght; int reserverd1; int reserved2; } PHB_ENTRY; I have declared a variable in .cpp file, which is nothing but a 'C' code. PHB_ENTRY myEntry[50]; First i have init function, thought to initialize the struc with null, so i used memset memset(myEntry,NULL,50); Problem no #1: I cant see index 0 is intialized to NULL ! then i started initializing the struct strcpy((char*)myEntry[1].name,"STEFY"); Probmelem no #2: I can see STEFY being copied from myEntry[1].name[4], first for bytes are empty , that is myEntry[1].name[0]...myEntry[1].name[3] are empty!! i assigned other values. myEntry[1].checksum = 13; myEntry[2].length = 4; Problem no: #3: I see 13 is getting updated in myEntry[1].resevered1 variable and i cant see myEntry[2].length getting updated! can some one help me out on this strange problem?! Just a simple C code though!!!

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Hi sakthii, In addition to what the other members said you can remove the call to memset completely. You can do this to initialize your array to zero:PHB_ENTRY myEntry[50] = {0};
            Best Wishes, -David Delaune

            1 Reply Last reply
            0
            • S sakthii

              I created a form application File->New->Project->VC++->Windows Form Application. I created couple of .Cpp files and .h files. I have structure defined in .h file typedef struct _PHB_ENTRY { unsigned char name[30]; unsigned char add[50]; int checksum; int lenght; int reserverd1; int reserved2; } PHB_ENTRY; I have declared a variable in .cpp file, which is nothing but a 'C' code. PHB_ENTRY myEntry[50]; First i have init function, thought to initialize the struc with null, so i used memset memset(myEntry,NULL,50); Problem no #1: I cant see index 0 is intialized to NULL ! then i started initializing the struct strcpy((char*)myEntry[1].name,"STEFY"); Probmelem no #2: I can see STEFY being copied from myEntry[1].name[4], first for bytes are empty , that is myEntry[1].name[0]...myEntry[1].name[3] are empty!! i assigned other values. myEntry[1].checksum = 13; myEntry[2].length = 4; Problem no: #3: I see 13 is getting updated in myEntry[1].resevered1 variable and i cant see myEntry[2].length getting updated! can some one help me out on this strange problem?! Just a simple C code though!!!

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              sakthii wrote:

              Problem no: #3: I see 13 is getting updated in myEntry[1].resevered1 variable...

              Even though you are not assigning anything to it?

              sakthii wrote:

              can some one help me out on this strange problem?!

              How are you verifying the values?

              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              S C 2 Replies Last reply
              0
              • D David Crow

                sakthii wrote:

                Problem no: #3: I see 13 is getting updated in myEntry[1].resevered1 variable...

                Even though you are not assigning anything to it?

                sakthii wrote:

                can some one help me out on this strange problem?!

                How are you verifying the values?

                "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                S Offline
                S Offline
                sakthii
                wrote on last edited by
                #7

                Thanks to all, memset problem is solved, i could see the structure is set with 0. sakthii wrote: Problem no: #3: I see 13 is getting updated in myEntry[1].resevered1 variable... Even though you are not assigning anything to it? No. it happens when I am assigning the value to other parameter like myEntry[1].length= 4 How are you verifying the values? verifying using step by step debugging, I am using Visual Studio 8.

                C 1 Reply Last reply
                0
                • S sakthii

                  Thanks to all, memset problem is solved, i could see the structure is set with 0. sakthii wrote: Problem no: #3: I see 13 is getting updated in myEntry[1].resevered1 variable... Even though you are not assigning anything to it? No. it happens when I am assigning the value to other parameter like myEntry[1].length= 4 How are you verifying the values? verifying using step by step debugging, I am using Visual Studio 8.

                  C Offline
                  C Offline
                  Cedric Moonen
                  wrote on last edited by
                  #8

                  sakthii wrote:

                  verifying using step by step debugging, I am using Visual Studio 8.

                  And how do you verify the values ? Are you looking at the variables directly (within the watch window for instance) or are you looking up at the raw memory because you know where your member are located in memory ?

                  Cédric Moonen Software developer
                  Charting control [v2.0] OpenGL game tutorial in C++

                  S 1 Reply Last reply
                  0
                  • D David Crow

                    sakthii wrote:

                    Problem no: #3: I see 13 is getting updated in myEntry[1].resevered1 variable...

                    Even though you are not assigning anything to it?

                    sakthii wrote:

                    can some one help me out on this strange problem?!

                    How are you verifying the values?

                    "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    C Offline
                    C Offline
                    chirag_chauhan
                    wrote on last edited by
                    #9

                    Problem#2 & 3 seems to be occur because of memory-overusage that leads memory corruption at some un-imagined places, I have observed this kind of behavior mostly with mobile technologies. I suggest you to use heap-allocation instead of stack-allocation or try to reduce the array size.

                    1 Reply Last reply
                    0
                    • C Cedric Moonen

                      sakthii wrote:

                      verifying using step by step debugging, I am using Visual Studio 8.

                      And how do you verify the values ? Are you looking at the variables directly (within the watch window for instance) or are you looking up at the raw memory because you know where your member are located in memory ?

                      Cédric Moonen Software developer
                      Charting control [v2.0] OpenGL game tutorial in C++

                      S Offline
                      S Offline
                      sakthii
                      wrote on last edited by
                      #10

                      I am using watch window. I created new project (windows console application) and added the file to it and same code is working!!! I think some problem with the Windows Forms Aplication. problem with IDE ?!

                      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