🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

data storage

Started by
5 comments, last by INI_FILE 4 years, 3 months ago

when data is stored in RAM in a c program is it stored without enclosing data type tags? (i.e. as opposed to how it would be stored in a html or xml document )

like if I have

struct human 
{
 int hand;
 int foot;
 int ear;
};
human Friends[3];

in memory that will end up as

hand, foot, ear, hand, foot, ear,hand, foot, ear rather then

human, hand, foot, ear,human, hand, foot, ear etc.

My project`s facebook page is “DreamLand Page”

Advertisement

Calin said:
hand, foot, ear, hand, foot, ear,hand, foot, ear

this.

If you want to understand deeply C language, you can create small programs, compile them and then disassembly them to see the code.

// It will probably look like this
&Friends[0] = hand
+ 4 bytes = foot
+ 4 bytes = ear

+ 4 bytes = hand (&Friends[1])
+ 4 bytes = foot
+ 4 bytes = ear

+ 4 bytes = hand (&Friends[2]
+ 4 bytes = foot
+ 4 bytes = ear

NikiTo said:

Calin said:
hand, foot, ear, hand, foot, ear,hand, foot, ear

this.

k

@ini_file what`s the &amp section for?

My project`s facebook page is “DreamLand Page”

The simple answer is that they are stored in order, the ints are 4 bytes each, the struct is 12 bytes long, and the array has them in the order mentioned without any extra data type.

But it does not need to be, and that isn't guaranteed everywhere.

The language is extremely open about what is allowed.

In theory it could store data type, nothing in the language forbids storing additional information.

The language leaves a lot of details up to implementations. A system might have padding, such as making sure everything is at 4-byte boundaries, or 16 byte, or some other size. The padding could have information inside it that the compiler uses, that is up to the compiler. The ordering of elements in memory also has some flexibility in the language standard, some things may be reordered, some things must be in the order the programmer specified.

The only way to be absolutely certain is to look for yourself, either at the output or the compiler documentation.

For your specific code, an int has a compiler specific size. On today's common compilers that size happens to be 32 bits, which also happens to be 4 bytes. On compilers I grew up with, the size was 16 bits or 2 bytes. On some compilers, the size could be 64 bits, or something else entirely. I have worked on machines with 9-bit bytes. I have worked on machines with 32-bit bytes.

For the structure which contains three ints, on today's compilers that size is probably 12 bytes long. But it could be some other size. The structure size could be 16 bytes long with a 4 byte padding. If the machine had different sizes of ints, such as an older 16-bit computer, the size would be different. If the machine were something other than x86 architecture it could also be something different. If the compiler was designed to have additional tracking information, it could add the information and still be valid within the language.

When asking questions about how things are stored internally, usually things are done a certain way, but also be aware that some systems, such as mainframes, microcontrollers, custom chips, and customized compilers, have the option to do things differently.

@Calin & was meant to be the & symbol. Dont know why the code block escaped it

This topic is closed to new replies.

Advertisement