hello to all,

i am learning c++ and while looking at some code, i came upon this: *(float*) and
*(DWORD*).
for example

#define adress_1 0x00AA8B20
*(float*) adress_1 = 5000000;

so can you explain what the *(float*) and *(DWORD*) do.

and i did try and search on google but didnt get very far

Dani AI

Generated

Short summary and practical caveats that build on and : that pattern is the low-level way to treat a numeric value as an address and perform a direct memory access. It is commonly used for memory-mapped I/O, but it is not "normal" application-level code and carries portability and safety hazards that aren't obvious from the one-line form.

Key pitfalls and standards notes: converting an integer to a pointer and then dereferencing it can be implementation-defined or undefined if the address is invalid, misaligned, or not actually an object of the accessed type. The C++ cast forms and aliasing rules are documented on cppreference for reinterpret_cast and aliasing; read those before relying on this technique: reinterpret_cast and . Also, accessing arbitrary physical addresses from user-mode will typically crash unless you map them into your process (see OS APIs such as mmap on Linux).

Safer, clearer approach (patterns to adopt): use a pointer-sized integer type (std::uintptr_t), an explicit reinterpret_cast, and volatile for device registers; wrap accesses in small helper functions to centralize casting and any necessary memory barriers. For example:

#include <cstdint>

inline volatile float &hw_reg(std::uintptr_t addr) {
    return *reinterpret_cast<volatile float*>(addr);
}

/* use: hw_reg(addr) = some_value; */

Platform notes and troubleshooting: on Windows DWORD is the Win32 typedef for a 32-bit unsigned value (see Microsoft docs). To avoid segfaults, confirm the address is mapped into the process (Linux: mmap, Windows: mapping APIs or a driver). Remember volatile prevents certain compiler optimizations for hardware access but does not provide atomicity or inter-thread/device synchronization — use proper barriers or atomics where needed.

If code crashes: check mapping/permissions, alignment, and whether the memory really holds the expected object type. When in doubt, implement a kernel-mode driver or use OS-provided interfaces rather than poking arbitrary addresses from user-space.

Recommended Answers

All 2 Replies

This casts 0x00AA8B20 to a pointer to float and dereferences it.
So it interprets 0x00AA8B20 as an address of a float value.
One would normally write this as such:
*reinterpret_cast<float*>(0x00AA8B20)=5000000;

DWORD is just a typedef to (u)int or (u)long.

That is very common when programming hardware in C and the addresses are known. They could be static I/O or similar.

address_1 is 0x00AA8B20,
(float*) address_1 would interpret the address as a pointer to a float and
* (float*) address_1 then means the variable stored at that address.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.