79832328

Date: 2025-11-28 07:09:28
Score: 0.5
Natty:
Report link

Since the comment button doesn't work on this site, I have no choice but to respond to my own question. Let this be a comment on John Bollinger's answer. At the same time, I'll expand a bit on my concerns regarding the original question.

By default, global variables without initialization are assigned the value 0.

In my example, the value of all elements of the 'buffer' array is 0 as soon as we enter the main() function.

As for the startDMARead() function, its appearance, simplified to all accesses to 'buffer' via a pointer to it, is:

static unsigned char buffer[32];

int main(void) {
  startDMARead(buffer, sizeof(buffer));
  return 0;
}

void startDMARead(unsigned char *ptr, int size) {
  *(volatile unsigned int *)DMA_HW_ADDR = (unsigned int)ptr;
  *(volatile unsigned int *)DMA_HW_SIZE = size;
  ... // wait for end transfer
}

As is clear (including to the compiler), the value of the ptr pointer is written to some MMIO address.

During optimization, I see no reason to think anything is happening to the contents pointed to by ptr.

In C code, there are no modifications to 'buffer' due to any dereference of ptr.

Now, when returning from startDMARead(), the compiler knows that 'buffer' was not modified by the call to startDMARead().

This means that reading 'buffer', for example, buffer[0], can be omitted. Instead, the compiler can simply replace such access with the value 0 (the value the buffer was initialized with).

That's what I'm afraid of. And I want to tell the compiler explicitly: "Hey, you don't see the changes in C, but I have to tell you that this buffer was modified beyond your comprehension!"

Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Evgeny Ilyin