473,839 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Recursive Union

I have 2 differesnt defination of same Union as below and a piece of
code for printing size of Union and its members..

union U
{
union U
{
int i;
int j;
}a[10];
int b[10];
}u;

union M
{
union N
{
int i;
int j;
}a[10];
int b[10];
}m;

void unionTest ()
{
printf("Union u\n");
printf("%d\n", sizeof(u));
printf("%d\n", sizeof(u.a)); // Its the cause for my headache...
printf("%d\n", sizeof(u.a[4].i));
printf("Union M\n");
printf("%d\n", sizeof(m));
printf("%d\n", sizeof(m.a));
printf("%d\n", sizeof(m.a[4].i));
}

For my surprise.. the output is
Union u
40
400
4
Union M
40
40
4

It will be complied if its a c file and Its ( redefination of Union U)
an error with c++ file..

Please let me know the reason for the out put ( 3rd printf statement)
And I obeserved that the memory layour for Union U is very peculiar!!
I have tried this with Microsoft Compiler..
Nov 14 '05 #1
4 2941
In article <88************ **************@ posting.google. com>,
gi***********@r ediffmail.com (Girish) wrote:
I have 2 differesnt defination of same Union as below and a piece of
code for printing size of Union and its members..

union U
{
union U
{
int i;
int j;
}a[10];
int b[10];
}u;
.... void unionTest ()
{
printf("Union u\n");
printf("%d\n", sizeof(u));
printf("%d\n", sizeof(u.a)); // Its the cause for my headache...
printf("%d\n", sizeof(u.a[4].i));
printf("Union M\n");
printf("%d\n", sizeof(m));
printf("%d\n", sizeof(m.a));
printf("%d\n", sizeof(m.a[4].i));
}

For my surprise.. the output is
Union u
40
400
4
Union M
40
40
4


I'm pretty sure you've just had an encounter with the wonderful world of
undefined behavior! Passing your code to the Sun C Compiler v5.5
results in:

"tmpc.c", line 6: (union) tag redeclared: U

and refuses to compile it in any way, shape, or form. gcc complains:

tmpc.c: In function `unionTest':
tmpc.c:28: union has no member named `i'

and (if I fix that line to let it compile) it outputs "40" for sizeof
(u.a).

In short: don't do that, then.

Cheers,
- jonathan
Nov 14 '05 #2
Jonathan Adams <jw*****@gmail. com> wrote in message news:<jw******* *************** *****@news1nwk. sfbay.sun.com>. ..
In article <88************ **************@ posting.google. com>,
gi***********@r ediffmail.com (Girish) wrote:
I have 2 differesnt defination of same Union as below and a piece of
code for printing size of Union and its members..

union U
{
union U
{
int i;
int j;
}a[10];

int b[10];
}u;

...
void unionTest ()
{
printf("Union u\n");
printf("%d\n", sizeof(u));
printf("%d\n", sizeof(u.a)); // Its the cause for my headache...
printf("%d\n", sizeof(u.a[4].i));
printf("Union M\n");
printf("%d\n", sizeof(m));
printf("%d\n", sizeof(m.a));
printf("%d\n", sizeof(m.a[4].i));
}

For my surprise.. the output is
Union u
40
400
4
Union M
40
40
4


I'm pretty sure you've just had an encounter with the wonderful world of
undefined behavior! Passing your code to the Sun C Compiler v5.5
results in:

"tmpc.c", line 6: (union) tag redeclared: U

and refuses to compile it in any way, shape, or form. gcc complains:

tmpc.c: In function `unionTest':
tmpc.c:28: union has no member named `i'

and (if I fix that line to let it compile) it outputs "40" for sizeof
(u.a).

In short: don't do that, then.

Cheers,
- jonathan

Hi Jonathan,

I have tried the same in Linux with CC compiler and it does not give
me any error for the Union Defination. But in the function, unionTest
(), it gives me compiler error for this statement.

printf("%d\n", sizeof(u.a[4].i));

Error: union has no member named `i'

If I comment out that statement, my code gets comiped and I get proper
output.

So the conclusion is, its a BUG in Windows compiler right?

Thanks a lot
Girish
Nov 14 '05 #3
In article <88************ **************@ posting.google. com>,
gi***********@r ediffmail.com (Girish) wrote:
Jonathan Adams <jw*****@gmail. com> wrote in message
news:<jw******* *************** *****@news1nwk. sfbay.sun.com>. ..
In article <88************ **************@ posting.google. com>,
gi***********@r ediffmail.com (Girish) wrote:
I have 2 differesnt defination of same Union as below and a piece of
code for printing size of Union and its members..

union U
{
union U
{
int i;
int j;
}a[10]; int b[10];
}u;

...
void unionTest ()
{
printf("Union u\n");
printf("%d\n", sizeof(u));
printf("%d\n", sizeof(u.a)); // Its the cause for my headache...
printf("%d\n", sizeof(u.a[4].i));
printf("Union M\n");
printf("%d\n", sizeof(m));
printf("%d\n", sizeof(m.a));
printf("%d\n", sizeof(m.a[4].i));
}

For my surprise.. the output is
Union u
40
400
4
Union M
40
40
4


I'm pretty sure you've just had an encounter with the wonderful world of
undefined behavior! Passing your code to the Sun C Compiler v5.5
results in:

"tmpc.c", line 6: (union) tag redeclared: U

and refuses to compile it in any way, shape, or form. gcc complains:

tmpc.c: In function `unionTest':
tmpc.c:28: union has no member named `i'

and (if I fix that line to let it compile) it outputs "40" for sizeof
(u.a).

In short: don't do that, then.

Cheers,
- jonathan

Hi Jonathan,

I have tried the same in Linux with CC compiler and it does not give
me any error for the Union Defination. But in the function, unionTest
(), it gives me compiler error for this statement.

printf("%d\n", sizeof(u.a[4].i));

Error: union has no member named `i'


Which is exactly what I said above, after "gcc complains:".
If I comment out that statement, my code gets comiped and I get proper
output.

So the conclusion is, its a BUG in Windows compiler right?


No. The bug is in the code which is redefining a union while it is being
defined. Why are you doing that?

- jonathan
Nov 14 '05 #4
Jonathan Adams <jw*****@gmail. com> wrote in message news:<jw******* *************** *****@news1nwk. sfbay.sun.com>. ..
In article <88************ **************@ posting.google. com>,
gi***********@r ediffmail.com (Girish) wrote:
Jonathan Adams <jw*****@gmail. com> wrote in message
news:<jw******* *************** *****@news1nwk. sfbay.sun.com>. ..
In article <88************ **************@ posting.google. com>,
gi***********@r ediffmail.com (Girish) wrote:

> I have 2 differesnt defination of same Union as below and a piece of
> code for printing size of Union and its members..
>
> union U
> {
> union U
> {
> int i;
> int j;
> }a[10]; int b[10]; > }u;
> ... > void unionTest ()
> {
> printf("Union u\n");
> printf("%d\n", sizeof(u));
> printf("%d\n", sizeof(u.a)); // Its the cause for my headache...
> printf("%d\n", sizeof(u.a[4].i));
> printf("Union M\n");
> printf("%d\n", sizeof(m));
> printf("%d\n", sizeof(m.a));
> printf("%d\n", sizeof(m.a[4].i));
> }
>
> For my surprise.. the output is
> Union u
> 40
> 400
> 4
> Union M
> 40
> 40
> 4

I'm pretty sure you've just had an encounter with the wonderful world of
undefined behavior! Passing your code to the Sun C Compiler v5.5
results in:

"tmpc.c", line 6: (union) tag redeclared: U

and refuses to compile it in any way, shape, or form. gcc complains:

tmpc.c: In function `unionTest':
tmpc.c:28: union has no member named `i'

and (if I fix that line to let it compile) it outputs "40" for sizeof
(u.a).

In short: don't do that, then.

Cheers,
- jonathan

Hi Jonathan,

I have tried the same in Linux with CC compiler and it does not give
me any error for the Union Defination. But in the function, unionTest
(), it gives me compiler error for this statement.

printf("%d\n", sizeof(u.a[4].i));

Error: union has no member named `i'


Which is exactly what I said above, after "gcc complains:".
If I comment out that statement, my code gets comiped and I get proper
output.

So the conclusion is, its a BUG in Windows compiler right?


No. The bug is in the code which is redefining a union while it is being
defined. Why are you doing that?

- jonathan


Hi Jonathan,

I was just doing some timepasss RnD while I was bit free and I thought
how different compilers will behave for such codes. (I got different
output for same code in different compiler) and later I started
thinking why it behaves so and could not get any answer :-)

Girish
Nov 14 '05 #5

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

Similar topics

14
9305
by: Bob | last post by:
Hi there, Need a little help with a certain query that's causing a lot of acid in my stomach... Have a table that stores sales measures for a given client. The sales measures are stored per year and there could be multiple sales measures every year per client. There is another field called last update date. If there are multiple sales measures then need to select the one that's been entered last based on this field. Also, if there
5
6334
by: Petr Bravenec | last post by:
I have found that when I use the RETURN NEXT command in recursive function, not all records are returned. The only records I can obtain from function are records from the highest level of recursion. Does exist some work-around? Thanks Petr Bravenec example: create table foo (
12
3744
by: Mikito Harakiri | last post by:
I wonder if WITH RECURSIVE MaryAncestor(anc,desc) AS ( (SELECT parent as anc, child as desc FROM ParentOf WHERE desc = "Mary") UNION (SELECT A1.anc, A2.desc FROM MaryAncestor A1, MaryAncestor A2 WHERE A1.desc = A2.anc) ) SELECT anc FROM MaryAncestor
2
5577
by: Anand Krishnan | last post by:
I have a table that has 3 columns folderid parentfolderid scope ------------------------------ 1 0 1 2 1 0 3 2 0
3
4179
by: Vincenzino | last post by:
Hi, I have some problem in using SQL3 recursive queries on DB2 database system (8.1 and 8.2 UDB). I need to compute the transitive closure of a (possibly) ciclic graph using SQL3 on DB2. I MUST use pure SQL3 and I cannot use cursors, indexes and the like. The graph is represented by a set of arcs with the table "arc" having two attributes "a1" and "a2" for source and target nodes resp. The SQL3 query I'm using is the following:
25
2840
by: Mike MacSween | last post by:
Regular viewers may want to turn off now. This will be an orchestral management system. Musicians and other staff being booked/paid for jobs. A job may contain other jobs, e.g: World Tour contains US leg and Europe leg (and others) US leg contains State tours (and others)
1
2198
by: a_valued_employee | last post by:
My data model has many tables in many hierarchies. I believe that by using recursion, I can check for all invalid codes in the working tables that do not exist in the code tables. For example, I would like to check the EMPLOYEE table for state_codes that do not exist in the STATE_CT table where EMPLOYEE.state_code=STATE_CT.state_code. Another more complex example is ensuring PROJECT.project_code exists in PROJECT_CT where...
0
1962
by: champ1979 | last post by:
I wrote an algorithm to get all the relatives of a person in a family tree. I'm basically getting all the users from the DB and am doing the recursive logic in code, so that there is only 1 call made to the DB. However, I am trying to do the same thing within a stored procedure in SQL using recursive CTEs (I think the performance might be better) but I'm finding it really tough to craft the CTE. I would really appreciate if someone could...
4
3591
by: the6campbells | last post by:
for the conext of this example imagine a simple part table where a part can only have a single parent. try to express a cte where the result set must be balanced. That is, the result set repeats node B and E sufficient times that the tree is as deep as node G. insert into Part ( PartId, PartName, ParentPart) values ( 1,'A',null), (2,'B',1), (3,'C',1), (4,'D',1), (5,'E',3), (6,'F',4), (7,'G',6) The following query will not parse as...
0
9856
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
10911
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
10298
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
9429
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
7833
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
7021
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
5867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4494
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
3136
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.