How to declare variable with fixed address

Pages: 12
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.

Following Code is wrong, but may give an idea..
1
2
3
4
uint84_t *p=(uint64_t *)0xf4240;
*p=125;

cout<<"RESULT : "<<*(0xf4240)<<endl;


 
RESULT : 125



I want something like that.. Any help? How to do that?
Simply output what p points to.

1
2
3
4
uint64_t *p = (uint64_t *)0xf4240;
*p=125;

cout <<"RESULT : " << *p << endl; 



Why do care at what address the variable is stored?
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.
Last edited on
You can make a pointer, and use anly the value-pointed-to it
@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 = new char[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.
Thank you for all your replies,

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?

mat1[125][200] , processor will first fetch 125th row and then 200th column


I doubt it very much. More likely it will simply read the memory location directly.

Edit: Changed so that Viliml can unwind.
Last edited on
I doubt it very much. More likely it will simply read the memory location directly;
mat1+(125*row_size)+(200*size_of_type)

I doubt THAT very much! It's equivalent to (mat[125])[200], which means *((*(mat+125))+200)
Also, the
*size_of_type
is wrong, since the compiler has acces to RTTI, and know how much to increment
Last edited on
closed account (3hM2Nwbp)
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.
I doubt it very much


I am also not confirmed about this. Some say it will buffer the whole row. But if its doing it in way you told. Every thing's fine.

But i want to know how to do what i asked in question.
Any reference?
is wrong, since the compiler has acces to RTTI, and know how much to increment


That's kind of the point, dude. It knows exactly where to look. It's not actual code and it's not meant to be. I'll change it for you.
Last edited on
Some say it will buffer the whole row.


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.
Last edited on
I know that, i tried it 100 times. But i don't know much about computer organization and architecture.

You might be true, because of following code and results.

1
2
3
4
5
6
7
8
9
	int i[5][5];
	for(int j=0;j<5;j++)
	{
		for(int k=0;k<5;k++)
			{
				cout<<&i[j][k]<<" ";
			}
	cout<<endl;
	}



0x7fff02c78a30 0x7fff02c78a34 0x7fff02c78a38 0x7fff02c78a3c 0x7fff02c78a40 
0x7fff02c78a44 0x7fff02c78a48 0x7fff02c78a4c 0x7fff02c78a50 0x7fff02c78a54 
0x7fff02c78a58 0x7fff02c78a5c 0x7fff02c78a60 0x7fff02c78a64 0x7fff02c78a68 
0x7fff02c78a6c 0x7fff02c78a70 0x7fff02c78a74 0x7fff02c78a78 0x7fff02c78a7c 
0x7fff02c78a80 0x7fff02c78a84 0x7fff02c78a88 0x7fff02c78a8c 0x7fff02c78a90 


But, Can you give me a reference i want to read.
Last edited on
Can you give me a reference i want to read.


About what? What exactly is it that you don't understand?
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.
can i declare array a[100000000+10000] of uint64_t ?

btw, Moschops, while declaring dynamic array .
1
2
3
4
5
6
7
8
9
10
11
	int **i=new int*[5];
	for(int j=0;j<5;j++) i[j]=new int[5];

	for(int j=0;j<5;j++)
	{
		for(int k=0;k<5;k++)
			{
				cout<<&i[j][k]<<" ";
			}
	cout<<endl;
	}



0x209a040 0x209a044 0x209a048 0x209a04c 0x209a050 
0x209a060 0x209a064 0x209a068 0x209a06c 0x209a070 
0x209a080 0x209a084 0x209a088 0x209a08c 0x209a090 
0x209a0a0 0x209a0a4 0x209a0a8 0x209a0ac 0x209a0b0 
0x209a0c0 0x209a0c4 0x209a0c8 0x209a0cc 0x209a0d0


It fails?
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.
It fails?

What fails? Looks like it worked fine to me.
Pages: 12