-2

In the MPLAB XC8 Compiler User Guide, an example on page 162 (reproduced below) uses the extern keyword in conjunction with the @ specifier. Given that we are specifying the address ourselves, why is this needed? It's not going to be allocating any memory per se.

The only reason I can think of is maybe extern variables aren't zeroed at startup. But then, C variables generally contain garbage anyway until you explicitly assign to them. So...I dunno.

Maybe it has something to do with it being in a header file? To avoid multiple #include statements causing a "variable already declared" error of some sort?


If the pointer has to access objects in data memory, you need to define a different object to act as a dummy target. For example, if the checksum was to be calculated over 10 bytes starting at address 0x90 in data memory, the following code could be used.

const char * cp;
extern char inputData[10] @ 0x90;
cp = &inputData;
// cp is incremented over inputData and used to read values there

No memory is consumed by the extern declaration, and this can be mapped over the top of existing objects.

klutt
  • 30,332
  • 17
  • 55
  • 95
Sod Almighty
  • 1,768
  • 1
  • 16
  • 29
  • 1
    Please don't post pictures of code. Or pictures of text in general. It's not searchable. – klutt Dec 15 '19 at 20:35
  • I linked to the source for that purpose. It was intended to be an exact copy for quick context. If I'd copied and pasted the code, it would lack formatting. Would this be preferable to you? – Sod Almighty Dec 15 '19 at 22:11
  • The linked manual is vague about which declarations with the “@” initialize memory and which do not. It may be that only those with explicit initializers initialize memory, regardless of how the same declaration (with the “@” part removed) would behave in strictly conforming C. But I do not see this explicitly stated in the manual—I surmise it from examples and some indirect discussion. – Eric Postpischil Dec 16 '19 at 02:43
  • @SodAlmighty I assume my edit answers that question? :) – klutt Dec 16 '19 at 08:36

1 Answers1

0

It makes no difference. It's mainly a choice of being explicit vs. being implicit.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76