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. structure questions

structure questions

Scheduled Pinned Locked Moved C / C++ / MFC
16 Posts 4 Posters 1 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.
  • D Offline
    D Offline
    Dennis L
    wrote on last edited by
    #1

    Hi every one! Is this a safe way to define key structure.If not then how'd it be safer Thanks! typedef struct _key { struct p1 { struct p2 { unsigned char a1[128]; unsigned char a2[8]; }; unsigned char b1[64]; unsigned char b2[64]; unsigned char b3[128]; unsigned char b4[64]; unsigned char b5[64]; unsigned char b6[64]; }; } KEY;

    M P R 3 Replies Last reply
    0
    • D Dennis L

      Hi every one! Is this a safe way to define key structure.If not then how'd it be safer Thanks! typedef struct _key { struct p1 { struct p2 { unsigned char a1[128]; unsigned char a2[8]; }; unsigned char b1[64]; unsigned char b2[64]; unsigned char b3[128]; unsigned char b4[64]; unsigned char b5[64]; unsigned char b6[64]; }; } KEY;

      M Offline
      M Offline
      Matthew Faithfull
      wrote on last edited by
      #2

      What do you mean safe? Type safe, thread safe, OO safe, exception safe or just proof against terrorist bombing?:~

      "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

      D 1 Reply Last reply
      0
      • D Dennis L

        Hi every one! Is this a safe way to define key structure.If not then how'd it be safer Thanks! typedef struct _key { struct p1 { struct p2 { unsigned char a1[128]; unsigned char a2[8]; }; unsigned char b1[64]; unsigned char b2[64]; unsigned char b3[128]; unsigned char b4[64]; unsigned char b5[64]; unsigned char b6[64]; }; } KEY;

        P Offline
        P Offline
        Perspx
        wrote on last edited by
        #3

        Do you mean is it safe to define a structure within a structure? Regards, --Perspx

        "The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript

        1 Reply Last reply
        0
        • M Matthew Faithfull

          What do you mean safe? Type safe, thread safe, OO safe, exception safe or just proof against terrorist bombing?:~

          "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

          D Offline
          D Offline
          Dennis L
          wrote on last edited by
          #4

          I mean safe by the fact that these arrays are big and many, or would it be a problem in memory ? Also is it Thread safe?. And is it OO safe because i will use it in a class Thanks!

          P M 2 Replies Last reply
          0
          • D Dennis L

            I mean safe by the fact that these arrays are big and many, or would it be a problem in memory ? Also is it Thread safe?. And is it OO safe because i will use it in a class Thanks!

            P Offline
            P Offline
            Perspx
            wrote on last edited by
            #5

            It wouldn't be a problem in memory, it will just use up quite a bit of it :) And being thread safe is only a problem if you have two or more threads trying to access the same bit of data at the same time. It is perfectly OO-safe unless it is a private/protected member and you are trying to access it from outside the class :) Hope this helps, --Perspx

            "The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript

            1 Reply Last reply
            0
            • D Dennis L

              I mean safe by the fact that these arrays are big and many, or would it be a problem in memory ? Also is it Thread safe?. And is it OO safe because i will use it in a class Thanks!

              M Offline
              M Offline
              Matthew Faithfull
              wrote on last edited by
              #6

              Dennis L wrote:

              safe by the fact that these arrays are big and many

              That will depend on what error checking you have active when you allocate the structure, rather than on the structure itself and how much memory is available of course.

              Dennis L wrote:

              Also is it Thread safe?.

              Not as presented. There's nothing inherent in this structure to prevent multiple contexts accessing it asynchronously.

              Dennis L wrote:

              And is it OO safe because i will use it in a class

              Not as presented. The data members should ideally all be private (data hiding), all access should be via accessor functions, default constructirs, copy constructors and possibly equivalence/comparison operators may need to be provided. Having said that it's a good start to get your fundamental data structures right for the job. All the rest can then be added and you know you're not wasting your time. :)

              "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

              D 2 Replies Last reply
              0
              • M Matthew Faithfull

                Dennis L wrote:

                safe by the fact that these arrays are big and many

                That will depend on what error checking you have active when you allocate the structure, rather than on the structure itself and how much memory is available of course.

                Dennis L wrote:

                Also is it Thread safe?.

                Not as presented. There's nothing inherent in this structure to prevent multiple contexts accessing it asynchronously.

                Dennis L wrote:

                And is it OO safe because i will use it in a class

                Not as presented. The data members should ideally all be private (data hiding), all access should be via accessor functions, default constructirs, copy constructors and possibly equivalence/comparison operators may need to be provided. Having said that it's a good start to get your fundamental data structures right for the job. All the rest can then be added and you know you're not wasting your time. :)

                "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

                D Offline
                D Offline
                Dennis L
                wrote on last edited by
                #7

                About the OO safe question i will use it like: class MYCLASS { ... ... public: KEY k; or KEY *k; ... };

                M 1 Reply Last reply
                0
                • M Matthew Faithfull

                  Dennis L wrote:

                  safe by the fact that these arrays are big and many

                  That will depend on what error checking you have active when you allocate the structure, rather than on the structure itself and how much memory is available of course.

                  Dennis L wrote:

                  Also is it Thread safe?.

                  Not as presented. There's nothing inherent in this structure to prevent multiple contexts accessing it asynchronously.

                  Dennis L wrote:

                  And is it OO safe because i will use it in a class

                  Not as presented. The data members should ideally all be private (data hiding), all access should be via accessor functions, default constructirs, copy constructors and possibly equivalence/comparison operators may need to be provided. Having said that it's a good start to get your fundamental data structures right for the job. All the rest can then be added and you know you're not wasting your time. :)

                  "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

                  D Offline
                  D Offline
                  Dennis L
                  wrote on last edited by
                  #8

                  What is the difference to use it as KEY k; and KEY *k; I have read some papers that say that there is speed difference or some kind of difference in memory. (if i remember well) Or what is the difference anyway?

                  M P 2 Replies Last reply
                  0
                  • D Dennis L

                    About the OO safe question i will use it like: class MYCLASS { ... ... public: KEY k; or KEY *k; ... };

                    M Offline
                    M Offline
                    Matthew Faithfull
                    wrote on last edited by
                    #9

                    Hmm, that's not very OO. Google up some terms like 'data hiding' and 'design by contract' to get ideas on 'better' ways.

                    "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

                    D 1 Reply Last reply
                    0
                    • D Dennis L

                      What is the difference to use it as KEY k; and KEY *k; I have read some papers that say that there is speed difference or some kind of difference in memory. (if i remember well) Or what is the difference anyway?

                      M Offline
                      M Offline
                      Matthew Faithfull
                      wrote on last edited by
                      #10

                      If you're unsure of the difference between KEY k; an instance of KEY called k and KEY* k; an instance, called k, of a pointer to a KEY, then you really shouldn't be attempting any serious development in C++. Seriously I'm not being rude you need to read some books, go on a course or whatever or things will get very painful very quickly. C++ is not like BASIC or even VB. You can't really learn it from scratch by trial and error, you have to start from a certain base in order to stand a chance. At the very least read some Web tutorials on pointers before diving in or both the contents of your PC and your sanity are at risk.

                      "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

                      D 1 Reply Last reply
                      0
                      • D Dennis L

                        What is the difference to use it as KEY k; and KEY *k; I have read some papers that say that there is speed difference or some kind of difference in memory. (if i remember well) Or what is the difference anyway?

                        P Offline
                        P Offline
                        Perspx
                        wrote on last edited by
                        #11

                        Putting a * there means that it is a pointer to a KEY structure, whereas no * means that it is actually a KEY structure and the required memory to hold all the KEY data is set aside for it. Regards, --Perspx

                        "The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript

                        1 Reply Last reply
                        0
                        • M Matthew Faithfull

                          Hmm, that's not very OO. Google up some terms like 'data hiding' and 'design by contract' to get ideas on 'better' ways.

                          "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

                          D Offline
                          D Offline
                          Dennis L
                          wrote on last edited by
                          #12

                          Sorry! If KEY is a private member? Or what if i do it like class MYCLASS { ... ... public: void Get(KEY *k); ... }; // in cpp void MYCLASS::Get(Key *k) { // Do operation: } int main() { MYCLASS g; KEY k; // key is empty g.Get(&k); //key is filled // Do operations with k return 0; }

                          M 1 Reply Last reply
                          0
                          • D Dennis L

                            Sorry! If KEY is a private member? Or what if i do it like class MYCLASS { ... ... public: void Get(KEY *k); ... }; // in cpp void MYCLASS::Get(Key *k) { // Do operation: } int main() { MYCLASS g; KEY k; // key is empty g.Get(&k); //key is filled // Do operations with k return 0; }

                            M Offline
                            M Offline
                            Matthew Faithfull
                            wrote on last edited by
                            #13

                            Yep, you can do it that way. It's not really the usual way to do things but you can do it that way if you wish. It would be more normal to write the functions that do operations with k as members of MYCLASS which use the private member k to do their work and return results to their callers e.g. Key validity.

                            "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

                            1 Reply Last reply
                            0
                            • M Matthew Faithfull

                              If you're unsure of the difference between KEY k; an instance of KEY called k and KEY* k; an instance, called k, of a pointer to a KEY, then you really shouldn't be attempting any serious development in C++. Seriously I'm not being rude you need to read some books, go on a course or whatever or things will get very painful very quickly. C++ is not like BASIC or even VB. You can't really learn it from scratch by trial and error, you have to start from a certain base in order to stand a chance. At the very least read some Web tutorials on pointers before diving in or both the contents of your PC and your sanity are at risk.

                              "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

                              D Offline
                              D Offline
                              Dennis L
                              wrote on last edited by
                              #14

                              Maybe i didn't ask right the question. I know the difference between KEY k; and KEY *k; but i really care that the fact *k is allocated in free memory has some advantages?

                              M 1 Reply Last reply
                              0
                              • D Dennis L

                                Maybe i didn't ask right the question. I know the difference between KEY k; and KEY *k; but i really care that the fact *k is allocated in free memory has some advantages?

                                M Offline
                                M Offline
                                Matthew Faithfull
                                wrote on last edited by
                                #15

                                OK but if you use a member instance class CMyClass { private: KEY k; }; then k is allocated wherever the instance of your class is allocated. You can use heap CMyClass* pMyInstance = new CMyClass(); or stack CMyClass MyInstance; If you're allocating really large amounts of data you definitely need the heap, possibly even look into the Virtual Memory API. In general its often best to leave the decision on where to allocate data up to the code that uses the data rather than trying to make it part of the data.

                                "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

                                1 Reply Last reply
                                0
                                • D Dennis L

                                  Hi every one! Is this a safe way to define key structure.If not then how'd it be safer Thanks! typedef struct _key { struct p1 { struct p2 { unsigned char a1[128]; unsigned char a2[8]; }; unsigned char b1[64]; unsigned char b2[64]; unsigned char b3[128]; unsigned char b4[64]; unsigned char b5[64]; unsigned char b6[64]; }; } KEY;

                                  R Offline
                                  R Offline
                                  roel_
                                  wrote on last edited by
                                  #16

                                  Watch out for memory alignment issues if you are going to use these keys (I'm assume they will be crypto keys) with raw memory access. You can change the way the object is laid out in memory with the #pragma pack directive. It may not be a problem though, depends on how you'll use your struct (Actually it would show not so great design if you'd need this, but there may be performance reasons).

                                  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