473,797 Members | 3,148 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is cast between structs safe?

I read `Advanced Programming in the UNIX Environment, 2nd ed.'. In
section 16.3.2, the book says that variables type struct sockaddr_in;
and/or struct sockaddr_in6; will be cast explicitly to variables type
struct sockaddr; Is this safe? Or will these casts between different
structs result in object slices? see casts at line 6 & 9 for example:

struct sockaddr add1, *add2; /*see struct declarations below*/
struct sockaddr_in add_in;
struct sockaddr_in6 add_in6;
void *p;

add1 = (struct sockaddr) add_in; /*LINE 6*/

p = &add_in6;
add2 = p; /*LINE 9*/
/*add2 = (struct sockaddr*) (p);*/

/*************** *************** ************/

struct sockaddr{
sa_family_t sa_family; /*address family*/
char sa_data[14]; /*variable-length address*/
};

struct sockaddr_in{
sa_family_t sin_family; /*address family*/
in_port sin_port; /*port number*/
struct in_addr {
in_addr_t s_addr;
} sin_addr; /*IPv4 address*/
};

struct sockaddr_in6{
sa_family_t sin6_family; /*address family*/
in_port sin6_port; /*port number*/
uint32_t sin6_flowinfo; /*traffic class and flow
info*/
struct in6_addr {
uint8_t s_addr[16];
} sin6_addr; /*IPv6 address*/
uint32_t sin6_scope_id; /*set of interface for
scope*/
};

Apr 4 '07 #1
2 7787
lovecreatesbea. ..@gmail.com wrote On 04/04/07 10:50,:
I read `Advanced Programming in the UNIX Environment, 2nd ed.'. In
section 16.3.2, the book says that variables type struct sockaddr_in;
and/or struct sockaddr_in6; will be cast explicitly to variables type
struct sockaddr; Is this safe? Or will these casts between different
structs result in object slices? see casts at line 6 & 9 for example:

struct sockaddr add1, *add2; /*see struct declarations below*/
struct sockaddr_in add_in;
struct sockaddr_in6 add_in6;
void *p;

add1 = (struct sockaddr) add_in; /*LINE 6*/
[...]
Are you sure you have reproduced the code accurately?
A conforming C compiler is required to emit a diagnostic
for your "LINE 6," which I suspect is missing a few
asterisks and ampersands. Please try again, or confirm
that the code you've posted is accurate (in which case,
it isn't C).

--
Er*********@sun .com
Apr 4 '07 #2
On 4 Apr 2007 07:50:38 -0700, "lovecreatesbea ...@gmail.com"
<lo************ ***@gmail.comwr ote:
I read `Advanced Programming in the UNIX Environment, 2nd ed.'. In
section 16.3.2, the book says that variables type struct sockaddr_in;
and/or struct sockaddr_in6; will be cast explicitly to variables type
struct sockaddr; Is this safe? Or will these casts between different
structs result in object slices? see casts at line 6 & 9 for example:
You can't cast struct types in C; if you could, slicing might indeed
be a problem. What you do with the various sockaddr_ structures is
cast _pointers to them_, and where relevant also keep track of the
length explicitly e.g. in the third argument to accept().

Apr 15 '07 #3

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

Similar topics

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 */
6
1711
by: Nick Weekes | last post by:
Hi all, I am trying to cast a Double (its actually the NextDouble method of the Random class, but could be any type of number) to a Type via its name. Im trying the following syntax: ThisNumber = (Type.GetType("MyType.Decimal"))(ThisRandom.NextDouble()); Im getting an error stating that 'Method name expected', so either im
11
3897
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
17
2575
by: goldfita | last post by:
I saw some code that appeared to do something similar to this struct foo { char offset; int d; }; struct foo { int a; int b;
14
3840
by: Jeff S. | last post by:
In a Windows Forms application I plan to have a collection of structs - each of which contains a bunch of properties describing a person (e.g., LastName, FirstName, EmployeeID, HomeAddress, ZipCode, etc). So each instance of the struct will describe a person - and about 900 instances (people) will be contained in the collection. Users must be able to search for a specific person by any of the properties (e.g., LastName, FirstName,...
61
3783
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch (myBranch + x) = new Branch(i); // it doesn't x is a loop iterator, i is an int for the constructor to define an array. What am I doing wrong here.
12
5543
by: Milux | last post by:
Hi All, This question has to do with interface design. Suppose I have an translation tool. It can translates structs from type "general" to other types and then does some processing. Example: typedef struct {
5
2490
by: christian.pontesegger | last post by:
Hi all, lately I had a problem where I wanted to cast a basic class to a derived type, which did not introduce any new members and did not change any stuff with dynamically allocated memory. I just wanted to add some methods to the class. So my question is: Is it valid C++ to create a basic class and to cast it to a derived one the way I did in my example below? The example works fine for
2
4301
by: =?Utf-8?B?Unlhbg==?= | last post by:
Hi, How can I get around runtime error that says I can not explicit cast List<SubClassto ICollection<Class>? Generic List inhertis generic ICollection and Subclass inherits Class, then should not a problem to cast it explicitly, right?
0
9685
marktang
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...
0
10468
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...
0
10245
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10021
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
9063
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
7559
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...
0
6802
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();...
1
4131
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
3
2933
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.