Re: A little help please

From: Jonathan Mcdougall (jonathanmcdougall_at_DELyahoo.ca)
Date: 12/28/04


Date: Tue, 28 Dec 2004 11:24:10 -0500

Rich wrote:
>>You don't need the cast.
>
> But according to the question if I read it correctly I do???
> The question is exactly as written to newsgroup.

You don't need an explicit cast to convert a
pointer type to void*. You *do* need to cast a
void* to another pointer type.

>>>arr += nBytes // increment by nBytes to get next element
>>
>>That shows us you don't know how pointers work and that means you shouldn't
>>use them.
>
> ??? if arr points to first element of int array then I thought by
> incrementing the pointer by nBytes I was pointing to next array element.?

That works if the pointer has a known type, such
as int*. When doing pointer arithmetics, such as
increment and decrement, the pointer is moved
according the the type's size. On an
implementation where ints are 4 bytes, the pointer
would move by 4 bytes. If you try to do that on a
void pointer, how many bytes would the pointer
move by?

>>>int* i = &arrX[0]; // set i as pointer to first element of array
>>>void * vp = static_cast<void*> (i); // convert i to a void*
>>
>>
>>You don't need that
>>
>>void *vp=i;
>
> right now I am confused, the question states that I should cast the int
> array to a void* using a static cast.

First, you don't cast it to an array of void*, you
are casting it to a pointer to a void*.

Second, you don't need static_cast to cast an int*
to a void* (try it).

>>> // assign to vp
>>>func1( vp, sizeof( arrX ), 8 );
>>>for( int i = 0; i < 10; i++ )
>>
>>
>>Prefer ++i over i++.
>
> if I did ++i would this not make the start of loop start at 1 ?? since
> it increments before evaluating?

No, the increment is done at the same point, that
is, after the first execution of the loop, whether
you use ++i or i++. Do read about the difference
about postfix and prefix increment/decrement.

>>Your code is bound to be invalid. You'll have to assume a type (such as
>>char, as does memset()), ask the user for one or forget that function.
>>Templates are meant for this kind of problem.
>
> We have not covered such things yet, we have played with vectors but not
> templates yet.

And now you are playing with void pointers? What
kind of c++ class is that?

> I meant the only reason I could see for using a vcid* is for when you
> don't know the type to be assigned to a pointer so by declaring the
> pointer void* I can assign any type to it, but can't dereference a void*
> but instead have to cast it back to dereference it.

That's right.

> For example
>
> void* vp;
> vp = &i;
>
> // *vp = 4 // can't do this can't dereference a void*
> // have to do this instead
>
> i = static_cast<int> (vp); // cast back to int*

You need a reinterpret_cast here.

> *i += 4; // i += 4 would make pointer point 4 ints further in memory?

Yes.

Here is an example.

# include <iostream>

void increment_an_int(void *p, int by)
{
   int *ip = reinterpret_cast<int*>(p);

   *ip += by;
}

int main()
{
   int my_int = 10;

   std::cout << "before: " << my_int << std::endl;

   increment_an_int(&my_int, 100);

   std::cout << "after: " << my_int << std::endl;

}

Output:
10
110

Jonathan



Relevant Pages

  • Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... You can't do pointer arithmetic on a void* value. ... qsort behaves in a manner consistent with its specification. ... actually works as advertised...but doesn't mean that the cast is ...
    (comp.lang.c)
  • Re: confusion: casting function pointers
    ... pointer from the 'actual/other modules' that takes arguments of type ... list to types of void *). ... int main{ ... without a prototype, a number of special "promotion" rules take ...
    (comp.lang.c)
  • Re: Common misconceptions about C (C95)
    ... Cast it to suitable type instead. ... The void and void * types come from C++ which has a better rules for them. ... object as an array of bytes (unsigned char *). ... it's okay for that pointer to be a pointer to unsigned char *. ...
    (comp.lang.c)
  • Re: How would you design Cs replacement?
    ... I would get rid of void. ... a member called size of type int. ... size of the array pointed at by v->dynamic is given by int size. ... If you pass an unsized pointer to a sized parameter, ...
    (comp.lang.c)
  • Re: dynamic_cast does not work as specified
    ... then p is a pointer to an object of type SubThing. ... SubThing* via a C-style cast. ... virtual void ShowPURE; ... type-id must be a derived class of expression [note that the simple explanation could be ...
    (microsoft.public.vc.mfc)