By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,130 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,130 IT Pros & Developers. It's quick & easy.

Default parameter for member array?

P: n/a
As far as I know, there is no way to provide a default value for the
argument to the constructor A::A(char (&array)[16]) in this example.
Correct?

struct A{
A(char (&array)[16] ){
std::copy(&array[0],&array[16],&_array[0]);
}
char _array[16];
};

int main() {
char arr[16] = {0};
A a(arr);
std::cout<<a._array<<std::endl;
}

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Aug 6 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
* Steven T. Hatton:
As far as I know, there is no way to provide a default value for the
argument to the constructor A::A(char (&array)[16]) in this example.
Correct?

char z[16] = {};

struct A{
A(char (&array)[16] ){
A( char (&array)[16] = z )

std::copy(&array[0],&array[16],&_array[0]);
You know, names that differ only in an underscore are not visually
distinctive.

}
char _array[16];
};

int main() {
char arr[16] = {0};
A a(arr);
std::cout<<a._array<<std::endl;
}


int main() {
A a;
std::cout<<a._array<<std::endl;
}

--
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?
Aug 6 '05 #2

P: n/a
Alf P. Steinbach wrote:
* Steven T. Hatton:
As far as I know, there is no way to provide a default value for the
argument to the constructor A::A(char (&array)[16]) in this example.
Correct?

char z[16] = {};


Too bad I can't have that as the default operation performed on a POD class
when it's constructed. AFAIK, I can do this.

struct C { int _a; int _b; };

C c = {};

To reliably zero-initialize all the members of a POD class. But I don't see
a way to use that for the default constructor. Writing out all the members
in the initialization list can be tedious.

struct A{
A(char (&array)[16] ){


A( char (&array)[16] = z )

std::copy(&array[0],&array[16],&_array[0]);


You know, names that differ only in an underscore are not visually
distinctive.


Actually, I was being sloppy. Typically I will do this:

struct A{
A(char (&array_)[16] )
{std::copy(&array_[0],&array[16]_,&_array[0]);}
char _array[16];
};

Which in this case doesn't help a lot because the syntax is so busy. In
general, however, since I frequently use _member for the member
declaration, and member_ for a parameter, I'm conditioned to look for them.
It works fairly well. The only symbol that really causes me much problem
is '#'.
}
char _array[16];
};

int main() {
char arr[16] = {0};
A a(arr);
std::cout<<a._array<<std::endl;
}


int main() {
A a;
std::cout<<a._array<<std::endl;
}


Interestingly, if the parameter is not const it won't accept a const char[]
for z, but as far as I can tell, z and A::_array are two distinct objects,
so z would never be modified as a result of that initialization. Typically
the parameter to the constructor should probably be const anyway. I also
found that I can make the default initializer a static member, which
satisfies my desire for proper containment.

This code show some behavior that was not obvious from looking at the
syntax.

#include <iostream>
#include <string>
std::string s("surprise!");

class A {
static const char s_Z[16];
public:
A(const char (&array_)[16] = s_Z ) {
std::copy(&array_[0],&array_[16],&_array[0]);
}
void surprise() { std::copy(&s[0],&s[s.size()],&_array[0]); }
char _array[16];
};

const char A::s_Z[16] = {'1','2','3','4','5','6','7','8','9'
,'A','B','C','D','E','F','\0'};

char z[16] = {};

struct B {
B( const char (&array_)[16] = z ) {
std::copy(&array_[0],&array_[16],&_array[0]);
}
char _array[16];
};

int main() {
char arr[16] = {'1','2','3','4','5','6','7','8','9'
,'a','b','c','d','e','f','\0'};
A a(arr);
A aa;
std::cout<<a._array<<std::endl;
a.surprise();
std::cout<<aa._array<<std::endl;
std::cout<<a._array<<std::endl;
std::cout<<z<<std::endl;
B b;
std::copy(&s[0],&s[s.size()],&z[0]);
B bb;
std::cout<<b._array<<std::endl;
std::cout<<bb._array<<std::endl;
}

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Aug 7 '05 #3

P: n/a
* Steven T. Hatton:
[someething]


?

--
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?
Aug 7 '05 #4

P: n/a
Alf P. Steinbach wrote:
* Steven T. Hatton:
[someething]


?


This sucks!

struct Elf32_Ehdr {
Elf32_Ehdr(unsigned char (&e_ident_)[EI_NIDENT] = sc_e_ident
,Elf32_Half e_type_ = 0
,Elf32_Half e_machine_ = 0
,Elf32_Word e_version_ = 0
,Elf32_Addr e_entry_ = 0
,Elf32_Off e_phoff_ = 0
,Elf32_Off e_shoff_ = 0
,Elf32_Word e_flags_ = 0
,Elf32_Half e_ehsize_ = 0
,Elf32_Half e_phentsize_ = 0
,Elf32_Half e_phnum_ = 0
,Elf32_Half e_shentsize_ = 0
,Elf32_Half e_shnum_ = 0
,Elf32_Half e_shstrndx_ = 0)
:e_type(e_type_)
,e_machine(e_machine_)
,e_version(e_version_)
,e_entry(e_entry_)
,e_phoff(e_phoff_)
,e_shoff(e_shoff_)
,e_flags(e_flags_)
,e_ehsize(e_ehsize_)
,e_phentsize(e_phentsize_)
,e_phnum(e_phnum_)
,e_shentsize(e_shentsize_)
,e_shnum(e_shnum_)
,e_shstrndx(e_shstrndx_)
{ std::copy(&e_ident_[0],&e_ident_[EI_NIDENT],&e_ident[0]); }

static const char sc_e_ident[EI_NIDENT];//for initialization. See
Types.cpp.

unsigned char e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
};
#include <sth/elf32/Types.h>

namespace sth {
namespace elf32 {
const char Elf32_Ehdr::sc_e_ident[EI_NIDENT]={};
}
}

Yes, I know I can do Elf32_Ehdr hdr = {};, but I want that to happen
automatically.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Aug 7 '05 #5

P: n/a
* Steven T. Hatton:
Yes, I know I can do Elf32_Ehdr hdr = {};, but I want that to happen
automatically.


Still not sure what you mean.

Is your question: how do I zero a POD member in a constructor initializer
list?

In that case, for POD member m and class C, "C(): m() {}".

--
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?
Aug 7 '05 #6

P: n/a
Alf P. Steinbach wrote:
* Steven T. Hatton:
Yes, I know I can do Elf32_Ehdr hdr = {};, but I want that to happen
automatically.


Still not sure what you mean.

Is your question: how do I zero a POD member in a constructor initializer
list?

In that case, for POD member m and class C, "C(): m() {}".


No, that wasn't the question. But that certainly is cleaner than what I
showed. I just figured if I was going to bother writing the initializers,
I might as well make them more useful. My question is how do I accomplish
what the following does, but with something as simple as Elf32_Ehdr eh={}:

// ELF file header
struct Elf32_Ehdr {
Elf32_Ehdr():e_type ()
,e_machine ()
,e_version ()
,e_entry ()
,e_phoff ()
,e_shoff ()
,e_flags ()
,e_ehsize ()
,e_phentsize()
,e_phnum ()
,e_shentsize()
,e_shnum ()
,e_shstrndx ()
{}
unsigned char e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
};

Actually, what's above is probably the closest you can get to what I want.
It seems like it would be relatively trivial to provide some syntactic
legerdemain to get the compiler to zero initialize all members by default
with one or two characters.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Aug 7 '05 #7

P: n/a
* Steven T. Hatton:
Alf P. Steinbach wrote:
* Steven T. Hatton:
Yes, I know I can do Elf32_Ehdr hdr = {};, but I want that to happen
automatically.


Still not sure what you mean.

Is your question: how do I zero a POD member in a constructor initializer
list?

In that case, for POD member m and class C, "C(): m() {}".


No, that wasn't the question. But that certainly is cleaner than what I
showed. I just figured if I was going to bother writing the initializers,
I might as well make them more useful. My question is how do I accomplish
what the following does, but with something as simple as Elf32_Ehdr eh={}:

// ELF file header
struct Elf32_Ehdr {
Elf32_Ehdr():e_type ()
,e_machine ()
,e_version ()
,e_entry ()
,e_phoff ()
,e_shoff ()
,e_flags ()
,e_ehsize ()
,e_phentsize()
,e_phnum ()
,e_shentsize()
,e_shnum ()
,e_shstrndx ()
{}
unsigned char e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
};


struct Elf32: Elf32_Ehdr{ Elf32(): Elf32_Ehdr() {} };

OK?

--
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?
Aug 7 '05 #8

P: n/a
Steven T. Hatton sade:
Actually, what's above is probably the closest you can get to what I want.
It seems like it would be relatively trivial to provide some syntactic
legerdemain to get the compiler to zero initialize all members by default
with one or two characters.


Doesn't the compiler zero-initialize (as a part of default-initializing)
every member for which no other intializer is specified?

Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Aug 7 '05 #9

P: n/a
* Tobias Blomkvist:
Steven T. Hatton sade:
Actually, what's above is probably the closest you can get to what I want.
It seems like it would be relatively trivial to provide some syntactic
legerdemain to get the compiler to zero initialize all members by default
with one or two characters.


Doesn't the compiler zero-initialize (as a part of default-initializing)
every member for which no other intializer is specified?


Short answer: No.

Longer answer: it depends on how and where the instance is created, e.g.

auto T o;

will not initialize o when T is a POD.

Even longer answer: the rules in C++98 were not properly implemented by all
compilers, and in C++03 new rules (value-initialization) were added, so it's
rather complex; it's not a good idea to count on the standard's rules here.

--
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?
Aug 7 '05 #10

P: n/a
Alf P. Steinbach wrote:
* Steven T. Hatton:
Alf P. Steinbach wrote:
> * Steven T. Hatton:
>> Yes, I know I can do Elf32_Ehdr hdr = {};, but I want that to happen
>> automatically.
>
> Still not sure what you mean.
>
> Is your question: how do I zero a POD member in a constructor
> initializer list?
>
> In that case, for POD member m and class C, "C(): m() {}".


No, that wasn't the question. But that certainly is cleaner than what I
showed. I just figured if I was going to bother writing the
initializers,
I might as well make them more useful. My question is how do I
accomplish what the following does, but with something as simple as
Elf32_Ehdr eh={}:

// ELF file header
struct Elf32_Ehdr {
Elf32_Ehdr():e_type ()
,e_machine ()
,e_version ()
,e_entry ()
,e_phoff ()
,e_shoff ()
,e_flags ()
,e_ehsize ()
,e_phentsize()
,e_phnum ()
,e_shentsize()
,e_shnum ()
,e_shstrndx ()
{}
unsigned char e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
};


struct Elf32: Elf32_Ehdr{ Elf32(): Elf32_Ehdr() {} };

OK?

That only works if I've already defined Elf32_Ehdr as shown above, correct?
IOW, it requires the member initialization list. I can and will experiment
with it, but that won't tell me what the Standard actually specifies.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Aug 8 '05 #11

P: n/a
* Steven T. Hatton:
struct Elf32: Elf32_Ehdr{ Elf32(): Elf32_Ehdr() {} };

OK?

That only works if I've already defined Elf32_Ehdr as shown above, correct?


No, it works if you _have not_ defined a constructor.

--
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?
Aug 8 '05 #12

P: n/a
Alf P. Steinbach wrote:
* Steven T. Hatton:
> struct Elf32: Elf32_Ehdr{ Elf32(): Elf32_Ehdr() {} };
>
> OK?
>

That only works if I've already defined Elf32_Ehdr as shown above,
correct?


No, it works if you _have not_ defined a constructor.


Are you saying my compiler's wrong? Can you provide clause and paragraph?
I'd like to run this past the GCC folks.

/home/hattons/code/c++/scratch/zero-initialize $ cat main.cpp
#include <iostream>

// Platform specific definitions.
typedef unsigned long Elf32_Addr;
typedef unsigned short Elf32_Half;
typedef unsigned long Elf32_Off;
typedef signed long Elf32_Sword;
typedef unsigned long Elf32_Word;

const unsigned EI_NIDENT(16);
struct Elf32_Ehdr_base {
unsigned char e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
};
// ELF file header
struct Elf32_Ehdr: Elf32_Ehdr_base
{ Elf32_Ehdr():Elf32_Ehdr_base(){} };

int main(){
Elf32_Ehdr eh;
for(size_t i = 0; i < EI_NIDENT; ++i){ std::cout<<int(eh.e_ident[i])<<"
"; }
std::cout<<"\n"
<<eh.e_type<<"\n"
<<eh.e_machine<<"\n"
<<eh.e_version<<"\n"
<<eh.e_entry<<"\n"
<<eh.e_phoff<<"\n"
<<eh.e_shoff<<"\n"
<<eh.e_flags<<"\n"
<<eh.e_ehsize<<"\n"
<<eh.e_phentsize<<"\n"
<<eh.e_phnum<<"\n"
<<eh.e_shentsize<<"\n"
<<eh.e_shnum<<"\n"
<<eh.e_shstrndx<<std::endl;
}
/home/hattons/code/c++/scratch/zero-initialize $ g++ -ozero-init main.cpp
/home/hattons/code/c++/scratch/zero-initialize $ ./zero-init
46 133 4 8 16 42 15 64 136 207 255 191 217 139 4 8
0
0
134520288
3221213080
134514289
1074749732
1074735632
53176
49151
35713
2052
28660
16385

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Aug 9 '05 #13

P: n/a
Steven T. Hatton wrote:

[...]

This seems to work, and isn't too terribly convoluted.

#include <iostream>

// Platform specific definitions.
typedef unsigned long Elf32_Addr;
typedef unsigned short Elf32_Half;
typedef unsigned long Elf32_Off;
typedef signed long Elf32_Sword;
typedef unsigned long Elf32_Word;

const unsigned EI_NIDENT(16);
struct Elf32_Ehdr_base {
unsigned char e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
static const Elf32_Ehdr_base Z;
};
const Elf32_Ehdr_base Elf32_Ehdr_base::Z = {};
// ELF file header
struct Elf32_Ehdr: Elf32_Ehdr_base
{ Elf32_Ehdr():Elf32_Ehdr_base(Elf32_Ehdr::Z){} };

int main(){
Elf32_Ehdr eh;
for(size_t i = 0; i < EI_NIDENT; ++i){ std::cout<<int(eh.e_ident[i])<<"
"; }
std::cout<<"\n"
<<eh.e_type<<"\n"
<<eh.e_machine<<"\n"
<<eh.e_version<<"\n"
<<eh.e_entry<<"\n"
<<eh.e_phoff<<"\n"
<<eh.e_shoff<<"\n"
<<eh.e_flags<<"\n"
<<eh.e_ehsize<<"\n"
<<eh.e_phentsize<<"\n"
<<eh.e_phnum<<"\n"
<<eh.e_shentsize<<"\n"
<<eh.e_shnum<<"\n"
<<eh.e_shstrndx<<std::endl;
}

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Aug 9 '05 #14

P: n/a
* Steven T. Hatton:
Alf P. Steinbach wrote:
* Steven T. Hatton:
> struct Elf32: Elf32_Ehdr{ Elf32(): Elf32_Ehdr() {} };
>
> OK?
>
That only works if I've already defined Elf32_Ehdr as shown above,
correct?
No, it works if you _have not_ defined a constructor.


Are you saying my compiler's wrong?


Assuming your example (snipped) is correct your compiler is indeed
non-conforming.

Can you provide clause and paragraph?
In C++98, first 12.6.2/3 "if the expression list of the mem-initializer is
omitted, the base class or member subobject is default-initialized", then
8.5/5, for your case, a POD, "the storage for the object is
zero-initialized". In C++03 I guess there might be a small detour via
"value-initialized". Anyway, the effect is that all members are zeroed.

I'd like to run this past the GCC folks.


First make sure you have the latest version of the compiler, and that that
is indeed the compiler you actually used. But if so, then yes, good idea.
They have a special web-page for reporting such things -- and it works.

[snipped example]

--
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?
Aug 9 '05 #15

P: n/a
Steven T. Hatton wrote:
Steven T. Hatton wrote:

[...]

This seems to work, and isn't too terribly convoluted.

#include <iostream>

// Platform specific definitions.
typedef unsigned long Elf32_Addr;
typedef unsigned short Elf32_Half;
typedef unsigned long Elf32_Off;
typedef signed long Elf32_Sword;
typedef unsigned long Elf32_Word;

const unsigned EI_NIDENT(16);
struct Elf32_Ehdr_base {
unsigned char e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
static const Elf32_Ehdr_base Z;
};
const Elf32_Ehdr_base Elf32_Ehdr_base::Z = {};
// ELF file header
struct Elf32_Ehdr: Elf32_Ehdr_base
{ Elf32_Ehdr():Elf32_Ehdr_base(Elf32_Ehdr::Z){} };

int main(){
Elf32_Ehdr eh;
for(size_t i = 0; i < EI_NIDENT; ++i){ std::cout<<int(eh.e_ident[i])<<"
"; }
std::cout<<"\n"
<<eh.e_type<<"\n"
<<eh.e_machine<<"\n"
<<eh.e_version<<"\n"
<<eh.e_entry<<"\n"
<<eh.e_phoff<<"\n"
<<eh.e_shoff<<"\n"
<<eh.e_flags<<"\n"
<<eh.e_ehsize<<"\n"
<<eh.e_phentsize<<"\n"
<<eh.e_phnum<<"\n"
<<eh.e_shentsize<<"\n"
<<eh.e_shnum<<"\n"
<<eh.e_shstrndx<<std::endl;
}


HOWEVER..., when I g++ -o test *.cpp -W I get a zillion warnings such as
"missing initializer for member ?sth::elf32::Elf32_Ehdr_base::e_ident?". Is
my compiler right or wrong on this one?
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Aug 9 '05 #16

P: n/a
* Steven T. Hatton:

HOWEVER..., when I g++ -o test *.cpp -W I get a zillion warnings such as
"missing initializer for member ?sth::elf32::Elf32_Ehdr_base::e_ident?". Is
my compiler right or wrong on this one?


The compiler is free to issue any warnings whatsoever, including "Warning:
identifier includes politically incorrect term(s)".

--
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?
Aug 9 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.