1

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?

BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
matt
  • 2,312
  • 5
  • 34
  • 57
  • 8
    You'll have to define "empty". Until you can define that (which, hint, you can't), you can't make your program define it either. – Lightness Races in Orbit Jun 11 '15 at 15:29
  • 1
    The value at an address isn't "empty". It is [uninitialized until you allocate something at that address and write to it](https://stackoverflow.com/questions/4259885/why-do-i-see-strange-values-when-i-print-uninitialized-variables) (unless something else has already allocated memory there, then you're stomping on their memory) – Cory Kramer Jun 11 '15 at 15:32
  • Assuming that you mean "empty" as "entry where you have never written" - you may be able to play with the fact that for quite (most of,) a few Flash/EEPROM devices, all the never-written bytes are either zeros, or (more frequently) 0xFFs. Then you might be able to construct your data in the way that you never ever write 0x00000000 (or 0xFFFFFFFF) there, and use this value as an "empty" indicator. – No-Bugs Hare Jun 11 '15 at 15:45
  • @LightnessRacesinOrbit: actually, in the context of embedded Flash you can define "empty"; moreover, it is a standard practice there, see above. – No-Bugs Hare Jun 11 '15 at 15:51
  • @No-BugsHare: That is not an "empty" state. That is a documented initial state. Those are two _very_ different things. Furthermore, it's amusing that your suggestion is precisely the same as the suggestion Olivier made 15 minutes prior, that you did not like. – Lightness Races in Orbit Jun 11 '15 at 15:53
  • @LightnessRacesinOrbit: you've claimed that it is not possible even to *define* "empty". Now you're saying that you already know what "empty"really means (at least that you're sure that it is different from "documented initial state"). Please take one single consistent point of view at least for one single thread. – No-Bugs Hare Jun 11 '15 at 15:59
  • @No-BugsHare: I never said "it is not possible to define 'empty'". Not even once. I said the OP can't. There is also a big difference between "I know 'empty' is not that" and "I know what 'empty' _is_". Stop misrepresenting everyone's words, _please_; it's really annoying. – Lightness Races in Orbit Jun 11 '15 at 16:11

2 Answers2

3

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.

BufferOverflow
  • 543
  • 3
  • 12
1

If your array is an array of pointers you can check to see by comparing it to {nullptr}, otherwise, you cannot unless you initialize all the initial indexes to the same value, and then check if the value is still the same.

Olivier Poulin
  • 1,778
  • 8
  • 15
  • Huh? Unless buf is static/global, it is not initialized by default. – No-Bugs Hare Jun 11 '15 at 15:36
  • @No-BugsHare: He didn't claim it was. – Lightness Races in Orbit Jun 11 '15 at 15:36
  • @LightnessRacesinOrbit: come on, he explicitly said that if it is not pointers, you need to initialize (which strongly implies that if it is pointers - you don't need to initialize). – No-Bugs Hare Jun 11 '15 at 15:40
  • 1
    @No-BugsHare uhm...no. That;s not what i implied. My second statement is completely separated from the first. What i was saying is that if it is not a pointer, you need to initialize the values to something (like 0) to be able to check if they've changed. Was i not clear enough? – Olivier Poulin Jun 11 '15 at 15:42
  • @OlivierPoulin: it is not only unclear, it is misleading; it is missing description what else he needs to do so this comparison with nullptr works. – No-Bugs Hare Jun 11 '15 at 15:50
  • @No-BugsHare: Sorry, I just don't agree. This is a fine answer. He didn't "explicitly" or even implicitly say anything even remotely resembling what you claimed! – Lightness Races in Orbit Jun 11 '15 at 15:52