What are the benefits of using struct?

Asked

Viewed 272 times

0

As that type variables struct are stored in memory, for example:

struct livro{
char autor[10];
float preco;
};

//como essas variaveis fica alocado na memória?
struct livro livro1;
strcpy(livro1.autor, "Deitel");
livro1.idade = 320.25;

Moreover, the concept of struct is very similar to object orientation, or is it my impression?

What happens inside memory when using struct?

  • Struct and class are essentially the same thing by just changing the standard accessibility level of members. Struct is public, class is private.

  • IS that? I suggest you see here also, there is a lot about the subject...

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

2

Every time you want to keep organized data, probably of heterogeneous types, grouped logically as one thing alone is useful. There are cases that even a given can be useful to do as well as an abstraction.

When you declare a struct is just mapping how this data is set up in memory and you will be able to access each individual data through the names of its members. It only exists in the code, during execution there is nothing to indicate that it is a struct, only has data, one after the other.

If you think this is similar to object orientation, it’s because you have no idea what object orientation is. But don’t worry, almost everyone programs like this, or thinks they program and also doesn’t know what it is. Gathering data into a single structure has never been object oriented, but actually many people think it is. And yet it uses object notation (noun, point, adjective), there it confuses even more.

In fact when it creates a struct is creating a model of how an object will be composed, as well as a int is an object composed of bits in a certain way that represents an integer and has a single member accessed directly to facilitate, ie object has always existed at all and people think object is something connected to a class, No, this is a universal concept independent of paradigm. I already answered about it in In programming, what is an object?.

The rest I’ve answered in How a "struct" is organized in memory?.

1

Answering your question in a more empirical way, we can turn to the utility objdump. Using your struct as an example and compiling with the "-g" flag to provide debug information, we obtain:

<1><2e2>: número abbrev: 8 (DW_TAG_structure_type)
    <2e3>   DW_AT_name        : (cadeia indirecta, desvio: 0xed): livro
    <2e7>   DW_AT_byte_size   : 16
    <2e8>   DW_AT_decl_file   : 1
    <2e9>   DW_AT_decl_line   : 4
    <2ea>   DW_AT_decl_column : 8
    <2eb>   DW_AT_sibling     : <0x30a>
 <2><2ef>: número abbrev: 9 (DW_TAG_member)
    <2f0>   DW_AT_name        : (cadeia indirecta, desvio: 0x18): autor
    <2f4>   DW_AT_decl_file   : 1
    <2f5>   DW_AT_decl_line   : 5
    <2f6>   DW_AT_decl_column : 6
    <2f7>   DW_AT_type        : <0x30a>
    <2fb>   DW_AT_data_member_location: 0
 <2><2fc>: número abbrev: 9 (DW_TAG_member)
    <2fd>   DW_AT_name        : (cadeia indirecta, desvio: 0xda): preco
    <301>   DW_AT_decl_file   : 1
    <302>   DW_AT_decl_line   : 6
    <303>   DW_AT_decl_column : 7
    <304>   DW_AT_type        : <0x31a>
    <308>   DW_AT_data_member_location: 12

The most relevant parameters above show the following information:

  1. Dw_tag_structure_type, Dw_tag_member and Dw_at_name evidence the types of information being processed, the same being the struct itself, its fields and names respectively.
  2. Dw_at_byte_size: Provides the number in bytes used by struct.
  3. Dw_at_data_member_location: Provides the position in bytes where the data of a field is located. Here is evidenced the phenomenon of "padding" (bytes inserted halfway) to align the fields.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.