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*)vo id_pointer;
// void_pointer is of type void*
if(foo == NULL)
{
printf("Error!\ n");
}
/ William Payne 4 9313
William Payne wrote: Hello! If a cast from a void pointer to a pointer to a struct fails at runtime, is
What do you mean by `fails'?
NULL the result? I.E, is this code meaningful:
struct my_struct_t foo* = (struct my_struct_t*)vo id_pointer;
Do you perhaps mean:
struct my_struct_t *foo - (struct my_struct_t *)void_pointer;
// void_pointer is of type void* if(foo == NULL) { printf("Error!\ n"); }
With the correction above, sure -- iff `void_pointer' was NULL to begin
with and originated as a pointer to `struct my_struct_t'.
I think you either have another question here -- or it's time to OTDB!*
HTH,
--ag
* - Open The Damn Book
--
Artie Gold -- Austin, Texas
"Yeah. It's an urban legend. But it's a *great* urban legend!"
On Sat, 14 Feb 2004, William Payne wrote: 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*)vo id_pointer;
First of all, it's not correct, because there are syntax errors
on it. Fixing the errors, and getting rid of the spurious cast
(which serves only to shut up the compiler about errors; casts
almost never solve problems), we have
| struct my_struct_t *foo = void_pointer;
If 'void_pointer' is pointing at a 'struct my_struct_t', or is
a null pointer, then this will work in the obvious fashion.
Otherwise, this line invokes undefined behavior. For example,
int i;
void *p = &i;
struct my_struct_t *foo = void_pointer;
This is undefined behavior and can do anything it likes (including
possibly setting 'foo' to NULL, but more likely setting it to a
nonsense value, rounding it to a "close-enough" value, or raising
a bus error).
struct my_struct_t s;
void *p = &s;
struct my_struct_t *foo = void_pointer;
This is defined behavior, and 'foo' now points to 's'.
HTH,
-Arthur
Artie Gold wrote: William Payne wrote:
Hello! If a cast from a void pointer to a pointer to a struct fails at runtime, is
What do you mean by `fails'?
NULL the result? I.E, is this code meaningful:
struct my_struct_t foo* = (struct my_struct_t*)vo id_pointer;
Do you perhaps mean:
struct my_struct_t *foo - (struct my_struct_t *)void_pointer;
= (of course) // void_pointer is of type void* if(foo == NULL) { printf("Error!\ n"); }
With the correction above, sure -- iff `void_pointer' was NULL to begin with and originated as a pointer to `struct my_struct_t'.
I think you either have another question here -- or it's time to OTDB!*
HTH, --ag
* - Open The Damn Book
--
Artie Gold -- Austin, Texas
"Yeah. It's an urban legend. But it's a *great* urban legend!"
Arthur J. O'Dwyer <aj*@nospam.and rew.cmu.edu> spoke thus: 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*)vo id_pointer;
First of all, it's not correct, because there are syntax errors on it. Fixing the errors, and getting rid of the spurious cast (which serves only to shut up the compiler about errors; casts almost never solve problems), we have
| struct my_struct_t *foo = void_pointer;
Is it possible that the OP was thinking in the C++ world, which (IIRC)
requires the cast of void_pointer? Perhaps he was looking for
something like dynamic_cast that throws an exception when it "fails"
at runtime? Just a (probably wrong and definitely OT) thought.
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: James Mcguire |
last post by:
Hi,
I frequently do non-initialisation type structure assignment via casting:
e.g.
struct s{int i,j,k;} mys;
....
mys=(struct s){3,4,5};
|
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
|
by: Frederick Gotham |
last post by:
Let's assume that we're working on the following system:
CHAR_BIT == 8
sizeof( char* ) == 4 (i.e. 32-Bit)
Furthermore, lets assume that the memory addresses are distributed as
follows:
0x00000000 through 0xFFFFFFFE : Valid byte addresses
|
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
|
by: Nick Keighley |
last post by:
Hi,
Is this code fundamentally broken?
class B
{
}
class D: public B
{
| |
by: Allen |
last post by:
I want to package an address to byte buffer. So I need to cast it to
integer value.
How to cast it?
const char * ptr = 0x0013e328;
int value = <cast(ptr); // use reinterpret_cast?
|
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);
|
by: andreyvul |
last post by:
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_thread {
T* start;
|
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
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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
| |
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...
| |