Giter Club home page Giter Club logo

c-beginner-tips's Introduction

Vernon Grant

Software Developer πŸ‘¨πŸ»β€πŸ’», passionate about old school programming and tools. Proud South African πŸ‡ΏπŸ‡¦

Email | Blog

c-beginner-tips's People

Contributors

vernongrant avatar

Watchers

 avatar  avatar  avatar

Forkers

shanekdev

c-beginner-tips's Issues

structure padding

Not all structs align to words. struct alignment is actually tied to the largest element in the struct (up to word size). For example:

// alignment = 1
struct abc {
    uint8_t a;
    uint8_t b;
    uint8_t c;
};

// will compile just fine
_Static_assert(sizeof(struct abc) == 3, "unexpected size of struct abc");  

// alignment  = 2
struct def {
    uint8_t a;
    // will pad a byte here
    uint16_t b;
};

_Static_assert(sizeof(struct def) == 4, "unexpected size of struct def");  

// alignment  = 8 (assuming 8 byte pointer)
struct ghi {
    uint8_t a;
    // will pad a 7 bytes!
    void* b;
};

_Static_assert(sizeof(struct ghi) == 16, "unexpected size of struct ghi");  

By the way, if you are ever unsure, _Static_assert is your best friend (I believe you need C11 for it though). If that assert was false, it wouldn't compile.

Also, there are some other convenience macros/functions to take advantage of for these types of things. There is alignof, __alignof or __alignof__ depending on your compiler. So you could do alignof(struct abc) to have access to the alignment attributes. There is also offsetof in <stddef.h> that you can use to get the byte offset of individual struct members: int x = offsetof(struct def, b);.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.