0
#include<stdio.h>
#include<string.h>
typedef struct
{
    float TotalSize;
    float AvailableSize;
}SdMemoryInfo;


void SdGetMemoryUsageRespCsccToGui(SdMemoryInfo *abcdSdMemoryInfo)
{
    printf("\nSize of(SdMemoryInfo): %d %d",sizeof(abcdSdMemoryInfo),sizeof(float)); //Size of abcdSdMemoryInfo
}

int main()
{
    SdGetMemoryUsageResp mSdGetMemoryUsageResp;
    SdMemoryInfo vSdMemoryInfo[2];
    vSdMemoryInfo[0].TotalSize = 1;
    vSdMemoryInfo[0].AvailableSize = 2;
    vSdMemoryInfo[1].TotalSize = 3;
    vSdMemoryInfo[1].AvailableSize = 4;


    printf("\nSize of(SdMemoryInfo): %d %d",sizeof(vSdMemoryInfo),sizeof(float)); //Size of vSdMemoryInfo

    SdGetMemoryUsageRespCsccToGui(vSdMemoryInfo);
}

Output:

Size of(SdMemoryInfo): 16 4
Size of(SdMemoryInfo): 8 4 

Why is the size of structure array vSdMemoryInfo and abcdSdMemoryInfo different?

Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79

3 Answers3

5

You pass a pointer to a structure to the function. And you get the size of the pointer and not the data it points to.

If you pass an array to a function, you always need to pass the number of elements in the array to the function as a separate argument.

With C++ there are other options though, like "size of array" templates. Or std::array (or optionally std::vector).

Community
  • 1
  • 1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Might also want to note that, if the size of (or number of elements in) the array is needed, that information needs to be passed separately. – Peter Dec 15 '16 at 08:45
0

You are getting size of pointer inside function instead of size of structure.To get actual size of structure you can change prototype of your function as follows and deference the pointer while calculating the size.

#include<stdio.h>
#include<string.h>
typedef struct
{
    float TotalSize;
    float AvailableSize;
}SdMemoryInfo;


void SdGetMemoryUsageRespCsccToGui(SdMemoryInfo (*abcdSdMemoryInfo)[2])
{
    printf("\nSize of(SdMemoryInfo): %d %d",sizeof(*abcdSdMemoryInfo),sizeof(float)); //Size of abcdSdMemoryInfo
}

int main()
{
    //SdGetMemoryUsageResp mSdGetMemoryUsageResp;
    SdMemoryInfo vSdMemoryInfo[2];
    vSdMemoryInfo[0].TotalSize = 1;
    vSdMemoryInfo[0].AvailableSize = 2;
    vSdMemoryInfo[1].TotalSize = 3;
    vSdMemoryInfo[1].AvailableSize = 4;


    printf("\nSize of(SdMemoryInfo): %d %d",sizeof(vSdMemoryInfo),sizeof(float)); //Size of vSdMemoryInfo

    SdGetMemoryUsageRespCsccToGui((SdMemoryInfo(*)[2])vSdMemoryInfo);
    printf("\nSize of(int *): %d %d",sizeof(int *),sizeof(float)); //Size of vSdMemoryInfo
}
DevMJ
  • 2,810
  • 1
  • 11
  • 16
0

Arrays decays to pointer. Since its type and dimension information is lost, it is immaterial whether it is an array of struct or any primitive data type.

Size of(SdMemoryInfo): 16 (= (sizeof(vSdMemoryInfo[0] * len) = 8*2)   4
Size of(SdMemoryInfo): 8  (= sizeof(pointer))                         4 

Note that sizeof(pointer) can be 4 bytes as well depending upon the system.

Community
  • 1
  • 1
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79