1

I have quite peculiar problem. I want initialize an array pointed by a void pointer to which memory is allocated using new as shown below.

const int ARRAY_SIZE = 10;
void InitArray()
{
    int *ptrInt = new int[ARRAY_SIZE];
    for(int i=0; i<ARRAY_SIZE;i++)
    {
         ptrInt[i] = 1; //OK
    }

    void *ptrVoid = new int[ARRAY_SIZE];
    for(int i=0; i<ARRAY_SIZE;i++)
    {
         *(int*)ptrVoid[i] = 1; //Culprit  : I get a compiler error here 
                                //(error C2036: 'void *' : unknown size)
    }
}

Now, I want to initialize the elements of this array which is pointed by ptrVoid with say 1. How do I go about it? With this code I get a compiler error as shown in the code(I am using VS 2010). Any suggestions?

2
  • don't use new[]. Use std::vector. Commented Mar 23, 2011 at 6:01
  • Why do it all at once in the expression? Just get yourself a new intptr, and assign it the value of your voidptr. There are no extra points for the most cryptic code! Commented Mar 23, 2011 at 17:58

4 Answers 4

9

You have an order of operations problem (and an extra *). Try this inside your second loop:

((int *)ptrVoid)[i] = 1;
Sign up to request clarification or add additional context in comments.

1 Comment

@Chris - yes it is. The [] array index operator occurs first - that's where the error comes from. The fact that he has an extra dereference is also an error, but not the one causing the compiler message.
2

*(int*)ptrVoid[i] is *((int*)(ptrVoid[i])), and you're dereferencing too many times (the [] does a dereference).

Write ((int*)ptrVoid)[i] (or, better, static_cast<int*>(ptrVoid)[i]) then re-consider your use of void* at all.

2 Comments

I wish I could avoid using void pointer but its use was necessary as I don't know what type of data I will have store using that pointer before hand and I don't want to use templates as it was slowing down the speed. I am using it in a very low level function which is called zillion times to read data from file.
@user672160: Templates do not affect execution speed. They are a compile-time construct only. Also, speed cannot be slowed down or sped up: an object has a speed that can be decreased or increased, making the object slow down or speed up, respectively.
1

You just need to parenthesize correctly and cast the void* to an int*, so that the compiler knows how many bytes to offset when you index it with [i].

for(int i=0; i<ARRAY_SIZE;i++)
{
     ((int*)ptrVoid)[i] = 1;
}

Comments

0

How about:

int* ptrVoidAsInt = new int[ARRAY_SIZE];
for(int i=0; i<ARRAY_SIZE;i++)
{
     ptrVoidAsInt[i] = 1;                               
}
void* ptrVoid = ptrVoidAsInt;

But, one has to wonder what the meaning of either a void array or 1 initialised data is. Is this really an array of int or some other type that is going to be passed as a void* and then recast back to a typed array?

Comments

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.