开发者

In C, How much memory my program waste?

开发者 https://www.devze.com 2023-03-08 00:17 出处:网络
I want to know, for example, I have a struct with three members...like: struct Somethings { int member1;

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? I means, will I have only a struct labeled 'thing' with theses values, or, will I have thi开发者_如何学JAVAs 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. 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.

Alternatively, within a function, this declares thing with automatic storage duration, which means that it lives only until the function returns. 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. (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). If the function is called recursively, then third, fourth, etc temporary copies of the data will be made.


Definition of struct Somethings is how you tell the compiler about a new type you created. struct Something thing is how you create an object of that type.

When the program runs, all the objects are created, and after their scope is exited, they are all destroyed. 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.

It should help if you look at the memory layout of a C program to understand the runtime foot print of a program.

And, the struct definition you have given in the question is not terminated properly.


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).

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:

   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. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消