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. Question about malloc a struct

Question about malloc a struct

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphicshelp
6 Posts 3 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.
  • M Offline
    M Offline
    Millenia Nova
    wrote on last edited by
    #1

    The code below uses a variable's type as size in malloc(). And I printf the size of //line 1, b and c to find out their sizes malloc will allocation the space for them in heap. Question part 1: One person told me I should use // line a's size to malloc in // #####, while the other person said I should use //line c instead. They do have the same size in printf(). Which one is correct? Why? Question part 2: // line b and // line c both have the same type of variable pointers, but why do they have different sizes? struct Vector { double *data; size_t size; }; int main() { int a, b, d; int sz = 12; a = sizeof(struct Vector); // line a printf("%d\n", a); //a = 16 b = sizeof(struct Vector*); // line b printf("%d\n", b); //b = 8 c = sizeof(*retVal); // line c printf("%d\n\n", c); //c = 16 struct Vector *retVal = malloc (sizeof (struct Vector)); //##### retVal->data = malloc (sz * sizeof (double)); // Set size and return. retVal->size = sz; printf("retVal->size: %d\n", sz); return 0; } Thank you for your help. :)

    L J 2 Replies Last reply
    0
    • M Millenia Nova

      The code below uses a variable's type as size in malloc(). And I printf the size of //line 1, b and c to find out their sizes malloc will allocation the space for them in heap. Question part 1: One person told me I should use // line a's size to malloc in // #####, while the other person said I should use //line c instead. They do have the same size in printf(). Which one is correct? Why? Question part 2: // line b and // line c both have the same type of variable pointers, but why do they have different sizes? struct Vector { double *data; size_t size; }; int main() { int a, b, d; int sz = 12; a = sizeof(struct Vector); // line a printf("%d\n", a); //a = 16 b = sizeof(struct Vector*); // line b printf("%d\n", b); //b = 8 c = sizeof(*retVal); // line c printf("%d\n\n", c); //c = 16 struct Vector *retVal = malloc (sizeof (struct Vector)); //##### retVal->data = malloc (sz * sizeof (double)); // Set size and return. retVal->size = sz; printf("retVal->size: %d\n", sz); return 0; } Thank you for your help. :)

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

      Jochen provided a more complete answer.

      1 Reply Last reply
      0
      • M Millenia Nova

        The code below uses a variable's type as size in malloc(). And I printf the size of //line 1, b and c to find out their sizes malloc will allocation the space for them in heap. Question part 1: One person told me I should use // line a's size to malloc in // #####, while the other person said I should use //line c instead. They do have the same size in printf(). Which one is correct? Why? Question part 2: // line b and // line c both have the same type of variable pointers, but why do they have different sizes? struct Vector { double *data; size_t size; }; int main() { int a, b, d; int sz = 12; a = sizeof(struct Vector); // line a printf("%d\n", a); //a = 16 b = sizeof(struct Vector*); // line b printf("%d\n", b); //b = 8 c = sizeof(*retVal); // line c printf("%d\n\n", c); //c = 16 struct Vector *retVal = malloc (sizeof (struct Vector)); //##### retVal->data = malloc (sz * sizeof (double)); // Set size and return. retVal->size = sz; printf("retVal->size: %d\n", sz); return 0; } Thank you for your help. :)

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #3

        Both are correct. sizeof(struct Vector) expands to the size of the type while sizeof(*retVal) expands to the size of the type pointed to by the variable. The second method should be preferred because it will be always the correct size even when changing the variable type. Imagine that you have another structure with different size. When you now change the type of retVal to that structure, you must also change the type for the first sizeof() but not for the second.

        M 1 Reply Last reply
        0
        • J Jochen Arndt

          Both are correct. sizeof(struct Vector) expands to the size of the type while sizeof(*retVal) expands to the size of the type pointed to by the variable. The second method should be preferred because it will be always the correct size even when changing the variable type. Imagine that you have another structure with different size. When you now change the type of retVal to that structure, you must also change the type for the first sizeof() but not for the second.

          M Offline
          M Offline
          Millenia Nova
          wrote on last edited by
          #4

          Thanks for your response. Just to make sure I understand your answer, I listed .....I'm thinking the second malloc() used in my previous example can be replaced by the line below?

          sizeof(12 * sizeof(retVal->data));

          Thank you!

          J 1 Reply Last reply
          0
          • M Millenia Nova

            Thanks for your response. Just to make sure I understand your answer, I listed .....I'm thinking the second malloc() used in my previous example can be replaced by the line below?

            sizeof(12 * sizeof(retVal->data));

            Thank you!

            J Offline
            J Offline
            Jochen Arndt
            wrote on last edited by
            #5

            No, that is wrong. The second example from your initial post (sizeof(struct Vector*)) returns the size of a pointer which is 4 or 8 with 32-bit or 64-bit builds. The sizeof[^] operator expands to the size of the argument during compilation. If the argument is not a type but a variable, the type of the variable is returned. If the variable is a pointer, the pointer must be dereferenced using the * operator to get the size of the type instead the size of the pointer. Similar applies when the type is a pointer itself. But when passing a type followed by * like in your second example, it is a pointer to the type. In that case the type does not care because all pointers have the same size. Maybe you are irritated how the * operator is treated. With declarations it indicates a pointer. When accessing variables, it accesses the content of pointer variables.

            M 1 Reply Last reply
            0
            • J Jochen Arndt

              No, that is wrong. The second example from your initial post (sizeof(struct Vector*)) returns the size of a pointer which is 4 or 8 with 32-bit or 64-bit builds. The sizeof[^] operator expands to the size of the argument during compilation. If the argument is not a type but a variable, the type of the variable is returned. If the variable is a pointer, the pointer must be dereferenced using the * operator to get the size of the type instead the size of the pointer. Similar applies when the type is a pointer itself. But when passing a type followed by * like in your second example, it is a pointer to the type. In that case the type does not care because all pointers have the same size. Maybe you are irritated how the * operator is treated. With declarations it indicates a pointer. When accessing variables, it accesses the content of pointer variables.

              M Offline
              M Offline
              Millenia Nova
              wrote on last edited by
              #6

              Thank you for clarifying my confusion, Jochen!! :D I appreciate it.

              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