473,396 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Efficienct structure array

Which of the following definition is more efficient?
struct TestData {
int a;
int b;
int var1[200];
int var2[200];
int var3[200];
int var4[200];
int var5[200];
int var6[200];
int var7[200];
int var8[200];
int var9[200];
};

OR

struct Test1Struct{
int a;
int b;
struct Test2Struct *Test2;
}Test1;

struct Test2Struct {
int var1;
int var2;
int var3;
int var4;
int var5;
int var6;
int var7;
int var8;
int var9;
} Test2[200];
Nov 14 '05 #1
3 6852
Depends on what you mean by efficient. The former may generate more
cache-coherent memory references if you are (e.g.) looping through var9,
while the latter may be better if you're working with the different varX's
with the same index.

That assumes your system has and uses cache, and that the compiler doesn't
step on its toes. But none of this is relevant to c.l.c.
--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 14 '05 #2
On 21 Jan 2004 13:49:00 -0800, fr********@yahoo.com (some one) wrote:
Which of the following definition is more efficient?
struct TestData {
int a;
int b;
int var1[200];
int var2[200];
int var3[200];
int var4[200];
int var5[200];
int var6[200];
int var7[200];
int var8[200];
int var9[200];
};

OR

struct Test1Struct{
int a;
int b;
struct Test2Struct *Test2;
}Test1;

struct Test2Struct {
int var1;
int var2;
int var3;
int var4;
int var5;
int var6;
int var7;
int var8;
int var9;
} Test2[200];


The two are not equivalent. An object of type struct TestData
contains 1802 ints. An object of type struct Test2Struct contains 2
ints and a pointer. The pointer must be initialized to point to a
struct Test2Struct.

If you changed the definition of Test1Struct to
struct Test2Struct Test2[200] /* no asterisk */;
then the two would probably be the same size but you would still have
to type more to reference one of the array elements:
struct TestData td;
struct Test1Struct t1s;
td.var6[5] = 3;
t1s.Test2[5].var6= 3;
<<Remove the del for email>>
Nov 14 '05 #3
The array-of-structures approach is better than the structure-with-arrays
approach from two points of view:

- Array-of-structures is probably closes to the problem domain. You have
a test record structure and an array of several test records is more
readable than the other option.

- Array-of-structures should be more efficent. Consider the case where
you are running a for loop over the entire index range. In each iteration
you just need to get the structure pointer once and then can work
on any entry in that structure.

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - System Architecture Design CASE Tool
Nov 14 '05 #4

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

Similar topics

6
by: Eric Smith | last post by:
Is a structure containing an incomplete array as its last element (per paragraph 2 of section 6.7.2.1 of ISO/IEC 9899:1999 (E)) itself an incomplete type? That appears to be indicated by paragraph...
5
by: Joe Thompson | last post by:
Hi I am new to C# and am rewritting some C++ code. I want to send a byte array over a serial port. The elements of the byte array are really a structure I have populated. My question is, how do...
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
1
by: Jonathan Amend | last post by:
I'm trying to port some C++ code to VB.NET but I have hit a snag. I need to add the pointer of a structure which includes an array to an array and then pass it to an API. Here are the two code...
14
by: Dennis | last post by:
If I have a structure like; Public Structure myStructureDef Public b() as Byte Public t as String End Structure If I pass this structure, will the values in the array b be stored on the...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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,...

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.