#### Archived

This topic is now archived and is closed to further replies.

# pointer array building using integers as addresses

This topic is 5905 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello all. I''m attempting to do something that I have not seen documented and I would appreciate it if some would pass judgement on the feasibility of my task. What I want to do is take an integer, cast it as an int*, add an offset, dereference the result, and then set that as the address of another pointer. Let see what that looks like: //Here''s how I arrive at an integer value of an address void * a[10]; void * b[10]; cout << "a:" << a << endl; // 0x0065FDD0 cout << "b:" << b << endl; // 0x0065FDA8 a[5] = b; cout << "a[5]:" << a[5] << endl;// 0x0065FDA8 cout << "&a[5]:" << &a[5] << endl;// 0x0065FDE4 cout << "0xE4-0xD0" << 0xE4-0xD0 << endl; // 20 / 5 is 4 bytes per index int num = (int)a[5]; cout << "num:" << num << endl; // 6684072 cout << "(int)&b[0]" << (int)&b[0] << endl; // 6684072 //So 6684072 is the integer equivalent of &b[0] //Now let''s continue int z = 6684072; //Cast z as an int pointer cout << "(int*)z:" << (int*)z << endl; // 0x0065DA8 //Add 4 bytes cout << "(int*)z+4:" << (int*)z+4 << endl; // 0x0065DB8 //Find out what''s at the new address -> nothing of course cout<<"(*((int*)z+4)):"<<(*((int*)z+4))<

##### Share on other sites
quote:
Original post by Kirijitsu
//(void**)(*((int*)z+4)) = new void*[10];

Ignoring everything prior to this line, you can''t assign a value to an expression. What you''re doing in the line above is like this:
int i = 40;i + 4 = 6; // this isn''t algebra!

You''re left with two options: 1.) increment your variable z; 2.) assign the left hand expression to a temporary variable.

Having answered that, let''s consider the higher issue: it''s a complete waste of time. Don''t think that a single temporary variable gives you a performance hit; the first point of optimization should be your high-level algorithm, followed by very specific inner loops. Keep this saying with you: most programs spend 90% of the time running 10% of the code. Find that 10% and tune it, and your sitting pretty.

So I suggest you forget about this foolishness (I wont tell anyone, promise) and write your application. Next, take a profiler (one comes with MSVC, for example, and there''s always GNU gprof, VTune, etc) and determine where your application''s bottlenecks are, and tune those sections only.

Finally, consider how convoluted the statement I quoted is. What happens if someone else needs to maintain or modify your code, and just can''t figure it out (largely because it looks like an entry for the International Obfuscated C Code Contest?

I''ve said enough.

##### Share on other sites
in that case i think it would work because the left isn''t an expression (i think). it''s like incrementing the adress by 4 or something

##### Share on other sites
long i = get_an_address();(int *)(i+4) = get_a_value();   // legal(int *)i + 4 = get_a_value();   // error!

The problem is (int *)z + 4, which I think should be (int *)(z + 4). Parentheses have higher precedence than the addition operator, so the cast is done first.

##### Share on other sites
Oluseyi,
I agree, what I have in terms of readability is the precursor to programming gone horribly wrong, but I maintain my original goal of removing assignments. The reason for this is that I''ve previously made several functions that perform the same ''search'' task. The fastest one is entirely compressed to one line and is faster than the next by a factor of 4! This is all because I removed a for loop and the affiliated temporary variables. Responding to your 90% of the time 10% is run: yes, this is just within that 10%. My program is going to do two main things, search and build. My search algorithm is as fast as it can get. Now I want to be able to build linked arrays as fast as I can.

Kirijitsu

##### Share on other sites
You can try using an array notation.

(*((int*)z+4)) ought to be the same as ((int*)z)[4]

##### Share on other sites
quote:
Original post by Kirijitsu
Hello all. I'm attempting to do something that I have not seen documented and I would appreciate it if some would pass judgement on the feasibility of my task. What I want to do is take an integer, cast it as an int*, add an offset, dereference the result, and then set that as the address of another pointer. Let see what that looks like:

By the looks of your code what you want to do is: take an (int) base, cast it to a (void**), add a (void**) offset, and assign a (void*) to what it's pointing to.

Note: (int*) might not be the same size as a (void*) on every compiler and system (although it certainly will on every 32 compiler for an Intel-like system). Whenever you do pointer arithmetic, you want to make sure you're working with a pointer of the right size.

  *((void**) value + (void**) offset)) = (void*) &pointer;

If you cast value to an int*, it might not work on every system. If you cast it to a void*, it probably won't work on any system (the size of a derefenced void* object is usually treated as 1).

It would help if you explained precisely what you're trying to do with this particular code.

All your bases belong to us (I know. It's irony.)

Edit: Got it wrong.

Edited by - Mayrel on November 20, 2001 12:53:04 PM