简体   繁体   English

C-如何找到结构的大小?

[英]C - How to find the size of a structure?

In c, say you have the following structure and an instance of that: 在c中,假设您具有以下结构以及该结构的一个实例:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    typedef struct _a {
        int *a1;
        float *a2;
        char *a3;
    }a;

    a b;
    b.a1 = (int *) malloc(sizeof(int) * 10);
    b.a2 = (float *) malloc(sizeof(float) * 10);
    b.a3 = (char *) malloc(sizeof(char) * 10);

    return 0;
}

Now, how to find the total memory that is allocated/associated with the variable b? 现在,如何找到与变量b分配/关联的总内存? sizeof(b) will just return the combined size of the pointers in the structure but will not calculate the sum of the memories that has been allocated using malloc for different types/sizes. sizeof(b)只会返回结构中指针的组合大小,但不会计算使用malloc为不同类型/大小分配的内存总和。 How to find the total size of this structure in the memory (including padding if applicable)? 如何在内存中找到此结构的总大小(包括填充,如果适用)?

There is no standard way. 没有标准方法。

If you want to know how much memory is stored in each of the arrays being pointed to, you will need to explicitly record that information in your struct, eg: 如果您想知道所指向的每个数组中存储了多少内存,则需要在该结构中显式记录该信息,例如:

typedef struct _a {
    int *a1;
    float *a2;
    char *a3;
    size_t num_elements_a1;
    size_t num_elements_a2;
    size_t num_elements_a3;
}a;

You'll have to keep track of the aggregate size of your structure yourself. 您必须自己跟踪结构的总大小。

The C infrastructure doesn't know what memory is allocated to which pointers. C基础结构不知道将哪些内存分配给了哪些指针。 Even creating a strong, generalized concept of "ownership" of memory by a pointer is exceedingly difficult. 甚至通过指针创建一个强大的,普遍的“内存所有权”概念也非常困难。

To illustrate, realize that the elements of your structure could contain other pointers that reference other memory. 为了说明,请意识到结构的元素可以包含引用其他内存的其他指针。 Some of those references could be in loops (circularly linked list, undirected graphs). 其中一些引用可能处于循环中(循环链表,无向图)。

In that case, counting the memory could become impossibly difficult. 在这种情况下,对内存进行计数可能会变得非常困难。 If two different elements point to the same memory, should it be counted twice, or once? 如果两个不同的元素指向同一个内存,应该算两次还是一次? What system would keep track of which memory is counted or not counted? 哪个系统可以跟踪计数或不计数的内存? How would that system fit into C's minimalist paradigm? 该系统如何适应C的极简主义范式?

To do what you're asking, I think you need a language with reflection/introspection, such as Java or .Net (C#). 为了满足您的要求,我认为您需要一种具有反射/自省性的语言,例如Java或.Net(C#)。

sizeof() will take into account the necessary padding. sizeof()将考虑必要的填充。 It is not, however, a "deep" sizeof , meaning that it won't follow pointers/references and will not include the sizes of the pointed-to objects, including the a1 ... a3 arrays in your example. 但是,它不是“ deep”的sizeof ,这意味着它将不遵循指针/引用,并且将不包括指向对象的大小,在您的示例中包括a1 ... a3数组。

If that's what you're looking for, you'll have to compute that yourself. 如果这是您要查找的内容,则必须自己计算。 The easiest way to do that is by keeping track of the number of elements allocated for each array, and using that in the size calculations. 最简单的方法是跟踪为每个数组分配的元素数,并在大小计算中使用它。

sizeof() will return the size of the structure, including padding, however to calculate the total size including allocated memory, you'll have to write your own function. sizeof()将返回结构的大小,包括填充,但是要计算包括分配的内存在内的总大小,您必须编写自己的函数。 To do that, you'll also need to record how many of each data type you've allocated as there's no way to tell this after allocation has been done. 为此,您还需要记录已分配的每种数据类型中有多少种,因为分配完成后无法告知这一点。

A very very NON standard way of doing this is to work out how you systems malloc works. 一种非常非标准的方法是确定系统malloc的工作方式。 This does work on most linux'es 这确实适用于大多数Linux

You take the pointer malloc gave you and move back sizeof(ptr) - the value stored here is usually the size of the memory block reserved in the malloc call. 您将malloc给您的指针移回sizeof(ptr)-此处存储的值通常是malloc调用中保留的内存块的大小。

Doing this voids all warranties though and I would only use it for debugging. 这样做会使所有担保作废,而我只会将其用于调试。 Better to store the value when you malloc the memory though. 最好在您分配内存时存储该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM