|
|
|
|
|
by gilgad13
5360 days ago
|
|
In C, at least, it helps to think of regions of memory independently of any meaning overlaid on top. For instance, there is plenty of code that depends on behavior like this: struct list_node {
struct list_node * next;
struct list_node * prev;
}
struct useful {
/* This struct should be part of a list, so include the list header first */
struct list_node;
/* That data that this struct is storing follows */
in_addr_t sip;
in_port_t sport;
in_addr_t dip;
in_addr_t dport;
}
Then, you can write a generic list walker by casting the a "struct useful * " to a "struct list_node * " since the first fields will match up.This is just one example, another would be parsing and unwrapping ip headers and data sections. As I'm sure you've noticed, C prefers explicit to implicit, and this applies to the layout of memory as well. edit: example: http://stackoverflow.com/questions/3766229/casting-one-struc... |
|