Arduino Mega integr Value at memory location changes

#include <stdio.h>

void setup() {
  Serial.begin(115200);

  char b[100];

  int * t = 65042;

  *t = 12;

  sprintf(b, "%p: %d\n", t, *t);
  Serial.print(b);

  
  sprintf(b, "%p: %d\n", t, *t);
  Serial.print(b);

}

void loop() {
  // put your main code here, to run repeatedly:

}

Result :
0xfe12: 12

0xfe12: 27756

Did this on Arduino Mega using Arduino IDE 2.x
Is this memory corruption?

Where do you think that points?

what are you trying to do with that ?

At the end/ close to the memory, but i am trying to learn how this works, i chose it as a random number.

just trying out pointer to learn them

Did you consider that accessing RAM and program memory require different machine instructions on the Mega?

But should it matter, if it is pointing to the same location? why has the value changed?

I strongly recommend to spend some time with the ATmega2560 data sheet, if you wish to learn about memory addressing on the Mega2560.

Do not post in "Uncategorized", please read the sticky topics in Uncategorized - Arduino Forum.

Topic moved.

You created a pointer to a random memory location, which could (and appears to) have been in use by some other bit of code already.

And you are surprised that you got unpredictable results?

I did find the datasheet, but am not able to understand what is being told.

But from reading the passage about the XMEM, I understand that the pointer I am pointing to is in the upper sector[?] and that

"The External Memory Interface is designed to guaranty minimum address hold time after G is asserted low of th =5ns."

but what if I want to extend this? what should I do?

Oh, an X-Y problem.

Check the datasheet: is there actually any RAM at that location in the Arduino Mega?

What is it you are actually trying to accomplish?

Sorry will remember that!

But I specifically chose a a number towards the end, will it still get get affected by the program?

I am just trying to learn about pointers, since i am a beginner at C

There are lots of tutorials on line that provide specific examples.

Poking around in the complete darkness, as you are doing, is unlikely to give predictable or understandable results.

Ok Thank you, will follow them in the future

The program, the core, memory mapped I/O, mapped processor registers... the possibilities are endless.

And the lesson here is "don't assign values to pointers to random bits of memory and expect to get away with it".

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.