Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your pointer arithmetic is faulty. Adding 1 to a pointer does not advance it by one byte, but to the next element; by the size of the pointed type.</p> <p>If you have</p> <pre><code>struct foo { ... }; struct bar { ... }; struct baz { ... }; struct header { ... struct foo *foo; struct bar *bar; struct baz *baz; ... }; </code></pre> <p>you can allocate the structures using a single memory block using</p> <pre><code>struct header *ptr; ptr = malloc (sizeof (struct header) + sizeof (struct foo) + sizeof (struct bar) + sizeof (struct baz)); if (!ptr) { /* out of memory */ exit(1); } ptr-&gt;foo = (struct foo *)(ptr + 1); ptr-&gt;bar = (struct bar *)(ptr-&gt;foo + 1); ptr-&gt;baz = (struct baz *)(ptr-&gt;bar + 1); </code></pre> <p>Alternatively, if you wish to work with byte offsets (for example, if the sizes of the structures are dynamic -- quite legal in C99), you can use</p> <pre><code>struct header *ptr; char *tmp; tmp = malloc (sizeof (struct header) + sizeof (struct foo) + sizeof (struct bar) + sizeof (struct baz)); if (!tmp) { /* out of memory */ exit(1); } ptr = (struct header *)tmp; ptr-&gt;foo = (struct foo *)(tmp + sizeof (struct header)); ptr-&gt;bar = (struct bar *)(tmp + sizeof (struct header) + sizeof (struct foo)); ptr-&gt;baz = (struct baz *)(tmp + sizeof (struct header) + sizeof (struct foo) + sizeof (struct bar)); </code></pre> <p>In both cases the pointers in the <code>struct header</code> structure will get the same values relative to the pointer of the <code>struct header</code> itself. In the latter case, you must also remember to align the new pointers per ABI requirements, if you use arbitrary structure sizes.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload