hello,
I wish to manually define an address of a variable and then assign a value to that variable and then access data of that variable through address.
Why do care at what address the variable is stored?
There are good reasons for being able to do this. It's common for hardware to place a value at a known location; usually a value indicating some interaction with the physical world, be it an output (for example, setting a voltage or an LED) or an input (an integrated sensor that dumps a value, representative of some real-world physical property, to a known memory location).
In this case, it looks like practice and just messing around for learning, rather than such an interaction, though.
@AbstractionAnon: That's not so easy. Did you try to compile it? You know you will get an error because you don't own that space of memory?
Now to trying to give a answer: I don't know how to do it.
Maybe you should take a look to Fixed Memory Base or something like that (Allocations are done by default randomly in memory, if you set a memory base address, allocations will be done from that address ongoing, so you will be able to use the memory allocated at that address, if you first new'ed enough memory space)
Or you can simply make a std::map<DWORD, DWORD> mapping a "Custom" memory pointer to a "Actual" memory pointer. Not suggesting this thing.
And remember that you cannot "own" all the memory from 0x0 to 0xFFFFFFFF - that is like doing void * Allocate = newchar[0xFFFFFFFF];
And that's too much memory to handle for a basical pc.
Not suggesting to use fixed-memory-address functionalities. They're not much safe from hackers attacks.
Basically i was doing a project to reduce matrix multiplication time.
So, while dealing with arrays of size 10,000 is consuming much time. As if i try to access mat1[125][200] , processor will first fetch 125th row and then 200th column, and in whole multiplication it will do this for total 1,000,000,000,000 times.
Using fixed memory address will lead processor to directly that value. Vectors are good idea. But this is much faster.
i will search for what EssGeEich told.
Back to the question,
Any other suggestions to do so? Any alternative way of above?
I don't know your exact use-case, but you might consider reading up on placement new. It sounds like you're running repeated multiplications, and by using placement new with a fixed buffer, you could avoid the cost of reallocation.
Here's a simpler case: array[17] becomes *(array+17). Via the magic of pointer arithmetic, the value needed is fetched straight from memory. Exapnd for multi-dimensional arrays.
Could just be me but personally when dealing with multi-dimensional arrays I tend to prefer using a single dimensional array accessed through something like someArray[(row * width) + col] assuming I'm not using a vector.
Could just be me but personally when dealing with multi-dimensional arrays I tend to prefer using a single dimensional array accessed through something like someArray[(row * width) + col] assuming I'm not using a vector.
Nice way. Also suggesting this to the OP. + suggest something like a Macro or a Templated Inline Function to Optimize it.
If you need to seriously optimize matrix multiplication, you'll probably need to do it on the GPU and weed out as many cache misses as possible.
Also, if you Google Matrix Multiplication Optimization you'll find a whole bunch of algorithms that you can use.