473,770 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Data structures question

Hi everyone.
My question is, why are data structures implemented only with struct
data type?
Why not union when it is more efficient as compared with structures?

Thanks in advance

Nov 15 '05 #1
5 1910
Shwetabh <sh**********@g mail.com> wrote:
My question is, why are data structures implemented only with struct
data type?
Why not union when it is more efficient as compared with structures?


Well, depending on what I need to do, I will use which ever one is most
appropriate.

They are not interchangeable , but since you seem to be implying that
they are, I am wondering if you really understand the differences
between them.

Nov 15 '05 #2
Shwetabh wrote on 26/07/05 :
My question is, why are data structures implemented only with struct
data type?
Why not union when it is more efficient as compared with structures?


I don't see what you meant. Just implement the data structures in the
way that seems to be the best for your application. If you want an
union, 'just do it'...
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
Nov 15 '05 #3


Shwetabh wrote:
Hi everyone.
My question is, why are data structures implemented only with struct
data type?
Why not union when it is more efficient as compared with structures?

Thanks in advance


Unions are not more "efficient" than structs, any more than sizeof is
more efficient than strlen(); they are different entities, with
different purposes. You can't replace one with the other.

Nov 15 '05 #4
"John Bode" <jo*******@my-deja.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Shwetabh wrote:
Hi everyone.
My question is, why are data structures implemented only with struct
data type?
Why not union when it is more efficient as compared with structures?

Thanks in advance


Unions are not more "efficient" than structs, any more than sizeof is
more efficient than strlen(); they are different entities, with
different purposes. You can't replace one with the other.


Consider:

struct foo {
enum foo_type t; /* specifies which member of u is valid */
union {
int i;
unsigned u;
/* ... */
} u;
};

In this case, with what I hope is obvious use, you can replace the union
with a struct (without affecting correctness), but doing so increases the
size of struct foo making it less memory efficient.

Alex
Nov 15 '05 #5
On Tue, 26 Jul 2005 12:18:45 -0700, Shwetabh wrote:
Hi everyone.
My question is, why are data structures implemented only with struct
data type?
Why not union when it is more efficient as compared with structures?

Thanks in advance


A union only allows you have have data stored in one of its members at any
particular time. When that is what you need you can use a union. However
most datastructures require data to be stored in more than one member at
ther same time so a union would not work and you would use a structure for
those.

Lawrence
Nov 15 '05 #6

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

Similar topics

1
1595
by: Amit | last post by:
Hello, Can any of you recommend a really good book on data structures and more so, if it relates to STL data structures, and how they are used to build far more complex data structures. Thanks.
4
3862
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system that doesn't offer dynamic memory allocation (to be clear: no malloc, no realloc), and with rather tight memory constraints. Writing my own malloc to do dynamic allocation from some static pool isn't really an option, for various reasons, not...
11
3192
by: theshowmecanuck | last post by:
As a matter of academic interest only, is there a way to programmatically list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to accomplish it. For example: int main void() { while there are more data types { print next data type; }
10
4788
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data structures are read only) Ideally one should be able to put the redundant information there automatically so no mistakes are possible, but in a lot of case I see no way how to do it.
14
8797
by: SD | last post by:
I am thinking about writing a text editor in C for unix sometime soon. I am just doing this to learn more about C. I want to write something like ed.c, a simple line editor. What types of data structures would be appropriate? I am thinking about using a linked list, but I am also wondering whether a tree would be useful. Please give me your ideas...thanks, tilak
13
5257
by: Leszek Taratuta | last post by:
Hello, I have several drop-down lists on my ASP.NET page. I need to keep data sources of these lists in Session State. What would be the most effective method to serialize this kind of data structures? Thanks for any hints, Leszek Taratuta
6
2624
by: James | last post by:
I am using vb.net and need to keep in memory a large data structure, so I am looking for the best option. And after several test I am pretty confused. So I will be grateful if anyone can help me. My basic need is:
11
3778
by: efrat | last post by:
Hello, I'm planning to use Python in order to teach a DSA (data structures and algorithms) course in an academic institute. If you could help out with the following questions, I'd sure appreciate it: 1. What exactly is a Python list? If one writes a, then is the complexity Theta(n)? If this is O(1), then why was the name "list" chosen? If this is indeed Theta(n), then what alternative should be used? (array does not seem suited for...
29
6356
by: Mik0b0 | last post by:
Hallo to everyone. This fall I am going to start data structures as a part of C language course. The problem is I could not find any satisfying tutorial about structures in C. There are plenty of books about data structures in C+ + etc., could anyone please recommend me such a C -specific book ? And another question: are data structures (like stack, structure etc.) used in C++ identical to those in C and is it possible to use C++ books to...
0
9618
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
10260
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
10101
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...
1
10038
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
8933
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
7456
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
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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
2850
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.