lundi 7 juin 2021

Getting a constant number from a pointer to an address in C++

I am working with some embedded code but have to run the unit tests in Linux on the desktop. In the embedded code I am using a fixed memory address but cannot use this same address/number in Linux.

So in the embedded code/device I have code in this form:

class Foo
{
    private:
        const static uint64_t ADDRESS_TO_USE = 0xdeadbeef;

I am trying to fix this using code of this form:

uint8_t bigLumpOfMemory[0x100000];

class MockFoo
{
    private:
        const static uint64_t ADDRESS_TO_USE = reinterpret_cast<const uint64_t>(&bigLumpOfMemory[0]);

But this fails as I am not able to use a cast in such an expression (I believe). Is there a canonical way round this?

Edit the compiler error is:

error: conversion from pointer type ‘uint8_t* {aka unsigned char*}’ to arithmetic type ‘uint64_t {aka long long unsigned int}’ in a constant-expression

Aucun commentaire:

Enregistrer un commentaire