Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a certain memory address is available for use in c++?

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!!

like image 439
Daniel Rotnemer Avatar asked Jul 04 '19 09:07

Daniel Rotnemer


People also ask

How do you look at a memory location in C?

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);

How do you access the memory address of a variable?

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.

How do I know if a pointer is accessible?

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).

Where are addresses stored in memory C?

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.

What data type is a memory address in C?

The data type of a memory address is a pointer, which is denoted by the type that it points to, followed by an asterisk ( * ).


1 Answers

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).

like image 171
Michael Chourdakis Avatar answered Sep 22 '22 06:09

Michael Chourdakis