0
\$\begingroup\$

I think that the following question is simple, but it is important for me to know.

I am working on microcontrollers. I have seen some code to access the data from the flash memory.

For example in order to read the contents of the 0xA010 address of the flash there is the following code:

           #define ADDRESS_TO_READ      0xA010

           uint8 *Read_Ptr = NULL;
           Read_Ptr = (uint8 *) ADDRESS_TO_READ;
           value = *Read_Ptr; 

This is working fine. My question here is, since Read_Ptr is uint8, how is it storing the value 0xA010, which is a short value (16-bit Address)? How the read and write functions will occur in this memory, when the pointer is uint8?

\$\endgroup\$
4
\$\begingroup\$

Since Read_Ptr is uint8 ...

Except that it's not. It's uint8*.

ptr_t, which is (should be) the type for pointers on an architecture, is however long the architecture deems it to be and is completely independent from the size of the value the compiler is looking at in that specific location.

\$\endgroup\$
1
\$\begingroup\$

The pointer itself will only contain the address. The type of pointer is the type of data you expect to find at that address. So a (uint8*) will give you uint8 data from the address you specified.

If we have an example like this where each address hold a byte of data

address 0x0000     0x00001     0x0010     0x0011     0x0010
data      0x00        0x21       0x34       0x55       0x69

uint8 *Read_Ptr = NULL;
ReadPptr = (uint8*)0x00000
value = *Read_Ptr

value = 0x00 because its loading in address 0x0000 and reading back uint8 data

But if we do this

uint16 *Read_Ptr = NULL;
ReadPptr = (uint16*)0x00000
value = *Read_Ptr

value =0x0021 because it's loading in address 0x0000, but its reading back uint16 data which would be the contents of 0x0000 and 0x0001

Another example would be

uint64 *Read_Ptr = NULL;
ReadPptr = (uint64*)0x00001
value = *Read_Ptr

value = 0x21345569

It's assumed that value is declared large enough to hold the size of the read data. Don't do a read on (uint64*) when your value variable is only uint8. It'll be truncated.

\$\endgroup\$
  • \$\begingroup\$ Can uint8* ReadPtr have 32/64 bit Address(not content in the Address) \$\endgroup\$ – rock_buddy Nov 26 '14 at 3:35
  • \$\begingroup\$ It depends on the CPU architecture. You can use sizeof(int *) to get the size of a pointer, or check your compiler manual. There might be a pre-#defined constant in one of the standard headers, too. \$\endgroup\$ – Adam Haun Nov 26 '14 at 6:26
0
\$\begingroup\$

a pointer is always big enough for an address.

\$\endgroup\$
  • \$\begingroup\$ Then what is the use of uint16* and uint32*. For all(any type of Address) we can use uint8* itself right? \$\endgroup\$ – rock_buddy Nov 25 '14 at 19:29
  • \$\begingroup\$ uint8* is a pointer to an 8-bit value, while uint16* is a pointer to a 16 bit value. The different pointer types may be handled differently in some situations - incrementing a uint8* will increment the pointer by 1, to point to the next byte, but incrementing a uint16* may increment it by two, so it points to the next 16-bit value. \$\endgroup\$ – Peter Bennett Nov 25 '14 at 19:38
  • \$\begingroup\$ If writing pointer is uint8*(which is pointing to Address 0x2000) and i am writing an Ascii character 'A' to address 0x2000 and Char 'D' to address 0x2001. And my reading pointer is uint16 and pointing to Address 0x2000. What Value it will return. Only 'A' or "AD" \$\endgroup\$ – rock_buddy Nov 25 '14 at 19:44
  • \$\begingroup\$ @rock_buddy: Either 0x4144 or 0x4441, depending on the endianness of the architecture. \$\endgroup\$ – Ignacio Vazquez-Abrams Nov 25 '14 at 19:51
  • 3
    \$\begingroup\$ This is a little short to be considered an answer. It would help if you edit in further details to expand it into a full answer. \$\endgroup\$ – David Nov 25 '14 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.