Quick C++ question

Alphathree33

Platinum Member
Dec 1, 2000
2,419
0
0
Does

sint16 base[12];

declare an array of TWELVE 16-bit signed integers?

(For a total of 12x16 bits)?



--------------------------------------------------
you have been here long enough to know the proper forums.

Senior Anandtech Moderator
Common Courtesy

 

MegaVovaN

Diamond Member
May 20, 2005
4,131
0
0
yes 12 integers numbered from 0 to 11

all will have default value (0)

edit: I didn't get to sint16 part, but I know arrays.

sint16 shouldn't be any different that int or double or string in this case
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: MegaVovaN
yes 12 integers numbered from 0 to 11

all will have default value (0)

edit: I didn't get to sint16 part, but I know arrays.

sint16 shouldn't be any different that int or double or string in this case

not necessarily true. primitive values on stack are not initialized.

to the OP: if sint16 is defined to be a signed 16-bit integer then yeah. but it's non-standard.

and... you do know we have a forum for this stuff right?
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
At least for Visual C++ you can't count on variables to be initialized to any non-garbage value by the runtime, except class objects will run the constructor.

That will get you 12 "sint16"s, whatever sint16 is typedef'd to be, [0] - [11], filled with who-knows-what until you initialize them.
 

MegaVovaN

Diamond Member
May 20, 2005
4,131
0
0
Sorry, Dave is right = they won't be 0 unless you write it like this:


sint16 base[12] = {0};
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Everyone else has pretty much covered it, but the ANSI C (99) and ANSI C++ (whatever revision it's at) all refer to the state of uninitialized variables as "indeterminate." Most platforms will throw exceptions when accessing unitialized space. *nix throws seg faults, and Windows platforms typically threw memory access violations.

Anyway, MegaVovaN's way of initializing an array is exactly how I've always done it.
 

sao123

Lifer
May 27, 2002
12,653
205
106
we spent years getting a programming forum... please use it...
Lest they take it away again. :(