![]() |
|
|
|
#1 |
|
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 |
|
|
|
|
#2 |
|
Posts: n/a
|
Sorry, the error is "Invalid use of void expression"
bandejapaisa |
|
|
|
#3 |
|
Posts: n/a
|
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 |
|
|
|
#4 |
|
Posts: n/a
|
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 |
|
|
|
#5 |
|
Posts: n/a
|
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 |
|
|
|
#6 |
|
Posts: n/a
|
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 |
|
|
|
#7 |
|
Posts: n/a
|
> > 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 |
|
|
|
#8 |
|
Posts: n/a
|
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 |
|
|
|
#9 |
|
Posts: n/a
|
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 |
|
|
|
#10 |
|
Posts: n/a
|
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 |
|