Go Back   Velocity Reviews > Newsgroups > C Programming
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C Programming - void *

 
Thread Tools Search this Thread
Old 11-01-2009, 07:23 PM   #1
Default void *


I'm just experimenting with what i can and can't do with C and
pointer. I wanted to create a method that will swap 2 variables, no
matter what their type. I tried doing it like so:

void swap(void *source, void *dest) {
void *temp = source;

*source = *dest;
*dest = *temp;
}

...but i'm getting the error.

Invalid use of void type.

How can i achieve what i'm trying to do?

Thanks


bandejapaisa
  Reply With Quote
Old 11-01-2009, 07:25 PM   #2
bandejapaisa
 
Posts: n/a
Default Re: void *
Sorry, the error is "Invalid use of void expression"



bandejapaisa
  Reply With Quote
Old 11-01-2009, 07:44 PM   #3
Seebs
 
Posts: n/a
Default Re: void *
On 2009-11-01, bandejapaisa <> wrote:
> I'm just experimenting with what i can and can't do with C and
> pointer. I wanted to create a method that will swap 2 variables, no
> matter what their type.


Interesting thought!

> I tried doing it like so:


> void swap(void *source, void *dest) {
> void *temp = source;
>
> *source = *dest;
> *dest = *temp;
> }


> ..but i'm getting the error.


> Invalid use of void type.


Right.

That's because "void" doesn't have a size. In general, when you convert
an address to (void *), you no longer have information about how large the
object pointed to was. As a result, there's no way for this to generate
code; it has no idea what to copy.

> How can i achieve what i'm trying to do?


You can't completely solve this portably without providing extra data. The
problem is that there's no portable way to inquire as to the type of an
object in a macro or function or anything like that. Here's what I'd
probably do:

void swap_item(void *o1, void *o2, size_t len) {
/* assumes C99 or GNU C */
unsigned char buf[len];
memcpy(buf, o1, len);
memcpy(o1, o2, len);
memcpy(o2, buf, len);
}

#define swap(x, y) (swap_item((x),(y),sizeof((x))))

This doesn't sanity-check that x and y have compatible types, or even
compatible sizes.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


Seebs
  Reply With Quote
Old 11-01-2009, 07:46 PM   #4
Seebs
 
Posts: n/a
Default Re: void *
On 2009-11-01, Richard Heathfield <> wrote:
> void swap(void *vleft, void *vright, size_t len)
> {
> unsigned char *left = vleft;
> unsigned char *right = vright;
> unsigned char tmp;
> while(len--)
> {
> tmp = *left;
> *left++ = *right;
> *right++ = tmp;
> }
> }


Oh, yes, listen to Richard, not me, he avoided depending on the VLA
feature. (Downside, for sufficiently large objects, mine might be
faster...)

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


Seebs
  Reply With Quote
Old 11-01-2009, 08:14 PM   #5
Keith Thompson
 
Posts: n/a
Default Re: void *
Seebs <usenet-> writes:
> On 2009-11-01, Richard Heathfield <> wrote:
>> void swap(void *vleft, void *vright, size_t len)
>> {
>> unsigned char *left = vleft;
>> unsigned char *right = vright;
>> unsigned char tmp;
>> while(len--)
>> {
>> tmp = *left;
>> *left++ = *right;
>> *right++ = tmp;
>> }
>> }

>
> Oh, yes, listen to Richard, not me, he avoided depending on the VLA
> feature. (Downside, for sufficiently large objects, mine might be
> faster...)


A reasonable compromise, if you're concerned about both time and
space, would be to declare a fixed-size buffer and use memcpy() in a
loop.


--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


Keith Thompson
  Reply With Quote
Old 11-01-2009, 09:15 PM   #6
Seebs
 
Posts: n/a
Default Re: void *
On 2009-11-01, Keith Thompson <kst-> wrote:
> A reasonable compromise, if you're concerned about both time and
> space, would be to declare a fixed-size buffer and use memcpy() in a
> loop.


Yes.

Interestingly, this might end up being a good place to use Duff's Device.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


Seebs
  Reply With Quote
Old 11-01-2009, 09:25 PM   #7
bandejapaisa
 
Posts: n/a
Default Re: void *

>
> Its hard to know where to start.
>
> Even forgetting the issues with types : have a careful look at your
> *temp pointer. You dont actually store the temp value pointed at.
>
> You are confused with pointers and the values they point to it seems.
>
>
>
> > void swap(void *source, void *dest) {
> > * *void *temp = source;

>
> you are storing the POINTER not the VALUE pointed to.
>


I did this because i was experimenting and it wouldn't compile when i
did

void *temp = *source;

Its the use of (void *) that i'm not quite getting. It worked fine
when i declared the types, i.e:

void swap(int *x, int *y) {
int *t = *x;
*x = *y;
*y = t;
}




bandejapaisa
  Reply With Quote
Old 11-01-2009, 09:28 PM   #8
bandejapaisa
 
Posts: n/a
Default Re: void *
It seems this problem is more complex than i thought. I was under the
assumption that i was missing something quite basic.

Thanks for your responses.


bandejapaisa
  Reply With Quote
Old 11-01-2009, 09:31 PM   #9
Flash Gordon
 
Posts: n/a
Default Re: void *
Richard wrote:
> bandejapaisa <> writes:


<snip>

>> Invalid use of void type.
>>
>> How can i achieve what i'm trying to do?
>>
>> Thanks

>
> I would suggest going back to basics of pointers and step through with a
> good debugger noting the pointer address values and the contents of the
> memory they point to. It will become very clear, very quickly then.


Whilst stepping through in a debugger might be good sometimes, it is a
bit difficult when the code won't compile. RH and Peter have addressed
these issues, so I won't repeat them.
--
Flash Gordon


Flash Gordon
  Reply With Quote
Old 11-01-2009, 09:32 PM   #10
Flash Gordon
 
Posts: n/a
Default Re: void *
Richard wrote:
> bandejapaisa <> writes:


<snip>

>> Invalid use of void type.
>>
>> How can i achieve what i'm trying to do?
>>
>> Thanks

>
> I would suggest going back to basics of pointers and step through with a
> good debugger noting the pointer address values and the contents of the
> memory they point to. It will become very clear, very quickly then.


Whilst stepping through in a debugger might be good sometimes, it is a
bit difficult when the code won't compile. RH and Peter have addressed
these issues, so I won't repeat them.
--
Flash Gordon


Flash Gordon
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46