I'm working on my hobby project in c++, and want to test a continuous memory allocation for variables of different types, (Like array with variables of different types). How can i check if a specific memory address is available for use?
More Details:
Let's say we've got the following code: we have an integer int_var
, (it doesn't matter in which memory address this variable seats), In order to allocate a variable of different type in the address right after the address of int_var
i need to check if that address is available and then use it. i tried the following code:
int int_var = 5;
float* flt_ptr = (float*)(&int_var + (sizeof(int_var) / sizeof(int)));
// check if flt_ptr is successfully allocated
if (flt_ptr) { // successfully allocated
// use that address
} else { // not successfully allocated
cout << "ERROR";
}
The problem is: When i run the program, sometimes flt_ptr
is successfully allocated and all right, and sometimes not - but when it is not successfully allocated it throws an exception that says "Read access violation ..." instead of printing "ERROR"
. Why is that? Maybe i missed something about checking if flt_ptr
is successfully allocated? Or did something wrong? If so, How do i check if flt_ptr
is successfully allocated before i use it?
Thanks!!
int *ptr=0xFE1DB124; *ptr; This again was confusing as the memory location was identified by the code given below: int var; printf("\nThe Address is %x",&var);
A memory location where data is stored is the address of that data. In C address of a variable can be obtained by prepending the character & to a variable name. Try the following program where a is a variable and &a is its address: To output address of a variable, %p format specifier is used.
The best bet if you must use raw pointers is to make sure that it is either a valid pointer or NULL. Then you can check if it is valid by checking if it is equal to NULL. But to answer your question, you can catch these kinds of things with structured exception handling (SEH).
When a variable is created in C, a memory address is assigned to the variable. The memory address is the location of where the variable is stored on the computer. When we assign a value to the variable, it is stored in this memory address.
The data type of a memory address is a pointer, which is denoted by the type that it points to, followed by an asterisk ( * ).
This memory model you are assuming was valid back in DOS, where, in real mode, the memory was a continuous stream of bytes.
Now that we have paging (either in x86 or in x64), this is not possible. Therefore, you can make no assumptions on the existance of memory "near" memory.
You have to allocate properly, which means using C++ shared_ptr/unique_ptr/STL. Or, new/malloc the old (bad) way.
If you want variables to be one near the other, allocate the whole memory at once (via a struct, for example).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With