Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Casting void * to void ** ?

Reply
Thread Tools

Casting void * to void ** ?

 
 
Twister
Guest
Posts: n/a
 
      04-21-2006
Hi All,

I have a question which might sound very basic.

I have a simple structure:

struct simple{
void *buffer;
};
typedef struct simple Simple;

In my function I do this:

void do_Something(){

Simple *simp_struct;
simp_struct->buffer = malloc(10 * sizeof(int *));

call_func((void **)((int **)(simp_struct->buffer)));
....
}

The function call_func has this prototype:
call_func(void **buf);

I am confused with this piece of code:
call_func((void **)((int **)(simp_struct->buffer)));

What does this construct mean? How is that simp_struct->buffer
(which is a void *) is being cast to a int** followed by a
cast to void ** and passed to call_func ?

Rgds.
Mirage
 
Reply With Quote
 
 
 
 
Twister
Guest
Posts: n/a
 
      04-21-2006
Twister wrote:
> Hi All,
>
> I have a question which might sound very basic.
>
> I have a simple structure:
>
> struct simple{
> void *buffer;
> };
> typedef struct simple Simple;
>
> In my function I do this:
>
> void do_Something(){
>
> Simple *simp_struct;
> simp_struct->buffer = malloc(10 * sizeof(int *));
>
> call_func((void **)((int **)(simp_struct->buffer)));
> ....
> }
>
> The function call_func has this prototype:
> call_func(void **buf);
>
> I am confused with this piece of code:
> call_func((void **)((int **)(simp_struct->buffer)));
>
> What does this construct mean? How is that simp_struct->buffer
> (which is a void *) is being cast to a int** followed by a
> cast to void ** and passed to call_func ?
>
> Rgds.
> Mirage


I mistyped part of my previous mail:

This piece of code:
> I am confused with this piece of code:
> call_func((void **)((int **)(simp_struct->buffer)));


should be this:

for(i=0; i<10 ;i++)
call_func((void **)((int **)simp_struct->buffer + i));

My question remains the same. What does the above
construct mean?

Rgds.
Mirage
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      04-21-2006
In article <k872g.27$(E-Mail Removed)>,
Twister <(E-Mail Removed)> wrote:

>struct simple{
> void *buffer;
>};
>typedef struct simple Simple;


>void do_Something(){
>
> Simple *simp_struct;


simp_struct is an uninitialized pointer after that statement.

> simp_struct->buffer = malloc(10 * sizeof(int *));


But there you are using it as if it was initialized.
simp_struct->buffer involves dereferencing simp_struct first and
then accessing the structure component named buffer there, so
simp_struct needs to be given a value first.

> call_func((void **)((int **)(simp_struct->buffer)));
> ....
>}

--
Prototypes are supertypes of their clones. -- maplesoft
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      04-21-2006
Twister said:

<snip>

> simp_struct->buffer = malloc(10 * sizeof(int *));
>
> call_func((void **)((int **)(simp_struct->buffer)));


Why not just do this:

call_func(&simp_struct->buffer);

Casts are almost always wrong.


> The function call_func has this prototype:
> call_func(void **buf);
>
> I am confused with this piece of code:
> call_func((void **)((int **)(simp_struct->buffer)));
>
> What does this construct mean?


It means you don't (or whoever wrote it doesn't) understand what casting is
for.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      04-21-2006
Walter Roberson said:

> In article <k872g.27$(E-Mail Removed)>,
> Twister <(E-Mail Removed)> wrote:
>
>>struct simple{
>> void *buffer;
>>};
>>typedef struct simple Simple;

>
>>void do_Something(){
>>
>> Simple *simp_struct;

>
> simp_struct is an uninitialized pointer after that statement.


Good spot. I didn't see that. Silly me.

Everything I said still applies, but that applies too!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      04-21-2006
Twister said:

> for(i=0; i<10 ;i++)
> call_func((void **)((int **)simp_struct->buffer + i));
>
> My question remains the same. What does the above
> construct mean?


That's a major difference.

What you have now is a bug.

simp_struct->buffer has type void *, so you can't do pointer arithmetic on
it. So the cast to int ** gives you a value (of type int **) with which you
/can/ do pointer arithmetic. That is, (int **)simp_struct->buffer + i gives
you the address of the i'th int **, starting at simp_struct->buffer. The
expression has type int **. The cast to void ** is an error because there
is no guarantee that an int ** can be copied to a void ** without loss of
information.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
Twister
Guest
Posts: n/a
 
      04-21-2006
Richard Heathfield wrote:
> Twister said:
>
>
>>for(i=0; i<10 ;i++)
>> call_func((void **)((int **)simp_struct->buffer + i));
>>
>>My question remains the same. What does the above
>>construct mean?

>
>
> That's a major difference.
>
> What you have now is a bug.
>
> simp_struct->buffer has type void *, so you can't do pointer arithmetic on
> it. So the cast to int ** gives you a value (of type int **) with which you
> /can/ do pointer arithmetic. That is, (int **)simp_struct->buffer + i gives
> you the address of the i'th int **, starting at simp_struct->buffer. The
> expression has type int **. The cast to void ** is an error because there
> is no guarantee that an int ** can be copied to a void ** without loss of
> information.
>


simp_struct->buffer was earlier initialized to point to memory
of 10 (int *)'s. So isn't just saying, (int *)simp_struct->buffer + i
correct? Why cast it to an (int **), unless i'm not passing it
to a function which expects a int ** or a void ** ? Please correct
me if i'm wrong here.

Rgds.
Mirage
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      04-21-2006
Twister said:

> Richard Heathfield wrote:
>> Twister said:
>>
>>
>>>for(i=0; i<10 ;i++)
>>> call_func((void **)((int **)simp_struct->buffer + i));
>>>
>>>My question remains the same. What does the above
>>>construct mean?

>>
>>
>> That's a major difference.
>>
>> What you have now is a bug.
>>
>> simp_struct->buffer has type void *, so you can't do pointer arithmetic
>> on it. So the cast to int ** gives you a value (of type int **) with
>> which you /can/ do pointer arithmetic. That is, (int
>> **)simp_struct->buffer + i gives you the address of the i'th int **,
>> starting at simp_struct->buffer. The expression has type int **. The cast
>> to void ** is an error because there is no guarantee that an int ** can
>> be copied to a void ** without loss of information.
>>

>
> simp_struct->buffer was earlier initialized to point to memory
> of 10 (int *)'s. So isn't just saying, (int *)simp_struct->buffer + i
> correct?


No, that would point to the i'th int, not the i'th int *.

> Why cast it to an (int **),


To get a pointer to the i'th int *.

> Please correct me if i'm wrong here.


The cast to int ** is correct, but doesn't help you solve your void **
problem.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
Twister
Guest
Posts: n/a
 
      04-21-2006
Richard Heathfield wrote:
> Twister said:
>
>
>>Richard Heathfield wrote:
>>
>>>Twister said:
>>>
>>>
>>>
>>>>for(i=0; i<10 ;i++)
>>>> call_func((void **)((int **)simp_struct->buffer + i));
>>>>
>>>>My question remains the same. What does the above
>>>>construct mean?
>>>
>>>
>>>That's a major difference.
>>>
>>>What you have now is a bug.
>>>
>>>simp_struct->buffer has type void *, so you can't do pointer arithmetic
>>>on it. So the cast to int ** gives you a value (of type int **) with
>>>which you /can/ do pointer arithmetic. That is, (int
>>>**)simp_struct->buffer + i gives you the address of the i'th int **,
>>>starting at simp_struct->buffer. The expression has type int **. The cast
>>>to void ** is an error because there is no guarantee that an int ** can
>>>be copied to a void ** without loss of information.
>>>

>>
>>simp_struct->buffer was earlier initialized to point to memory
>>of 10 (int *)'s. So isn't just saying, (int *)simp_struct->buffer + i
>>correct?

>
>
> No, that would point to the i'th int, not the i'th int *.
>
>
>>Why cast it to an (int **),

>
>
> To get a pointer to the i'th int *.
>
>
>>Please correct me if i'm wrong here.

>
>
> The cast to int ** is correct, but doesn't help you solve your void **
> problem.
>


Thanks. That clarified part of my question.

>The cast to void ** is an error because there is no guarantee that an
>int ** can be copied to a void ** without loss of information.


The malloc just allocated enough space for 10 (int *) pointers.
The pointers themselves are not pointing to valid memory. So If I cast
simp_struct->buffer finally to a void **(after the cast to an int **)
and pass it to a function which allocates some momory and points these
int *'s to valid memory, am I not doing the right thing ? Where is the
loss of information happening ?

Rgds.
Mirage
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      04-21-2006
Twister said:

> Richard Heathfield wrote:
>
> >The cast to void ** is an error because there is no guarantee that an
> >int ** can be copied to a void ** without loss of information.

>
> The malloc just allocated enough space for 10 (int *) pointers.


Yes.

> The pointers themselves are not pointing to valid memory.


Right.

> So If I cast
> simp_struct->buffer finally to a void **(after the cast to an int **)
> and pass it to a function which allocates some momory and points these
> int *'s to valid memory, am I not doing the right thing ?


No, I'm afraid not.

> Where is the loss of information happening ?


There's no guarantee that information is lost. There's just no guarantee
that it's not lost! Either could happen. In other words, it might "work"
just fine on your development machine - and then break on some other box.

The problem is that, whilst the Standard guarantees that you can use void *
to store any object pointer (without loss of information), it doesn't offer
the same guarantee for void **.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the difference, void func(void) and void fucn() noblesantosh@yahoo.com C Programming 5 07-22-2005 04:38 PM
"void Method()" vs "void Method(void)" Ollej Reemt C++ 7 04-22-2005 03:47 AM
Casting const void * into void * Enrico `Trippo' Porreca C Programming 14 06-04-2004 03:30 PM
[Slightly OT] Casting non-void function results to void in modern compilers. David M. Wilson C Programming 8 01-07-2004 07:32 AM
`void **' revisited: void *pop(void **root) Stig Brautaset C Programming 15 10-28-2003 09:03 AM



Advertisments