1

I have an array of 64 bit unsigned integers. Is it possible to assign some pointers of an array in some positions of it. I mean:

array [0] = Integer
array [1] = Integer
array [2] = Pointer to array
array [3] = Pointer to array
array [4] = Integer

Can anybody help me how to do that ?

1
  • It's very ugly practice (in C++ not in C) to interpret pointers as numbers (and vice versa). It's in general, but according to your example it's ugly ^ 2, because - how would you reinterpret them back to pointers ? Maybe the best way is to revise an architecture. Commented Jan 21, 2013 at 8:36

5 Answers 5

4

One way is to turn array into an array of

union {
  uint64_t   intval;
  uintptr_t  ptrval;
};

and work with that.

You will, of course, need some way to know whether a particular element stores an integer or a pointer.

Sign up to request clarification or add additional context in comments.

Comments

3

You can use reinterpret_cast:

array[3] = reinterpret_cast<std::uintptr_t>(pointer);

(uintptr_t is from <cstdint>)

Try to avoid doing this if possible though. Can you rethink your design?

8 Comments

Thanks but would "array[3] = reinterpret_cast<std::uintptr_t>(pointer);" it create an pointer to an array of specified type and length and how to access that ? Can you please give a simple code to illustrate above fact ?
@user1838343 you lose all type information when you do this, so you can't get its length. You just have to reinterpret_cast it back into the pointer... What are you trying to do with this?
I want to put integers into arrays. If positions have lots of values (an array of values) I want to create an pointer array and store values into it. Then I have to access those values accordingly.
I know I will loose information of the length. But, I know how much integers it contains when an pointer will appear.
The problem is that you don't know what is a pointer and what isn't - there is no arbitrary way to determine that.
|
1

Let me guess: you want to have an array, each element of which could be either a number or another array of number ? If so, make an array of std::vectors, and keep only one value if you want to store a number. Like this:

typedef std::vector< long long > I64Vector;
std::vector< I64Vector > array;
array.push_back( I64Vector( { 42 } ) ); // store one integer ( 42 ) in array[ 0 ]
array.push_back( I64Vector( { 1, 2, 3, 4, 5 } ) ); // store 5 integers in array[ 1 ]
//... etc.
// accessing them
long long num = array[ 0 ][ 0 ]; // 42
const I64Vector & subarray = array[ 1 ];
long subNumber1 = subarray[ 0 ]; // 1
long subNumber2 = subarray[ 1 ]; // 2
//... etc.

Comments

1

You have to change the type your array element to something that can store integers and pointers (or preferably std::vector<std::uint64_t>). A type-safe approach is to use boost::variant:

std::vector<boost::variant<std::uint64_t, std::vector<std::uint64_t>>> array;
array.push_back(1);
array.push_back(2);
array.push_back(std::vector<std::uint64_t>{4,5,6});

if (int* x = boost::get<std::uint64_t>(array[0])) { ... }
if (std::vector<std::uint64_t>* x 
        = boost::get<std::vector<std::uint64_t>>(array[2])) { ... }

If you want to use builtin arrays instead of vectors you can use unions instead of boost::variant:

 struct IntOrArray
 {
   bool isInt;
   union {
     std::uint64_t int_value;
     std::uint64_t* array_value;
   }
 };

 std::vector<IntOrArray> array;
 IntOrArray a1;
 IntOrArray a1.isInt = true;
 IntOrArray a1.int_value = 1;
 array.push_back(a1);

 IntOrArray a2;
 IntOrArray a2.isInt = false;
 IntOrArray a2.array_value = arrayYouGotElsewhere;
 array.push_back(a2);

 if (array[0].isInt) { ... }

C++11 allows you to put std::vectors into unions but this is not supported by all compilers.

Comments

-1

Yes we can do that,

a[3] = (unsigned long long)&a; (in windows)

and to get back the pointer value, (unsigned long long *)a[3]

1 Comment

Why did you point out "in windows" ? This code wouldn't work in Linux or MacOS ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.