vernongrant / c-beginner-tips Goto Github PK
View Code? Open in Web Editor NEWA series of notes for C beginners
License: GNU General Public License v3.0
A series of notes for C beginners
License: GNU General Public License v3.0
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);
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.