Re: update...
From: Thomas Matthews (Thomas_MatthewsHatesSpam_at_sbcglobal.net)
Date: 10/22/03
- Next message: red floyd: "Re: Why no copy_if?"
- Previous message: Victor Bazarov: "Re: overriding static and virtual members with same proto. What is the standard ?"
- In reply to: earl: "update..."
- Next in thread: Julián Albo: "Re: update..."
- Reply: Julián Albo: "Re: update..."
- Reply: Karl Heinz Buchegger: "Re: update..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 22 Oct 2003 16:42:29 GMT
earl wrote:
> class foo
> {
> foo();
> private:
> char table[64];
> };
>
> foo::foo()
> {
> for(int index=0; index < 64; index++)
> table[index] = index;
Here you assign an "int" value (index)
into a "char" type (table[]).
In many systems, a char type is smaller than an
int type. In these systems, an "invisible" truncation
or conversion will take place. Ugly, very ugly.
Try this:
for (char index = 0; index < 64; ++index)
table[index] = index;
> }
>
> in the main program I do ;
>
> foo test;
>
> nothing more, just run the constructor
>
> it seems to run through the above loop coz I get 'Press any key to continue'
> then I get popup box saying test.exe has encountered a problem and needs to
> close. We are sorry for the inconvenience.
>
> If I remove the for loop in the constructor everything runs smoothly.
>
>
One item that others haven't mentioned is that you
declare the array as type "char" while you assign it
"int" values. Although many compilers may not issue a warning,
I believe it should, and you should turn up the warning levels.
In your main() program, insert the following:
std::cout << "Size of char: " << sizeof(char) << "\n";
std::cout << "Size of int: " << sizeof(int) << "\n";
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
- Next message: red floyd: "Re: Why no copy_if?"
- Previous message: Victor Bazarov: "Re: overriding static and virtual members with same proto. What is the standard ?"
- In reply to: earl: "update..."
- Next in thread: Julián Albo: "Re: update..."
- Reply: Julián Albo: "Re: update..."
- Reply: Karl Heinz Buchegger: "Re: update..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|