简体   繁体   中英

How can I know if the memory address I'm reading from is empty or not in C++?

So on an embedded system I'm reading and writing some integers in to the flash memory. I can read it with this function:

read(uint32_t *buffer, uint32_t num_words){
    uint32_t startAddress = FLASH_SECTOR_7;

    for(uint32_t i = 0; i < num_words; i++){
        buffer[i] = *(uint32_t *)(startAddress + (i*4));
    }
}

then

uint32_t buf[10];
read(buf,10);

How can I know if buff[5] is empty (has anything on it) or not?

Right now on the items that are empty I get something like this 165 '¥' or this 255 'ÿ'

Is there a way to find that out?

You need first to define "empty", since you are using uint32_t. A good ide is to use value 0xFFFFFFFF (4294967295 decimal) to be the empty value, but you need to be sure that this value isn't used to other things. Then you can test if if ( buf [ 5 ] == 0xFFFFFFFF ) .

But if your using the whole range of uint32_t, then there is no way to detect if it's empty.

Another way is to use structures, and define a empty bit.

struct uint31_t
{
    uint32_t empty : 0x01; // If set, then uint31_t.value is empty
    uint32_t value : 0x1F;
};

Then you can check if the empty bit is set, but the negative part is that you lose a whole bit.

如果您的数组是指针数组,则可以通过将其与{nullptr}进行比较来进行检查,否则,除非将所有初始索引都初始化为相同的值,然后检查该值是否仍然相同,否则您将无法查看。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM