简体   繁体   English

在 C 中,我的程序浪费了多少 memory?

[英]In C, How much memory my program waste?

I want to know, for example, I have a struct with three members...like:我想知道,例如,我有一个包含三个成员的结构......比如:

struct Somethings { 
     int member1;
     int member2;
     char *member3;
} 

And I do this:我这样做:

  
struct Something thing = {1, 2, "I'm a sentence..."};
 

When I run my program, What I will have in memory?当我运行我的程序时,我将在 memory 中拥有什么? I means, will I have only a struct labeled 'thing' with theses values, or, will I have this struct and another copy of theses values in the code itself?我的意思是,我是否只有一个带有这些值的结构标记为“事物”,或者,我是否会在代码本身中有这个结构和这些值的另一个副本? I think that the compiler is smart enough to only have one copy of these values, in the struct, right?我认为编译器足够聪明,在结构中只有这些值的一个副本,对吧? Someone knows any command to see it?有人知道看到它的任何命令吗? I don't know debug very well...我不太懂调试...

Thanks.谢谢。

In practice, the answer to your question depends on exactly where you declare that line:实际上,您的问题的答案取决于您声明该行的确切位置:

struct Something thing = {1, 2, "I'm a sentence..."};

At file scope, this declares thing with static storage duration , which means that it lives from program startup to program termination.在文件 scope 中,声明了static storage duration thing ,这意味着它从程序启动到程序终止。 In this case, there is quite likely to be only one physical copy of the data, both in the compiled program on disk and in memory while the program executes.在这种情况下,很可能只有一个数据的物理副本,无论是在磁盘上的编译程序中还是在程序执行时的 memory 中。

Alternatively, within a function, this declares thing with automatic storage duration , which means that it lives only until the function returns.或者,在 function 中,这声明了具有自动存储持续时间thing ,这意味着它仅在 function 返回之前存在。 In this case, there is still likely to be only one physical copy of the data in the compiled program, but while the program is executing there will be a second, temporary copy created each time the declaration of thing is reached, and destroyed each time that function returns.在这种情况下,编译后的程序中仍然可能只有一个数据的物理副本,但是在程序执行时,每次到达thing的声明时都会创建第二个临时副本,并且每次都销毁function 返回。 (Note that this applies to the 1 , 2 and the pointer value stored in thing.member3 , but not the string "I'm a sentence..." itself - there is still only ever one copy of that). (请注意,这适用于12和存储在thing.member3中的指针值,但不适用于字符串"I'm a sentence..."本身 - 仍然只有一个副本)。 If the function is called recursively, then third, fourth, etc temporary copies of the data will be made.如果递归调用 function,则将制作数据的第三、第四等临时副本。

That statement will cause there to be somewhere in memory a sequence of bytes that looks something like this (assuming a big-endian processor and ignoring structure padding for simplicity's sake).该语句将导致 memory 中的某个地方出现一个看起来像这样的字节序列(假设一个大端处理器并为简单起见忽略结构填充)。

Location    Contents
   N          0x00    // highest byte of 1
   N+1        0x00   
   N+2        0x00
   N+3        0x01    // lowest byte of 1
   N+4        0x00    // highest byte of 2
   N+5        0x00
   N+6        0x00
   N+7        0x02    // loweest byte of 2
   N+8        0xLL    // Highest byte of pointer to pointing to location 0xLLMMNNPP
   N+9        0xMM    
   N+10       0xNN    
   N+11       0xPP    // Lowest byte of pointer pointing to 0xLLMMNNPP

And then at location 0xLLMMNNPP you will have the string data:然后在位置0xLLMMNNPP您将获得字符串数据:

   Location     Contents
   0xLLMMNNPP    0x49  // I
   0xLLMMNNPP+1  0x27  // '
   0xLLMMNNPP+2  0x6D  // m
   [and so on]
   0xLLMMNNPP+15 0x00  // the end-of-string terminator

In your program the structure doesn't really exist at all.在您的程序中,该结构根本不存在。 The structure only defines how the data is to be laid out in memory.该结构仅定义了数据在 memory 中的布局方式。 The compiler will remember that definition so it knows where to find the pieces of the structure given the start location of where structure data is sitting in memory.编译器将记住该定义,因此它知道在给定结构数据位于 memory 中的起始位置的情况下在哪里找到结构的片段。

Definition of struct Somethings is how you tell the compiler about a new type you created. struct Somethings的定义是您如何告诉编译器您创建的新类型。 struct Something thing is how you create an object of that type. struct Something thing是如何创建该类型的 object。

When the program runs, all the objects are created, and after their scope is exited, they are all destroyed.程序运行的时候,所有的对象都被创建了,它们的scope退出后,全部销毁。 For the objects you create using dynamic allocation, it is left to you, or the program exiting, to destroy the objects and release the memory held by them.对于您使用动态分配创建的对象,由您或程序退出来销毁对象并释放它们持有的 memory。

It should help if you look at the memory layout of a C program to understand the runtime foot print of a program.如果您查看 C 程序的 memory 布局以了解程序的运行时足迹,它应该会有所帮助。

And, the struct definition you have given in the question is not terminated properly.而且,您在问题中给出的结构定义没有正确终止。

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

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