1

My issue is simple:

uint32_t memory::read_word1 (uint32_t address) {
  if(address>(maxWord)){
        return 0;
    }
    uint32_t temp = 10; 
    return temp; 


}

uint32_t memory::read_word2 (uint32_t address) {
      if(address>(maxWord)){
            return 0;
        }

        return mem[address];

    }

void memory::show_address (uint32_t address) {
    int temp=read_word(address);
    printf("%08jx\n", (uintmax_t)temp);
}

// Write a word of data to an address, mask contains 1s for bytes to be updated
void memory::write_word (uint32_t address, uint32_t data, uint32_t mask) {
    if(address>(maxWord)){
        int newMax = ceil(address/1024)*1024;
        for(int i=maxWord+1;i<=newMax;i++){
            mem.push_back(0);
        }
        maxWord=newMax;
    }

    data=data&mask;
    mem[address]=data&~mask;
    mem[address]=mem[address]|data;
}

read_word1 and 2 return the value to be printed through show_address. read_word2 is the function that we desire. However, the problem we find is that when we return from the vector using mem[address]; read_word1 will print 0000000a while read_word2 prints 00000010 when we set an element to 10 (via write_word).

To solve this I have tried using mem.at(address), type casting and even converting to string, all to no avail. The vector mem is defined as

   std::vector<uint32_t> mem=decltype(mem)(1024,0);

in the class header file.

It seems this is an underlying problem with the vector- what can be done?

EDIT:

Thanks everybody for your help. Feeling like the topic has been debunked a bit more. I couldn't get it working the way I thought I could, however if you pass a number in hex eg. 000000aa into write_address everything seems to work! All a bit strange, but thanks.

Dave
  • 454
  • 1
  • 7
  • 17
  • 2
    Maybe you should show the code that prints the number? – user253751 Mar 26 '17 at 23:42
  • How do you know whether it is printing in decimal or in hex? – user253751 Mar 26 '17 at 23:45
  • Thanks @immibis, I've added a bit more info – Dave Mar 26 '17 at 23:46
  • @immibis When using the vectors return for a value of 10, the output is 00000010. With the code in read_word un-commented we get 0000000a. This is the same of all numbers when using the return - we get the padding and decimal value – Dave Mar 26 '17 at 23:48
  • Are elements of mem initialised to anything? – Sergey Slepov Mar 26 '17 at 23:53
  • @SergeySlepov I'm assumimg `decltype(mem)(1024,0);` is initialising the first 1024 elements to 0, although could be wrong – Dave Mar 26 '17 at 23:55
  • Ah, yes, just noticed that. Then why are you getting `00000010` and not `00000000`? – Sergey Slepov Mar 26 '17 at 23:57
  • How about putting all these snippets into one source file, including initialization? (MCVE) – donjuedo Mar 26 '17 at 23:58
  • @Dave In fact, it would be great to have a `read_word1` and a `read_word2`, the first having the commented `10` and the second uncommented `10`. Each would be called before calling `show_address`. – donjuedo Mar 27 '17 at 00:00
  • @donjuedo Thanks, I've reordered the question to make things a bit more manageable - hopefully this provides more insight – Dave Mar 27 '17 at 00:13
  • @SergeySlepov You're right -I'm using a write function to set the vector, updated the question – Dave Mar 27 '17 at 00:14
  • @Dave Are you absolutely sure the value in the vector is 10 (decimal) and not 16 (decimal)? That seems to be the most likely explanation, that it is actually 16. – user253751 Mar 27 '17 at 00:21
  • @immibis 100% sure. I'd bet my firstborn, and maybe even the next one. – Dave Mar 27 '17 at 00:24
  • I'm using cin to get the input for my write function if that makes any difference. – Dave Mar 27 '17 at 00:27
  • Could you try and replicate the problem in a cleanroom environment such as http://cpp.sh/? – Sergey Slepov Mar 27 '17 at 00:36
  • What compiler are you using? I think the Microsoft one still doesn't support `j` in printf – M.M Mar 27 '17 at 01:08
  • @M.M Gcc version 4.9.2 – Dave Mar 27 '17 at 01:10
  • What build? There is a version of gcc for windows that passes on printf calls to the microsoft C library that doesn't support `j` – M.M Mar 27 '17 at 01:14
  • @m.m Not to sure on the build, is there a terminal function I can use to find it? – Dave Mar 27 '17 at 01:20
  • @SergeySlepov Couldn't recreate the error unfortunately, but found a workaround posted in edit, thanks guys – Dave Mar 27 '17 at 01:33
  • well, you could test out the printf in a separate program – M.M Mar 27 '17 at 01:39

0 Answers0