Lets say I have this structure:
typedef struct numbers {
double first = 0.0;
double second = 0.0;
double third = 0.0;
} MYVALUES;
and I initialize an array of MYVALUES like the following
MYVALUES * foo;
foo = new MYVALUES [3];
I would expect the following initial values from foo:
foo[0].first == 0.0
foo[0].second == 0.0
foo[0].third == 0.0
foo[1].first == 0.0
foo[1].second == 0.0
foo[1].third == 0.0
foo[2].first == 0.0
foo[2].second == 0.0
foo[2].third == 0.0
My question is, how does the compiler initialize these values? Does it
run a loop to initialize foo kinda like this?
for (i=0; i<3; i++){
foo[i].first = 0.0;
foo[i].second = 0.0;
foo[i].third = 0.0;
}
I can't have it run a loop because my array can potentially be very
large and i don't want it to take up processing time.
Thanks
Eric 3 6252
* vduber6er:
Lets say I have this structure:
typedef struct numbers {
double first = 0.0;
double second = 0.0;
double third = 0.0;
} MYVALUES;
Don't use all uppercase except for macro names, where you should.
Don't use typedef, it's a C'ism.
Don't initialize in member declarations -- it shouldn't even compile
(have you even /tried/ to compile the code?).
In C++ write just
struct numbers
{
double first, second, third;
numbers(): first(0), second(0), third(0) {}
};
You can even omit the explicit 0's, since that's the default.
and I initialize an array of MYVALUES like the following
MYVALUES * foo;
foo = new MYVALUES [3];
I would expect the following initial values from foo:
foo[0].first == 0.0
foo[0].second == 0.0
foo[0].third == 0.0
foo[1].first == 0.0
foo[1].second == 0.0
foo[1].third == 0.0
foo[2].first == 0.0
foo[2].second == 0.0
foo[2].third == 0.0
With numbers declared as shown above that is indeed the result, because
that's what's specified in the numbers default constructor.
My question is, how does the compiler initialize these values? Does it
run a loop to initialize foo kinda like this?
for (i=0; i<3; i++){
foo[i].first = 0.0;
foo[i].second = 0.0;
foo[i].third = 0.0;
}
Yes.
I can't have it run a loop because my array can potentially be very
large and i don't want it to take up processing time.
No matter how it's done there will be a loop: there's no magic.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Thanks Alf. Yeah I realized my structure was flawed after I wrote it
down and was actually going to edit my post, but you beat me to it.
Thanks for the answer. I guess there is no way to avoid a loop.
Eric
Alf P. Steinbach wrote:
* vduber6er:
Lets say I have this structure:
typedef struct numbers {
double first = 0.0;
double second = 0.0;
double third = 0.0;
} MYVALUES;
Don't use all uppercase except for macro names, where you should.
Don't use typedef, it's a C'ism.
Don't initialize in member declarations -- it shouldn't even compile
(have you even /tried/ to compile the code?).
In C++ write just
struct numbers
{
double first, second, third;
numbers(): first(0), second(0), third(0) {}
};
You can even omit the explicit 0's, since that's the default.
and I initialize an array of MYVALUES like the following
MYVALUES * foo;
foo = new MYVALUES [3];
I would expect the following initial values from foo:
foo[0].first == 0.0
foo[0].second == 0.0
foo[0].third == 0.0
foo[1].first == 0.0
foo[1].second == 0.0
foo[1].third == 0.0
foo[2].first == 0.0
foo[2].second == 0.0
foo[2].third == 0.0
With numbers declared as shown above that is indeed the result, because
that's what's specified in the numbers default constructor.
My question is, how does the compiler initialize these values? Does it
run a loop to initialize foo kinda like this?
for (i=0; i<3; i++){
foo[i].first = 0.0;
foo[i].second = 0.0;
foo[i].third = 0.0;
}
Yes.
I can't have it run a loop because my array can potentially be very
large and i don't want it to take up processing time.
No matter how it's done there will be a loop: there's no magic.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
vduber6er posted:
typedef struct numbers {
double first = 0.0;
double second = 0.0;
double third = 0.0;
} MYVALUES;
and I initialize an array of MYVALUES like the following
MYVALUES * foo;
foo = new MYVALUES [3];
I would expect the following initial values from foo:
foo[0].first == 0.0
foo[0].second == 0.0
foo[0].third == 0.0
foo[1].first == 0.0
foo[1].second == 0.0
foo[1].third == 0.0
foo[2].first == 0.0
foo[2].second == 0.0
foo[2].third == 0.0
You can go with Alf's solution, but I prefer:
struct Whatever {
double first,second,th ird;
};
Whatever *const p = new Whatever[3]();
The parentheses behind the final semi-colon indicate that the array is to
be default-initialised.
--
Frederick Gotham This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Neil Zanella |
last post by:
Hello,
When an "std::map<int, int> foobar;" statement appears in a C++ program
followed by a statement of the form "foobar;" where nothing has ever
been inserted at position 123 into map foobar, the C++ standard states
that a new element is created at position 123 and is initialized to
zero. That is, in this case, the pair (123, 0) is inserted into the
map.
I am interested in knowing what the standard says should happen in the
|
by: simondex |
last post by:
Hi, Everyone!
Does anyone know how to initialize an int array with a non-zero number?
Thank You Very Much.
Truly Yours, Simon Dexter
|
by: fix |
last post by:
Hi all,
I feel unclear about what my code is doing, although it works but I am
not sure if there is any possible bug, please help me to verify it.
This is a trie node (just similar to tree nodes) struct, I am storing an
array of 27 pointers and a void pointer that can point to anything.
typedef struct trieNode
{
struct trieNode *children; // The children nodes
void *obj; // The object stored
} TrieNode;
|
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.
|
by: Paminu |
last post by:
In the following code I am trying to initialize a pointer that is located in
a struct.
#include <stdlib.h>
#include <stdio.h>
#define KIDS 4
typedef struct test
{
void *content;
| |
by: jimmij |
last post by:
Hi,
Please look at the code bellow
/*******************/
class ctab
{
private:
static const unsigned n=48;
|
by: newsposter0123 |
last post by:
The code block below initialized a r/w variable (usually .bss) to the
value of pi. One, of many, problem is any linked compilation unit may
change the global variable. Adjusting
// rodata
const long double const_pi=0.0;
lines to
// rodata
|
by: Mik0b0 |
last post by:
Hallo.
Let's say, there is a structure
struct struct10{
int a1;
int a2;
int a3;
int a4;
}count={
{10,20,30,40},
{50,60,70,80}
|
by: Jason Doucette |
last post by:
Situation:
I have a simple struct that, say, holds a color (R, G, and B). I
created my own constructors to ease its creation. As a result, I lose
the default constructor. I dislike this, but it's easy to solve: I
just make my own default constructor.
Problem:
My own default constructor is considered to be *initializing the
variable* (even though it doesn't), whereas the original one does
not. Thus, when I declare and use it before...
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |