Thread: get size allocated memory for ARRAY structs

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    11

    Post get size allocated memory for ARRAY structs

    my simple source code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int
    main()
    {
     int data_int_1;
    
     int my_array_int_1[100];
    
     struct my_struct_type_1
     {
      int data_int_1;
     } my_struct_data_1;
    
     struct my_struct_type_2
     {
      int data_int_1;
      int data_int_2;
     } my_struct_data_2;
    
     struct my_struct_type_3
     {
      int data_int_1;
      int data_int_2;
      int my_array_int_2[100];
     } my_struct_data_3;
    
     struct my_struct_type_3 *hundred_array_of_my_struct_type_3;
     hundred_array_of_my_struct_type_3 = malloc(sizeof(my_struct_data_3) * 10);
    
     /* is work fine: */
     /*
     hundred_array_of_my_struct_type_3[0] = my_struct_data_3;
     hundred_array_of_my_struct_type_3[1] = my_struct_data_3;
     hundred_array_of_my_struct_type_3[2] = my_struct_data_3;
     hundred_array_of_my_struct_type_3[3] = my_struct_data_3;
     hundred_array_of_my_struct_type_3[4] = my_struct_data_3;
     hundred_array_of_my_struct_type_3[5] = my_struct_data_3;
     hundred_array_of_my_struct_type_3[6] = my_struct_data_3;
     hundred_array_of_my_struct_type_3[7] = my_struct_data_3;
     hundred_array_of_my_struct_type_3[8] = my_struct_data_3;
     hundred_array_of_my_struct_type_3[9] = my_struct_data_3;
     */
    
     printf("sizeof(data_int_1):\t\t%i\n",sizeof(data_int_1));
     printf("sizeof(my_array_int_1):\t\t%i\n",sizeof(my_array_int_1));
     printf("sizeof(my_struct_data_1):\t%i\n",sizeof(my_struct_data_1));
     printf("sizeof(my_struct_data_2):\t%i\n",sizeof(my_struct_data_2));
     printf("sizeof(my_struct_data_3):\t%i\n",sizeof(my_struct_data_3));
     printf("sizeof(hundred_array_of_my_struct_type_3):\t%i\n\n",sizeof(*hundred_array_of_my_struct_type_3));
    
     free(hundred_array_of_my_struct_type_3);
     return 0;
    }

    In terminal I get this messages:
    Code:
    grytskiv@ZXDSL831II:~/memory$ gcc -ansi sizeof_struct.c -o sizeof_struct && ./sizeof_struct
    sizeof(data_int_1):             4
    sizeof(my_array_int_1):         400
    sizeof(my_struct_data_1):       4
    sizeof(my_struct_data_2):       8
    sizeof(my_struct_data_3):       408
    sizeof(hundred_array_of_my_struct_type_3):      408
    
    grytskiv@ZXDSL831II:~/memory$

    - all work successful, but how I can get size (sizeof) array. In my code I have array "hundred_array_of_my_struct_type_3". I allocated memory for this array, but can't get sizeof all array....
    (my array is ten my own struct types)

    HOW GET SIZE ARRAY IN BYTES???

    I can do this "sizeof(my_array) * 10", but where I can get count elements in array?

    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If the array was not declared in the same scope you want to use sizeof, then you can't do it. It's your job to keep track of your sizes, and if a function needs to know it, pass that size along.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-25-2010, 04:04 AM
  2. Problem with custom dynamic memory allocation routines
    By BLauritson in forum C++ Programming
    Replies: 12
    Last Post: 03-11-2010, 07:26 AM
  3. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  4. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM

Tags for this Thread