473,804 Members | 3,446 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

storing arrays in cpp

I have a public class Globals, which obviously holds all my global data.

I have an array of 243 items, currently structs of type

typedef struct STAR
{
int x;
int y;
int stellar_class;
}t_star;

t_star StarArray[243]={
{.....},
};
If I was doing this app in C I would just have the array as a seperate
C source file, compile it in, and have extern t_star StarArray[] where
ever I needed it.

What's the correct way to do this in cpp?

having global data is frowned upon, you can't put it in the class
header as this is against ansi conventions....

Are you supposed to define the empty array in the class header then
fill it in the class creation tool? What a waste of stack space....

puzzled...

--

Jan 31 '06
14 4952
I guess that would work too.

I'm assuming you're writing this for an RTOS since you're worried about
overhead. The compiler (well..gcc/gpp) will only include those
functions that you're using with std::list, which normally you would
write yourself for a linked list in C.

But, you still want an array :). I'm going to stop pushing it because
there is quite a bit of overhead compared with arrays.

Ok, I just read your replies to the other guys. Obviously for embedded
systems. I guess my next question is why c++ when you're using C fine?
I think for what you're trying to do, a class isn't the answer anyway.
I haven't been working in the "real world" for long, but my roots are
in C programming, so I remember thinking stacks, bitwise, memory
allocation on my 16bit motorolas, etc...

When I learned C++ I was scared everyday about overhead that it was
creating.

Basically, I use a struct now when I want a very basic structure, which
no methods belonging to it. I use a class when there are routines that
this class has, and that can be inherited or derived from other
classes...gah, I'm losing it.

Uh, for your example I would use a struct, and not even worry about
classes. If there is some abstraction that requires you to include
this struct in a greater combonation of variables, then, the struct is
part of the class, but don't replace it :). You can then use a global
array of that class member, with minimal changes to your code.

(very winded, sorry) Lastly, I think that your coding style is fine for
the application. If my assumption is correct, then there is no reason
that I will frown seeing some global variables. I mean, the compilier
allows them for a reason, right? To answer your original question, the
correct way is the way you can get it to work with your requirements in
your application while sticking to your employer's coding standards. I
would personally change very little with your code when porting from c
to c++.

Feb 1 '06 #11
>class Globals
{
public:
static t_star StarArray[];
};
----------------------------------------------------------------

// Globals.cpp
t_star Globals::StarAr ray[243] = { ... }; // Initialise the array


Thank you, this worked.

To me this was counter intuative, I would have expected it to be thrown
out by the compiler syaing something like

"StarArray already defined "

I knew there had to be a work around, but probably wouldn't have found
it by experimentation .

Feb 2 '06 #12
Ok, I just read your replies to the other guys. Obviously for
embedded systems. I guess my next question is why c++ when you're
using C fine?
I am writing portable code that will run on several target OS's.

Thus it has to be c++, as some of the target OS's don't have C
compilers.

The hassle is that all the OS's have different problems, in this case
all the global data for the app has to be in a single holder (could be
a struct but it is far more convenient to be able to have the attached
methods as well). This is for the Symbion port. In there "wisdom" they
decided to make global data illegal for app's. You get a single pointer
which they call "thread local storage", and that's it.

When I learned C++ I was scared everyday about overhead that it was
creating.


For the last, well at least ten years, I have been noticing a trend of
bloated code being prefered to clean code. As an example, when I was
first tasked with evaluating Symbion as a target platform we had a
device on the market, sold tens of millions of units, everybody was
happy with it, and it contained 1Meg of flash and 250K of ram.

Symbion's basic spec required a MINIMUM of 5Meg ram and 5Meg flash.

When you consider that at the time the 1Meg flash comprised almost 25%
of the materials cost for the device, we were looking at a major cost
increase.

I despise working on Symbion now, they have tried to be everything to
everybody, and ended up with a complete mess.

Feb 2 '06 #13
Stainless wrote:
class Globals
{
public:
static t_star StarArray[];
//Declaration
};
----------------------------------------------------------------

// Globals.cpp
t_star Globals::StarAr ray[243] = { ... }; // Initialise the array

//Definition

Thank you, this worked.

To me this was counter intuative, I would have expected it to be thrown
out by the compiler syaing something like

"StarArray already defined "


It's only defined once and the declaration is compatible.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 2 '06 #14

Stainless wrote:
Ok, I just read your replies to the other guys. Obviously for
embedded systems. I guess my next question is why c++ when you're
using C fine?


I am writing portable code that will run on several target OS's.

Thus it has to be c++, as some of the target OS's don't have C
compilers.

The hassle is that all the OS's have different problems, in this case
all the global data for the app has to be in a single holder (could be
a struct but it is far more convenient to be able to have the attached
methods as well). This is for the Symbion port. In there "wisdom" they
decided to make global data illegal for app's. You get a single pointer
which they call "thread local storage", and that's it.


So this thread local storage is your array? Is it dynamic or like a
buffer? It's actually a pointer, so what is the max mem allocation you
are allowed for it?

I can understand where you're coming from. I would probably use the
Linux v. Windows comparison as an analagy. When looking for Linux
programs, you search for a couple things:
1. How you want to compile it (be it rpm, Makefile, gcc, etc)
2. What platform you're running
3. What options you want.

When installing, you may be missing some libraries!

Where, in Windows, it's all there for you (most of the time), even
though it hardly ever works 100%.

I think customers are Windows, and Programmers/Engineers are Linux.
They didn't mix in the 1950s(people, not OSs), and as much as our
marketing departments wish, it's a huge hurdle to get them to do that
today.
When I learned C++ I was scared everyday about overhead that it was
creating.


For the last, well at least ten years, I have been noticing a trend of
bloated code being prefered to clean code. As an example, when I was
first tasked with evaluating Symbion as a target platform we had a
device on the market, sold tens of millions of units, everybody was
happy with it, and it contained 1Meg of flash and 250K of ram.


Thank God I have a giant brain and use code ACAP (as clean as
possible). I'm joking, but we did learn to consider your registers,
cache requirements, memory allocation/leaks, etc when writing any level
of code in college. The more stuff I see, though, the more I pull my
hair out wondering what I'm really supposed to be doing! (I graduated
only a short time ago).

I will not continue, because my skills are far inferior and anything I
have to say will be wasting people's time.
-ben

Feb 2 '06 #15

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

Similar topics

1
1429
by: nt9 | last post by:
I am very new with php so excuse me please if the question seems really easy.I have a page(form) which displays a list of the titles of some books(retreived from a database) and each title has a selection box with option values 1 to 7 each.The user should be able to enter 7 choices of his favourite books and store them in the database at a table called preferences with fields: customer_id ,first_choice, sec_choice,third_choice etc.I am...
2
2034
by: Mark Hannon | last post by:
I am trying to wrap my brain around storing form elements inside variables & arrays before I move on to a more complicated project. I created this simple example to experiment and as far as I can tell, it should work but it doesn't. Can someone tell me where I went wrong? <html><head> <title>Form Test</title> <script language="JavaScript> <!-- function copy(){
22
2465
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a nightmare. There are of course many solutions, but they all end up forcing you to abandon the array syntax in favour of macros or functions. Now I have two questions - one is historical, and the other practical. 1.) Surely malloc (and...
9
1303
by: Crirus | last post by:
I have ~200 players on a server... They share the same 512x512 tiles map... Each player have an explored area and a visible area... visible area is given by players's current units and buildings... Now, I'm facing with the problem of how to store does areas... One way is to keep a explored(512,512) array of booleans... each explored(i,j)=True mean player explored the tile on (i.j)... the same for visible...
2
1984
by: Jacek Dziedzic | last post by:
Hello! Suppose I have a class that contains only public members of builtin types and a default constructor. Because of the default constructor it is no longer an aggregate and therefore no longer POD, according to my understanding of http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/doc/POD.html I need to be able to serialize this class to/from a binary file
20
4655
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
2
7055
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs, input box for units of service, description of the service and each row has its own dropdown list of unit fees that apply. Each dynamically created row will return 3 values fee1_choice, fee1_unit and fee1_money. Note The above informaton is...
13
2150
by: stephen b | last post by:
(apologies for cross posting from the moderated group..i'm sure you understand) Hello, I'm passing an array into a Constructor and hoping to use it as a pointer and store it as a class member for future use. So far, I'm just causing crashes, psuedo code below: double block; foo = MyClass(block);
10
2314
by: deciacco | last post by:
I'm writing a command line utility to move some files. I'm dealing with thousands of files and I was wondering if anyone had any suggestions. This is what I have currently: $arrayVirtualFile = array( 'filename'=>'filename', 'basename'=>'filename.ext', 'extension'=>'ext', 'size'=>0,
0
9707
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
9585
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
10338
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
10323
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
10082
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...
1
7622
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
6856
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
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.