473,757 Members | 10,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compile time initialization of data.


typedef unsigned long long int uint64
typedef unsigned char uint8;

Class Simple
{
union { uint64 x; uint8 r[8]; }

public:
Simple(uint64 n) : x(n) {;}
//....
};
Class Simple_user
{
static const Simple simple_array[8];

public:
//....
};

const Simple Simple_user::si mple_array[8] =
{ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
Will the array of simples always be constructed at compile time?
Can an optimizing compiler construct the array at compile time?

Thanks for any answers/comments.

--
Regards,
S.K.Mody
Aug 3 '05 #1
7 2545
S.K.Mody wrote:
typedef unsigned long long int uint64
Not valid C++ (yet). There is no 'long long' type in C++. You
should probably have used 'double'.
typedef unsigned char uint8;

Class Simple
class Simple

, maybe?
{
union { uint64 x; uint8 r[8]; }

public:
Simple(uint64 n) : x(n) {;}
//....
};
Class Simple_user
class Simple_user

, maybe?
{
static const Simple simple_array[8];

public:
//....
};

const Simple Simple_user::si mple_array[8] =
{ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
Will the array of simples always be constructed at compile time?
No. It's unspecified (8.5.1/14).
Can an optimizing compiler construct the array at compile time?


It may. Whether a particular one _can_ or not, depends on its
implementors, doesn't it?

V
Aug 3 '05 #2
Victor Bazarov wrote:
S.K.Mody wrote:
typedef unsigned long long int uint64
Not valid C++ (yet). There is no 'long long' type in C++. You
should probably have used 'double'.


In the actual code, I used the C99 header provided with
glibc (stdint.h) which has nice fixed width types like
uint16_t, uint64_t, int_fast8_t etc, which may make it to C++
eventually. I'll have to use some preprocessor conditionals or
roll my own uint64 if I decide to compile it for other platforms.


Will the array of simples always be constructed at compile time?


No. It's unspecified (8.5.1/14).
Can an optimizing compiler construct the array at compile time?


It may. Whether a particular one _can_ or not, depends on its
implementors, doesn't it?


True, but I am hoping that there is some agreement among compiler
writers as to what constitutes an acceptable level of optimization.
After all, there is technically no requirement for example that
a compiler inline any methods at all, but for some types of code
that would prove to be unacceptable.

I'm using g++ on x86 Linux.
V


--
Regards,
S.K.Mody
Aug 4 '05 #3
S.K.Mody wrote:
[..] I am hoping that there is some agreement among compiler
writers as to what constitutes an acceptable level of optimization.
After all, there is technically no requirement for example that
a compiler inline any methods at all, but for some types of code
that would prove to be unacceptable.
There is no other "agreement" among compiler writers except the
Standard Document, I hope.
I'm using g++ on x86 Linux.


Good for you. It doesn't matter here, though.

V
Aug 4 '05 #4
Victor Bazarov wrote:
S.K.Mody wrote:
[..] I am hoping that there is some agreement among compiler
writers as to what constitutes an acceptable level of optimization.
After all, there is technically no requirement for example that
a compiler inline any methods at all, but for some types of code
that would prove to be unacceptable.
There is no other "agreement" among compiler writers except the
Standard Document, I hope.


Why do you hope? Would there be any problems if a subset
of the set of all compilers behaved somewhat predictably in
some respects even though the exact behaviour is left
unspecified by the standard?

V


--
Regards,
S.K.Mody
Aug 4 '05 #5
S.K.Mody wrote:
Victor Bazarov wrote:
S.K.Mody wrote:
[..] I am hoping that there is some agreement among compiler
writers as to what constitutes an acceptable level of optimization.
After all, there is technically no requirement for example that
a compiler inline any methods at all, but for some types of code
that would prove to be unacceptable.


There is no other "agreement" among compiler writers except the
Standard Document, I hope.


Why do you hope? Would there be any problems if a subset
of the set of all compilers behaved somewhat predictably in
some respects even though the exact behaviour is left
unspecified by the standard?


Yes. The problem is simple: if there is nothing _governing_
the behaviour, nothing is there to prevent it _change_ some
sunny day, and therefore none of it can be _relied upon_. What
else did you expect me to tell you?
Aug 4 '05 #6
Victor Bazarov wrote:
S.K.Mody wrote:
Victor Bazarov wrote:
S.K.Mody wrote:
[..] I am hoping that there is some agreement among compiler
writers as to what constitutes an acceptable level of optimization.
After all, there is technically no requirement for example that
a compiler inline any methods at all, but for some types of code
that would prove to be unacceptable.

There is no other "agreement" among compiler writers except the
Standard Document, I hope.


Why do you hope? Would there be any problems if a subset
of the set of all compilers behaved somewhat predictably in
some respects even though the exact behaviour is left
unspecified by the standard?


Yes. The problem is simple: if there is nothing _governing_
the behaviour, nothing is there to prevent it _change_ some
sunny day, and therefore none of it can be _relied upon_. What
else did you expect me to tell you?


I think the philosophy of C++ provides the governing principle
- To achieve the right balance between portability and efficiency.
For example without inlining, to which the original question is
closely related, it would often be unacceptably inefficient to
have deeply nested calls to small functions. But such calls may
be necessary for a variety of reasons related to good C++ design.
So should one go back to writing macros and forget about design
principles or can one compromise a little and ask for some informal
guarantees from the specific compiler (or class of compilers)
that one may be working with? The latter seems to me to be a better
option - since the choice is unmaintainable spaghetti code v/s
well designed code with some compiler specific preprocessing.

You may regard this as a strictly compiler related question but
it seems to me that the C++ efficiency goals virtually require
the compiler to provide such informal albeit non-portable
guarantees. The original question could therefore be rephrased
as "Is there any sort of uniformity among compilers in this regard?"
I'm not sure whether your answers were based on specific knowledge
of widely varying implementations or on the legal position of the
standard.

--
Regards,
S.K.Mody
Aug 4 '05 #7
S.K.Mody wrote:
I think the philosophy of C++ provides the governing principle
- To achieve the right balance between portability and efficiency.
For example without inlining, to which the original question is
closely related, it would often be unacceptably inefficient to
have deeply nested calls to small functions. But such calls may
be necessary for a variety of reasons related to good C++ design.
uh-huh.
So should one go back to writing macros and forget about design
principles or can one compromise a little and ask for some informal
guarantees from the specific compiler (or class of compilers)
that one may be working with? The latter seems to me to be a better
option - since the choice is unmaintainable spaghetti code v/s
well designed code with some compiler specific preprocessing.
If you know that you will never compile your program on anything other than the compiler you're using today, do what you like. But rest-assured that there are lots of people who
expected their code to not still be around 20 years later. (Such as that in banking systems... remember Y2K? It not like the programmers didn't know about the problem, they just
expected the code to not be around when the problem surfaced).

The point is that one day, somebody might try to re-use the code (I have heard of it happening in the past). They may not expect it to rely upon on-standard behaviour.
You may regard this as a strictly compiler related question but
it seems to me that the C++ efficiency goals virtually require
the compiler to provide such informal albeit non-portable
guarantees.
Not in the slightest. Optimisation is nice, but there is loads of code that relies upon side effects that should not exist, and optimising that code breaks it. Again, reliance
upon non-standard behaviour (such as doing anything in a copy-constructor that is not purely related to copying the object), might break your code some day.
The original question could therefore be rephrased
as "Is there any sort of uniformity among compilers in this regard?"
Yeah, of there is. But thats NOT the question. The question is "Should you rely upon it?"
I'm not sure whether your answers were based on specific knowledge
of widely varying implementations or on the legal position of the
standard.


Probably a bit (or more) of both.

Ben
--
I'm not just a number. To many, I'm known as a String...
Aug 8 '05 #8

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

Similar topics

19
3781
by: Christopher | last post by:
I am getting a parse error from g++ pointing at my catch line and can't figure out whats wrong with this code: #include "BigPosInt.h" #include <iostream> #include <new> #include <assert.h> BigPosInt::BigPosInt(int init_max_digits) {
6
385
by: stef | last post by:
Anyone have any guesses as to why I am getting an error message when I try to compile: In file included from CP.h:13,from driver.cpp:5: cb.h:33: invalid data member initialization cb.h:33: (use `=' to initialize static data members) *** Error code 1 make: Fatal error: Command failed for target `driver.o' line 33 is :
5
2148
by: xuatla | last post by:
Hi, I encountered the following compile error of c++ and hope to get your help. test2.cpp: In member function `CTest CTest::operator+=(CTest&)': test2.cpp:79: error: no match for 'operator=' in '*this = CTest::operator+(CTest&)((+t2))' test2.cpp:49: error: candidates are: CTest CTest::operator=(CTest&) make: *** Error 1
11
387
by: babuyama | last post by:
Hi, Is there a way to obtain library name at compile/preprocessor time? Assuming that the compilation unit, myfile.c is part of mylib.a, from myfile.c code at compile/preprocessor time, I would like to know that the library name is "mylib". I could not find an equivalent of __FUNCTION__, __FILE__ defines for getting the library name. Is there another way? Thanks, Babu
16
10532
by: John Kelsey | last post by:
Back in the "old" days with C, I used to do something like... struct { char Description; float price; } Items = { {"Apple", 1.99}, {"Banana", 2.04}
5
2228
by: Arno | last post by:
Hello, here is my problem: I have a complex data type, like a std::vector< std::string > and need some global constant values of this type. The type is also used non-constant throughout the program, so I do not want to use dumb types, but rather STL things. In the best of worlds, initialization of these constants would not happen at runtime, but at compile time, essentially generating a constant memory image of this type via...
22
3612
by: Tomás Ó hÉilidhe | last post by:
I've been developing a C89 microcontroller application for a while now and I've been testing its compilation using gcc. I've gotten zero errors and zero warnings with gcc, but now that I've moved over to the micrcontroller compiler I'm getting all sorts of errors. One thing I'd like to clarify is the need (in C89) for a compile- time constant in the initialiser of a variable. The compiler rejects the following source file: /* Start...
11
1773
by: Tim H | last post by:
The following program compiles just fine in C, but not in C++. Can anyone explain why? I have a chunk of code that defines stuff like this in headers (without the extern) that I can not easily change. The C compiler recognizes the first foo and second foo as the same. The C++ compiler not so much. Is there a way to get this to compile in C++ without changing all the headers?
2
6660
myusernotyours
by: myusernotyours | last post by:
Hi All, Am working on a Java application in which I have to use the JNI to Interface with some native code for both windows and unix. Am using netbeans IDE with the C/C++ pack installed. Am also using Cygwin as my compiler (gcc), this is ostensibly because I hope this compiler will also compile the unix native libraries since I don't have a Linux installation. (I am working on a personal project from the office and can't get linux installed)....
0
9489
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
9298
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
9906
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
9885
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
9737
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
7286
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
5172
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...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
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 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.