How many bytes in a memory address?

I need to memorize many times and in different locations a 4 bytes value. I was thinking of memorizing the address of that value using a pointer, how much memory will the address take?

You can use the sizeof function to determine that answer for yourself.

Serial.print("int size: ");
Serial.println(sizeof(int));

Serial.print("int * size: ");
Serial.println(sizeof(int *));

Change int to byte, float, etc. to see the various type sizes.

That is an interesting question. I would have said that a C pointer would be be an unsigned integer in size, so two bytes?

However the newer Arduino Mega boards use a Mega1280 chip that has 128KB of program memory, so I’m not sure how the GCC compiler handles memory pointers for larger then 64KB of program memory. Anyone know?

Lefty

A bit of research shows how little I know and how quickly I can be confused!

I built a little program for the Arduino Mega and dumped the assembly. Code and data pointers all appear to be 16 bit. It appears 16 bit pointers are used for small programs built for the Mega.

The Wikipedia articles claim that code pointers are 24 bits for the Mega processor. There is an implication that data pointers are still 16 bit.

The Atmel documentation describes several new registers that are used to extend addresses past 16 bits. The new registers appear to provide access to a “window” of memory. Some of the instructions (like CALL) appear to work on word boundaries so they may be able to access the full 128K without a “window” register. Even on the Mega processor, the instruction set appears to favor the first 64K (or a 64K window; I can’t tell for certain).

Ugh! Right now I’m VERY grateful to have a C++ compiler that hides those details!

I too read a little of the datasheet for the mega1280. Seems that program memory addresses point to 16 bit words, as all AVR machine code instructions are either 16 or 32 bits wide.

So the program memory specification of 128KB is the same as 64K, 16 bit words, so a 16 bit address pointer (unsigned int) should cover all the flash memory of the mega1280. The next size larger Mega chip probably uses the extended address bit(s)? you mentioned to cover the larger program memory space they have.

So I guess we have come to the same conclusion, 16 bit pointers work fine for the mega1280 chip. Now I wonder if that’s actually correct. ;D

Lefty

Now I wonder if that’s actually correct

Disassembly addresses are always even. As you said, machine instructions are always two or four bytes. From the Atmel documentation: “The Program memory is organized in 16-bit words”. It all comes out even addresses. I think you’re right.

That’s rather clever. I’m starting to like those Atmel processors. :slight_smile:

Aha, so you are saying one should not think of 128k Bytes but 64k Words? This is interesting. I think this knowledge is helpful :slight_smile:

Aha, so you are saying one should not think of 128k Bytes but 64k Words? This is interesting. I think this knowledge is helpful

Yes for flash memory. Now data memory (SRAM) is byte based as it’s separate memory and char type variables are just 8 bits wide. It’s a function of the Harvard architecture (separate program and data memories) and the more standard Von neumann architecture where instructions and data share the same physical memory.

Lefty