473,799 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about a cast


I have a couple of questions about the following code fragment:

struct list_info {
void * first;
void * last;
};

struct pipe_data_list {
void * data;
struct agent_port_list * dest;
struct pipe_data_list * link;
};
....

struct list_info next = {0};
struct list_info processed;
....

/* This code fragment concatenates lists next and processed */

if (next.last) {
((struct pipe_data_list *)next.last)->link = processed.first ;
next.last = processed.last;
} else next = processed;

What is going on here is that list_info is a generic container for null
terminated lists that records pointer to the first and last nodes of the
contained list. A reason for doing this is that it enables concatenating
lists without scanning them. The questions are about this line:

((struct pipe_data_list *)next.last)->link = processed.first ;

which compiles without warning. Are there any gotchas that I've
overlooked?

Richard Harter, cr*@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.
Sep 23 '08 #1
4 1498
On Sep 23, 7:51 pm, c...@tiac.net (Richard Harter) wrote:
I have a couple of questions about the following code fragment:

struct list_info {
void * first;
void * last;
};

struct pipe_data_list {
void * data;
struct agent_port_list * dest;
struct pipe_data_list * link;
};
....

struct list_info next = {0};
struct list_info processed;
....

/* This code fragment concatenates lists next and processed */

if (next.last) {
((struct pipe_data_list *)next.last)->link = processed.first ;
next.last = processed.last;
} else next = processed;

What is going on here is that list_info is a generic container for null
terminated lists that records pointer to the first and last nodes of the
contained list. A reason for doing this is that it enables concatenating
lists without scanning them. The questions are about this line:

((struct pipe_data_list *)next.last)->link = processed.first ;

which compiles without warning. Are there any gotchas that I've
overlooked?
No, why do you think so? (assuming next.last indeed does point to a
struct pipe_data_list or a compatible struct)
Sep 23 '08 #2
Richard Harter said:
>
I have a couple of questions about the following code fragment:

struct list_info {
void * first;
void * last;
};

struct pipe_data_list {
void * data;
struct agent_port_list * dest;
struct pipe_data_list * link;
};
....

struct list_info next = {0};
struct list_info processed;
....

/* This code fragment concatenates lists next and processed */

if (next.last) {
((struct pipe_data_list *)next.last)->link = processed.first ;
next.last = processed.last;
} else next = processed;

What is going on here is that list_info is a generic container for null
terminated lists that records pointer to the first and last nodes of the
contained list. A reason for doing this is that it enables concatenating
lists without scanning them. The questions are about this line:

((struct pipe_data_list *)next.last)->link = processed.first ;

which compiles without warning. Are there any gotchas that I've
overlooked?
As far as I can see, there is only one problem with the code, which is that
it causes someone as bright as Richard Harter to question whether it is
correct. Heaven knows what a maintenance weenie would make of it.

struct list_info *tmp = next.last;
tmp->link = processed.first ;

Don't you think that looks neater? YMMV, of course.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 23 '08 #3
On Tue, 23 Sep 2008 17:23:20 +0000, Richard Heathfield
<rj*@see.sig.in validwrote:
>Richard Harter said:
>>
I have a couple of questions about the following code fragment:

struct list_info {
void * first;
void * last;
};

struct pipe_data_list {
void * data;
struct agent_port_list * dest;
struct pipe_data_list * link;
};
....

struct list_info next = {0};
struct list_info processed;
....

/* This code fragment concatenates lists next and processed */

if (next.last) {
((struct pipe_data_list *)next.last)->link = processed.first ;
next.last = processed.last;
} else next = processed;

What is going on here is that list_info is a generic container for null
terminated lists that records pointer to the first and last nodes of the
contained list. A reason for doing this is that it enables concatenating
lists without scanning them. The questions are about this line:

((struct pipe_data_list *)next.last)->link = processed.first ;

which compiles without warning. Are there any gotchas that I've
overlooked?

As far as I can see, there is only one problem with the code, which is that
it causes someone as bright as Richard Harter to question whether it is
correct. Heaven knows what a maintenance weenie would make of it.
My view is that it is good for maintenance weenies; they need the
mental exercise. :-)
>
struct list_info *tmp = next.last;
tmp->link = processed.first ;

Don't you think that looks neater? YMMV, of course.
One minor problem - it's wrong. That should have been
struct pipe_data_list *tmp = next.last;

I dunno about neatness. I look at the posted form and say to
myself, there sure is a lot of crap on the LHS. OTOH I don't
much like sticking declarations in the middle of code. Yes, in
the posted code it slides in nicely because there is that test on
next.last and a brace immediately after it. Suppose I hadn't had
the test there? Then there wouldn't have been a natural reason
for braces to be there. Putting a pair in so that one could put
in a declaration for a temp for two lines of code would be rather
hokey. Actually, when I looked at your version, my first thought
was "Is this some kind of C99 crap?" and then it dawned on me
that it was a declaration with an initialization. Being old
fashioned, I like my declarations up front, gathered together,
with nice documentation.

Still and all, I suppose I will do it your way - I don't like
long winded statements.

Richard Harter, cr*@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.
Sep 23 '08 #4
Richard Harter said:
On Tue, 23 Sep 2008 17:23:20 +0000, Richard Heathfield
<rj*@see.sig.in validwrote:
<snip>
>>
struct list_info *tmp = next.last;
tmp->link = processed.first ;

Don't you think that looks neater? YMMV, of course.

One minor problem - it's wrong. That should have been
struct pipe_data_list *tmp = next.last;
Right. Did you see how easy my version was to debug? :-)
I dunno about neatness. I look at the posted form and say to
myself, there sure is a lot of crap on the LHS. OTOH I don't
much like sticking declarations in the middle of code. Yes, in
the posted code it slides in nicely because there is that test on
next.last and a brace immediately after it. Suppose I hadn't had
the test there? Then there wouldn't have been a natural reason
for braces to be there.
And so it would have had to be stuck in up top somewhere, yes. Not quite so
sweet then.
Putting a pair in so that one could put
in a declaration for a temp for two lines of code would be rather
hokey. Actually, when I looked at your version, my first thought
was "Is this some kind of C99 crap?"
Um, you clearly don't know me very well, then. :-)
and then it dawned on me
that it was a declaration with an initialization. Being old
fashioned, I like my declarations up front, gathered together,
with nice documentation.
Docs cost extra, payable in advance.
Still and all, I suppose I will do it your way - I don't like
long winded statements.
Never run for office.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 23 '08 #5

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

Similar topics

4
547
by: Richard Lee | last post by:
Hi, I have a question when I do a data type cast. the common way when we do a cast, is we know the type we want to cast to, i.e. we want to cast object to string, object xyz = "question"; (string)xyz; now we only have a type object of System.String type
15
1735
by: Christopher Benson-Manica | last post by:
If you had an unsigned int that needed to be cast to a const myClass*, would you use const myClass* a=reinterpret_cast<const myClass*>(my_val); or const myClass* a=(const myClass*)myVal; ?
4
10472
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is an unsigned integer, so I see a +1 if the bit is set. Microsoft VC++ thinks it's signed, so I see -1 if the bit is set. Ex. typedef enum {
26
2135
by: Janice | last post by:
What is the major reason for using void*? When should we use void* for both input arguments and return value? How can we cast the void* pointer to the type we need? Thanx
9
378
by: walt.stoneburner | last post by:
Why does aliasing an interface fail at runtime? SAMPLE WORKING CODE ... IHTMLDocument2 doc = (IHTMLDocument2) browser.Document; ... BROKEN CODE public interface IFooBar : IHTMLDocument2 { } ...
2
1937
by: Rouben Rostamian | last post by:
The main() function in the following code defines an m by n matrix, assigns value(s) to its elements, then passes the matrix to function foo(). For whatever it's worth, I have declared foo() so as to make it treat its first argument as a "read-only" object, that is, foo() can read but not alter the matrix. I have a problem, however, with /calling/ foo. If I call foo as foo(a,m,n), my compiler (gcc) complains about:
6
365
by: spibou | last post by:
In page 81 of N1124 in footnote 87 we read: If the value of the expression is represented with greater precision or range than required by the type named by the cast (6.3.1.8), then the cast specifies a conversion even if the type of the expression is the same as the named type. Can someone give me an example of what this means ?
14
1916
by: Daniel | last post by:
Hi guys who just answered me.....it really would have helped if i had written it right. Ok i will use better names to explain my problem. I have this: InterFaceClass ^ ClassA
7
1958
by: linq936 | last post by:
Hi, I am puzzled on this C puzzle: How to interpret this C statement: sizeof (int) * p Is that the size of integer type multiplies p or the size of whatever p points to in integer type? I think it should be the latter because there are 4 parser tokens here, sizeof, int, * and p, the first and the second are of same precedence which are higher than the third and all three operators are
14
2449
by: subramanian100in | last post by:
Suppose fgets is used to read a line of input. char str; fgets(str, sizeof(str), stdin); After reading some characters on the same line, if end-of-file is encountered, will fgets return the 'str' parameter and set EOF indicator for the stream ? Or will it return the string argument and set EOF indicator only on subsequent call ? Kindly clarify
0
9687
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
9543
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
10257
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
10029
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
9077
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
7567
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
6808
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();...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2941
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.