Close

pointer idea...

A project log for limited-code hacks/ideas

When you're running out of code-space... sometimes yah's gots to hack. Here are some ideas.

eric-hertzEric Hertz 12/30/2016 at 02:250 Comments

I have to add several values from several pointers...

e.g.

uint16_t *pA = NULL;
uint16_t *pB = NULL;
uint16_t *pC = NULL;

< a bunch of code that assigns pA, pB, and/or pC >

uint16_t value = *pA + *pB + *pC;

BUT any and/or all of these pointers may be unassigned... in which case, they should not be considered in the sum for value.

Of course, using NULL pointers makes sense, to indicate whether they've been assigned. But, as I understand the C standard, you're not supposed to *access* a NULL address... You're only supposed to *test* whether a pointer is NULL.

(e.g. address-zero may be a reset-vector, which probably contains a "jump" instruction, which most-likely is NOT zero, in value).

So, again, if I understand correctly, the "right" way to handle these potential NULL pointers would be something like:

uint16_t *pA = NULL;
uint16_t *pB = NULL;
uint16_t *pC = NULL;

< a bunch of code that assigns pA, pB, and/or pC >

uint16_t value = 0;
if(pA)
 value = *pA;
if(pB)
 value += *pB;
if(pC)
 value += *pC;
That's a lot of tests! Surely they'll add up in code-space...

Instead, what about:

uint16_t zeroWord = 0;
uint16_t *pA = &zeroWord;
uint16_t *pB = &zeroWord;
uint16_t *pC = &zeroWord;

< a bunch of code that assigns pA, pB, and/or pC >

uint16_t value = *pA + *pB + *pC;

Discussions