This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Design question about global variable

Hello

Normall in an application we define global variable as:
int a;
int b;
...

How about use one structure to include all variables, and just define one structure instance? as below:
struct{ int a; int b; ...
}global;

Is the performance slower? does it take more code space? Is there any other drawback?

Thanks.

/Wang

Parents
  • Yes, but did you pick up what the view was?

    XML is a solution where all data is sent together with metadata, so that a listener can pick out exactly what data that was sent - and what wasn't sent.

    A protocol that just packs n variables binary after each other have no such metadata. Much more efficient use of the link. But with significant issues if fiels have changed offsets inside the binary package.

    So everything is a trade-off based on what the major goals was.

    When talking transfers, XML is more robust but with less efficiency than packed binary data.
    When talking about using global variables, then packed structs may be more space efficient but at the cust of code size and runtime.

    I wouldn't recommend a solution that hurt code size and access speed when running the program, just to make it a bit simpler to get a magic block of data to send to the other side. Especially when there are still lots of issues to get the other side to be able to make use of that binary blob.

Reply
  • Yes, but did you pick up what the view was?

    XML is a solution where all data is sent together with metadata, so that a listener can pick out exactly what data that was sent - and what wasn't sent.

    A protocol that just packs n variables binary after each other have no such metadata. Much more efficient use of the link. But with significant issues if fiels have changed offsets inside the binary package.

    So everything is a trade-off based on what the major goals was.

    When talking transfers, XML is more robust but with less efficiency than packed binary data.
    When talking about using global variables, then packed structs may be more space efficient but at the cust of code size and runtime.

    I wouldn't recommend a solution that hurt code size and access speed when running the program, just to make it a bit simpler to get a magic block of data to send to the other side. Especially when there are still lots of issues to get the other side to be able to make use of that binary blob.

Children