473,804 Members | 3,462 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to force blind cast from void* to struct*

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 Tstruct mergesort_threa d {
T* start;
T* end;
mergesort_threa d(T* _start, T* _end) { start = _start; end = _end; }
static void *invoke(void *_this) {
pthread_t threads[2];
mergesort_threa d<T*child1, *child2;
T *mid, *shift, t;
T* start = (reinterpret_ca st< mergesort_threa d<T(_this))->start;
T* end = (reinterpret_ca st< mergesort_threa d<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_threa d<T>(start, mid);
pthread_create( &threads[0], NULL, mergesort_threa d<T>::invoke, (void
*)child1);
child2 = new mergesort_threa d<T>(mid + 1, end);
pthread_create( &threads[1], NULL, mergesort_threa d<T>::invoke, (void
*)child2);
delete child1;
delete child2;
pthread_join(th reads[0], NULL);
pthread_join(th reads[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;
};

Oct 31 '07 #1
14 4440
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 Tstruct mergesort_threa d {
T* start;
T* end;
mergesort_threa d(T* _start, T* _end) { start = _start; end = _end; }
static void *invoke(void *_this) {
pthread_t threads[2];
mergesort_threa d<T*child1, *child2;
T *mid, *shift, t;
T* start = (reinterpret_ca st< mergesort_threa d<T(_this))->start;
T* end = (reinterpret_ca st< mergesort_threa d<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_threa d<T>(start, mid);
pthread_create( &threads[0], NULL, mergesort_threa d<T>::invoke, (void
*)child1);
child2 = new mergesort_threa d<T>(mid + 1, end);
pthread_create( &threads[1], NULL, mergesort_threa d<T>::invoke, (void
*)child2);
delete child1;
delete child2;
pthread_join(th reads[0], NULL);
pthread_join(th reads[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
Oct 31 '07 #2
On Oct 31, 6:16 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
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 Tstruct mergesort_threa d {
T* start;
T* end;
mergesort_threa d(T* _start, T* _end) { start = _start; end = _end; }
static void *invoke(void *_this) {
pthread_t threads[2];
mergesort_threa d<T*child1, *child2;
T *mid, *shift, t;
T* start = (reinterpret_ca st< mergesort_threa d<T(_this))->start;
T* end = (reinterpret_ca st< mergesort_threa d<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_threa d<T>(start, mid);
pthread_create( &threads[0], NULL, mergesort_threa d<T>::invoke, (void
*)child1);
child2 = new mergesort_threa d<T>(mid + 1, end);
pthread_create( &threads[1], NULL, mergesort_threa d<T>::invoke, (void
*)child2);
delete child1;
delete child2;
pthread_join(th reads[0], NULL);
pthread_join(th reads[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_cas t
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++)

Oct 31 '07 #3
andreyvul wrote:
[..]
I was using void* to encapsulate a struct, so I used reinterpret_cas t
because I know the actual type of the object that the void* is
referencing.
'reinterpret_ca st' 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
Nov 1 '07 #4
On Nov 1, 4:37 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
andreyvul wrote:
[..]
I was using void* to encapsulate a struct, so I used
reinterpret_cas t because I know the actual type of the
object that the void* is referencing.
'reinterpret_ca st' 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_cas t is also legal (and should give the
same results).

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nov 1 '07 #5
On 31 Oct, 22:10, andreyvul <andrey....@gma il.comwrote:
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

Nov 1 '07 #6
On Nov 1, 1:10 am, andreyvul <andrey....@gma il.comwrote:
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 Tstruct mergesort_threa d {
T* start;
T* end;
mergesort_threa d(T* _start, T* _end) { start = _start; end = _end; }
static void *invoke(void *_this) {
pthread_t threads[2];
mergesort_threa d<T*child1, *child2;
T *mid, *shift, t;
T* start = (reinterpret_ca st< mergesort_threa d<T(_this))->start;
T* end = (reinterpret_ca st< mergesort_threa d<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_threa d<T>(start, mid);
pthread_create( &threads[0], NULL, mergesort_threa d<T>::invoke, (void
*)child1);
child2 = new mergesort_threa d<T>(mid + 1, end);
pthread_create( &threads[1], NULL, mergesort_threa d<T>::invoke, (void
*)child2);
delete child1;
delete child2;
pthread_join(th reads[0], NULL);
pthread_join(th reads[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.

Nov 1 '07 #7

Nick Keighley wrote:
On 31 Oct, 22:10, andreyvul <andrey....@gma il.comwrote:
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...
...
}
};

Nov 1 '07 #8
On 2007-11-01 18:06, andreyvul wrote:
Nick Keighley wrote:
>On 31 Oct, 22:10, andreyvul <andrey....@gma il.comwrote:
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
Nov 1 '07 #9
On 1 Nov, 17:23, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-11-01 18:06, andreyvul wrote:
Nick Keighleywrote:
On 31 Oct, 22:10, andreyvul <andrey....@gma il.comwrote:
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

Nov 2 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
3348
by: Jamie Burns | last post by:
Hello, I am writing a client / server application. There is 1 server, and many clients. The server processes requests from each client, and typically creates and manipulates C++ objects on their behalf. Now, when a client requests for an object to be created, I pass back a pointer to the object (from server memory address scope) as a "long integer". To the client, this is just an ID for the object they wish to access. From then on,...
3
1840
by: Andreas Müller | last post by:
i have a problem with threads and casts, I created a thread pthread_create(&grab_thread, NULL, grab_image, (void*)&camera); and my function is like this: void *grab_image(void *camera) {..} originally the type of camera is "struct Camera" but pthreads only wants voids. now I need to cast the "void *camera" to "struct Camera *camera"
4
9315
by: William Payne | last post by:
Hello! If a cast from a void pointer to a pointer to a struct fails at runtime, is NULL the result? I.E, is this code meaningful: struct my_struct_t foo* = (struct my_struct_t*)void_pointer; // void_pointer is of type void* if(foo == NULL) { printf("Error!\n"); }
11
2054
by: Alberto Giménez | last post by:
Hi, I've seen some object oriented programming bits out there and i'm not sure if they're legal. For example: struct Object { int field1; int field2; }; struct SubObject { int field1; /* the same as Object */
11
3899
by: Roman Mashak | last post by:
Hello, All! I'm implementing function parsing config file, which look like this: # this is comment port=10000 path=/var/run/dump.pid .... Declared the type
6
527
by: Jack | last post by:
Is it possible to cast a 4-byte data type (e.g. an unsigned int) to a 4-byte struct? Sometimes the HIWORD and LOWORD of a 4-byte value contain information independent of each other. Is there a direct way to perform this cast? If not, is there way in C++ to do it? For example, I would like my function test() to work the way it is coded below. It produces a compiler error though: #include <pshpack2.h// 16-bit padding
6
2654
by: Nick Keighley | last post by:
Hi, Is this code fundamentally broken? class B { } class D: public B {
1
452
by: etienne | last post by:
Hello all, i'm experimenting a problem in trying to cast a char * variable to a pointer to a function. My problem is that I want to execute functions in threads, using the pthread_create function. The name of the functions are read in txt file at runtime. So I want to do something like char * func_to_run = scanf(the, good, args); pthread_create (thread, attr, (magic cast)funct_to_run, arg);
7
3953
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
0
9591
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10594
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10331
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10087
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9166
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7631
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.