Daniel Rotnemer
Daniel Rotnemer

Reputation: 105

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

Upvotes: 5

Views: 9141

Answers (4)

Quimby
Quimby

Reputation: 19123

You can't, C++ memory model does not work like that.

The only valid pointers are those obtained by the '&' operator, those returned from 'new/malloc' and static arrays. There is no mechanism for checking if the memory address is (still) valid or whether the object has been destroyed or not existed there at all. So it is up to the programmer to manage the correctness of the pointers.

Because of the reasons above your program has undefined behavior.

if(pointer) only checks whether pointer==0, nothing more. Note that int n=5; int array[n]; is not valid C++ either. Not sure if you are using it, but if you do, don't.

Based on the comments, you want a heterogenous container. In that case use an array of unions or better std::array<std::variant<int,double,char, float...>> array;. Or std::vector if you need dynamic size.

C++ guarantees that arrays ([], malloc or new[]) are contiguous, but they only contain one type. In general, you cannot store float, double, int, char continuously together because of the alignment issues. The array above is continuous in terms of std::variant object, but its size will be at least the size of the largest type. So chars will not be packed together.

Upvotes: 3

Babajan
Babajan

Reputation: 382

want to test a continuous memory allocation for variables of a different types

You may use a structure and declare required variables as members for continuous memory allocation for different datatypes.

For example:

struct eg_struct
{
           unsigned char abc;
           unsigned int  xyz;
}

Note that if required you may need to pack the structure.

Here there is no need to check whether memory is free or not.

Upvotes: 1

Michael Chourdakis
Michael Chourdakis

Reputation: 11158

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

Upvotes: 4

Lehks
Lehks

Reputation: 3166

That is not how you allocate memory. You need to do it properly using new.

See here.

Upvotes: 1

Related Questions