Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to force blind cast from void* to struct*

Reply
Thread Tools

how to force blind cast from void* to struct*

 
 
andreyvul
Guest
Posts: n/a
 
      10-31-2007
g++ says a reinterpret cast from void* is disallowed; however, all I'm
trying to do is de-capsulate a void* argument that holds the pointer
to a class (and static_cast forces cast constructor)
any solutions?
also this pops up: 43: error: expected unqualified-id before 'return'
code:
includes: stdio.h, stdlib.h, time.h, pthread.h
struct:
template <typename T> struct mergesort_thread {
T* start;
T* end;
mergesort_thread(T* _start, T* _end) { start = _start; end = _end; }
static void *invoke(void *_this) {
pthread_t threads[2];
mergesort_thread<T> *child1, *child2;
T *mid, *shift, t;
T* start = (reinterpret_cast< mergesort_thread<T> >(_this))->start;
T* end = (reinterpret_cast< mergesort_thread<T> >(_this))->end;
if (start >= end) return NULL; //recursed deep enough (1 element)
//recursively sort halves of the list
mid = (T*)(start + ((end - start) >> 1));
child1 = new mergesort_thread<T>(start, mid);
pthread_create(&threads[0], NULL, mergesort_thread<T>::invoke, (void
*)child1);
child2 = new mergesort_thread<T>(mid + 1, end);
pthread_create(&threads[1], NULL, mergesort_thread<T>::invoke, (void
*)child2);
delete child1;
delete child2;
pthread_join(threads[0], NULL);
pthread_join(threads[1], NULL);
for (;(start <= mid) && (mid + 1 <= end); start++) {
if (*start < *(mid + 1)) //sorted (current element belongs in 1st
half)
continue;
else { /* true inplace merge requires insersion-sort-like
* methods because a value from the second half is inserted to
* the current element */
//copy the first element in the second half to t
t = *(mid + 1);
//shift first half to the right
for (shift = mid; shift >= start; shift--)
*(shift + 1) = *shift;
//copy t to start
*start = t;
mid++;
}
}
}
43: return NULL;
};

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-31-2007
andreyvul wrote:
> g++ says a reinterpret cast from void* is disallowed; however, all I'm
> trying to do is de-capsulate a void* argument that holds the pointer
> to a class (and static_cast forces cast constructor)
> any solutions?


Use 'static_cast'. That's what it's for (among some other things).

> also this pops up: 43: error: expected unqualified-id before 'return'


It would seem your 'return' is outside of any function.

> code:
> includes: stdio.h, stdlib.h, time.h, pthread.h
> struct:
> template <typename T> struct mergesort_thread {
> T* start;
> T* end;
> mergesort_thread(T* _start, T* _end) { start = _start; end = _end; }
> static void *invoke(void *_this) {
> pthread_t threads[2];
> mergesort_thread<T> *child1, *child2;
> T *mid, *shift, t;
> T* start = (reinterpret_cast< mergesort_thread<T> >(_this))->start;
> T* end = (reinterpret_cast< mergesort_thread<T> >(_this))->end;
> if (start >= end) return NULL; //recursed deep enough (1 element)
> //recursively sort halves of the list
> mid = (T*)(start + ((end - start) >> 1));
> child1 = new mergesort_thread<T>(start, mid);
> pthread_create(&threads[0], NULL, mergesort_thread<T>::invoke, (void
> *)child1);
> child2 = new mergesort_thread<T>(mid + 1, end);
> pthread_create(&threads[1], NULL, mergesort_thread<T>::invoke, (void
> *)child2);
> delete child1;
> delete child2;
> pthread_join(threads[0], NULL);
> pthread_join(threads[1], NULL);
> for (;(start <= mid) && (mid + 1 <= end); start++) {
> if (*start < *(mid + 1)) //sorted (current element belongs in 1st
> half)
> continue;
> else { /* true inplace merge requires insersion-sort-like
> * methods because a value from the second half is inserted to
> * the current element */
> //copy the first element in the second half to t
> t = *(mid + 1);
> //shift first half to the right
> for (shift = mid; shift >= start; shift--)
> *(shift + 1) = *shift;
> //copy t to start
> *start = t;
> mid++;
> }
> }
> }
> 43: return NULL;
> };


--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
andreyvul
Guest
Posts: n/a
 
      10-31-2007
On Oct 31, 6:16 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> andreyvul wrote:
> > g++ says a reinterpret cast from void* is disallowed; however, all I'm
> > trying to do is de-capsulate a void* argument that holds the pointer
> > to a class (and static_cast forces cast constructor)
> > any solutions?

>
> Use 'static_cast'. That's what it's for (among some other things).
>
> > also this pops up: 43: error: expected unqualified-id before 'return'

>
> It would seem your 'return' is outside of any function.
>
>
>
> > code:
> > includes: stdio.h, stdlib.h, time.h, pthread.h
> > struct:
> > template <typename T> struct mergesort_thread {
> > T* start;
> > T* end;
> > mergesort_thread(T* _start, T* _end) { start = _start; end = _end; }
> > static void *invoke(void *_this) {
> > pthread_t threads[2];
> > mergesort_thread<T> *child1, *child2;
> > T *mid, *shift, t;
> > T* start = (reinterpret_cast< mergesort_thread<T> >(_this))->start;
> > T* end = (reinterpret_cast< mergesort_thread<T> >(_this))->end;
> > if (start >= end) return NULL; //recursed deep enough (1 element)
> > //recursively sort halves of the list
> > mid = (T*)(start + ((end - start) >> 1));
> > child1 = new mergesort_thread<T>(start, mid);
> > pthread_create(&threads[0], NULL, mergesort_thread<T>::invoke, (void
> > *)child1);
> > child2 = new mergesort_thread<T>(mid + 1, end);
> > pthread_create(&threads[1], NULL, mergesort_thread<T>::invoke, (void
> > *)child2);
> > delete child1;
> > delete child2;
> > pthread_join(threads[0], NULL);
> > pthread_join(threads[1], NULL);
> > for (;(start <= mid) && (mid + 1 <= end); start++) {
> > if (*start < *(mid + 1)) //sorted (current element belongs in 1st
> > half)
> > continue;
> > else { /* true inplace merge requires insersion-sort-like
> > * methods because a value from the second half is inserted to
> > * the current element */
> > //copy the first element in the second half to t
> > t = *(mid + 1);
> > //shift first half to the right
> > for (shift = mid; shift >= start; shift--)
> > *(shift + 1) = *shift;
> > //copy t to start
> > *start = t;
> > mid++;
> > }
> > }
> > }
> > 43: return NULL;
> > };

>
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask

I was using void* to encapsulate a struct, so I used reinterpret_cast
because I know the actual type of the object that the void* is
referencing.
On a related note, do you of any c++ debuggers for linux? (gdb might
not work that easily with c++)

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-01-2007
andreyvul wrote:
> [..]
> I was using void* to encapsulate a struct, so I used reinterpret_cast
> because I know the actual type of the object that the void* is
> referencing.


'reinterpret_cast' is not for that. A pointer to an object can be
converted to 'void*' implicitly. You need 'static_cast' to get the
original pointer back.

> On a related note, do you of any c++ debuggers for linux? (gdb might
> not work that easily with c++)


I know that gdb worked just fine for me on Linux. Ask in the Linux
newsgroup, though. They may know more.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-01-2007
On Nov 1, 4:37 am, "Victor Bazarov" <(E-Mail Removed)> wrote:
> andreyvul wrote:
> > [..]
> > I was using void* to encapsulate a struct, so I used
> > reinterpret_cast because I know the actual type of the
> > object that the void* is referencing.


> 'reinterpret_cast' is not for that. A pointer to an object can be
> converted to 'void*' implicitly. You need 'static_cast' to get the
> original pointer back.


I'd use static_cast in this case, too, but according to the
standard, reinterpret_cast is also legal (and should give the
same results).

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      11-01-2007
On 31 Oct, 22:10, andreyvul <(E-Mail Removed)> wrote:

> g++ says a reinterpret cast from void* is disallowed; however, all I'm
> trying to do is de-capsulate a void* argument that holds the pointer
> to a class (and static_cast forces cast constructor)


what's a "cast constructor"?

<snip>


--
Nick Keighley

 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      11-01-2007
On Nov 1, 1:10 am, andreyvul <(E-Mail Removed)> wrote:
> g++ says a reinterpret cast from void* is disallowed; however, all I'm
> trying to do is de-capsulate a void* argument that holds the pointer
> to a class (and static_cast forces cast constructor)
> any solutions?
> also this pops up: 43: error: expected unqualified-id before 'return'
> code:
> includes: stdio.h, stdlib.h, time.h, pthread.h
> struct:
> template <typename T> struct mergesort_thread {
> T* start;
> T* end;
> mergesort_thread(T* _start, T* _end) { start = _start; end = _end; }
> static void *invoke(void *_this) {
> pthread_t threads[2];
> mergesort_thread<T> *child1, *child2;
> T *mid, *shift, t;
> T* start = (reinterpret_cast< mergesort_thread<T> >(_this))->start;
> T* end = (reinterpret_cast< mergesort_thread<T> >(_this))->end;
> if (start >= end) return NULL; //recursed deep enough (1 element)
> //recursively sort halves of the list
> mid = (T*)(start + ((end - start) >> 1));
> child1 = new mergesort_thread<T>(start, mid);
> pthread_create(&threads[0], NULL, mergesort_thread<T>::invoke, (void
> *)child1);
> child2 = new mergesort_thread<T>(mid + 1, end);
> pthread_create(&threads[1], NULL, mergesort_thread<T>::invoke, (void
> *)child2);
> delete child1;
> delete child2;
> pthread_join(threads[0], NULL);
> pthread_join(threads[1], NULL);
> for (;(start <= mid) && (mid + 1 <= end); start++) {
> if (*start < *(mid + 1)) //sorted (current element belongs in 1st
> half)
> continue;
> else { /* true inplace merge requires insersion-sort-like
> * methods because a value from the second half is inserted to
> * the current element */
> //copy the first element in the second half to t
> t = *(mid + 1);
> //shift first half to the right
> for (shift = mid; shift >= start; shift--)
> *(shift + 1) = *shift;
> //copy t to start
> *start = t;
> mid++;
> }
> }
> }
> 43: return NULL;
>
>
>
> };- Hide quoted text -
>


a pointer is an intrinsic type on which modern compilers are elegant
in optimizing. I mean using static_cast must not impose any overhead .

regards,
FM.

 
Reply With Quote
 
andreyvul
Guest
Posts: n/a
 
      11-01-2007

Nick Keighley wrote:
> On 31 Oct, 22:10, andreyvul <(E-Mail Removed)> wrote:
>
> > g++ says a reinterpret cast from void* is disallowed; however, all I'm
> > trying to do is de-capsulate a void* argument that holds the pointer
> > to a class (and static_cast forces cast constructor)

>
> what's a "cast constructor"?


A copy constructor with an argument of an unrelated object type.
Example:
class foo1 {
...
};
class foo2 {
foo2(foo1& foo) {
...member... = foo.member...
...
}
};

 
Reply With Quote
 
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
Posts: n/a
 
      11-01-2007
On 2007-11-01 18:06, andreyvul wrote:
> Nick Keighley wrote:
>> On 31 Oct, 22:10, andreyvul <(E-Mail Removed)> wrote:
>>
>> > g++ says a reinterpret cast from void* is disallowed; however, all I'm
>> > trying to do is de-capsulate a void* argument that holds the pointer
>> > to a class (and static_cast forces cast constructor)

>>
>> what's a "cast constructor"?

>
> A copy constructor with an argument of an unrelated object type.


The official name is converting constructor, if that matters.

--
Erik Wikström
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      11-02-2007
On 1 Nov, 17:23, Erik Wikström <(E-Mail Removed)> wrote:
> On 2007-11-01 18:06, andreyvul wrote:
>
> >Nick Keighleywrote:
> >> On 31 Oct, 22:10, andreyvul <(E-Mail Removed)> wrote:

>
> >> > g++ says a reinterpret cast from void* is disallowed; however, all I'm
> >> > trying to do is de-capsulate a void* argument that holds the pointer
> >> > to a class (and static_cast forces cast constructor)

>
> >> what's a "cast constructor"?

>
> > A copy constructor with an argument of an unrelated object type.

>
> The official name is converting constructor, if that matters.


aren't those invoked even if there is no cast?

class A
{
public:
A();
A(B);
};

void g(A a)
{
}

void f()
{
A a;
B b;

g(b); // CTOR A(B) invoked here?
}


--
Nick Keighley





 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Nike air force one, air force 1, air force one low cut, air force one abdul_razak@indiatimes.com Digital Photography 2 12-31-2008 04:29 PM
Nike Air Force Ones,Air Force One Air Force One-1,25th anniversary lky52112@gmail.com Digital Photography 0 01-15-2008 04:46 PM
Nike Air Force Ones,Air Force One Air Force One-1,25th anniversary lky52112@gmail.com Digital Photography 0 01-15-2008 04:34 PM
Exorcism: The Blind Leading The Blind Hats HTML 0 06-20-2005 12:49 AM



Advertisments