initialization in constructor problem

Z

zl2k

hi,
I got a strange problem when running my problem. Basically, if I do

int temp = someXYZ->someArray.size();
std::cout<<temp<<std::endl; //this temp is all right, say, temp is 12
std::cout<<-(someABC->someXYZ->someArray.size())<<std::endl; // it
will print a large unreasonable number

People suggests that this is due to the uninitialized variable when an
instance of a class is created. I checked many times but can't find
the error. I post the constructor as following, please let me know if
I am doing all right.

variables in head file abc.h:

std::vector<unsigned short> counter;
std::vector<gsl_vector*> image;
std::vector<boost::shared_ptr<xyz> > layers;

constructor in cpp file abc.cpp:

abc::abc(): counter(100, 0), image(0), layers(0) {
//construct counter, image here
for (int i = 0; i < 10; i++){
boost::shared_ptr<xyz> ptr;
layers.push_back(ptr);
}
}

//main is in abc.cpp

variables in the header file xyz.h:

std::vector<std::vector<unsigned long> > Lout;
std::vector<unsigned short> LoutPos;

constructor in cpp file xyz.cpp:

xyz::xyz(): Lout(0), LoutPos(0) {}

// The rows of the two dimensional std array Lout is dynamically
insert or delete while running the program.

Duplicatted type of variables are dropped to make it simple.

How can I detect if a variable is correctly initialized while running
the problem? Thanks for comments.

zl2k
 
I

Ian Collins

zl2k said:
hi,
I got a strange problem when running my problem. Basically, if I do

int temp = someXYZ->someArray.size();
std::cout<<temp<<std::endl; //this temp is all right, say, temp is 12
std::cout<<-(someABC->someXYZ->someArray.size())<<std::endl; // it
will print a large unreasonable number
size() is returning an unsigned type, by negating it you end up with a
very large unsigned number.
 
A

Andrey Tarasevich

zl2k said:
...
I got a strange problem when running my problem.

Hmm... Isn't it exactly how it's supposed to be?
Basically, if I do

int temp = someXYZ->someArray.size();
std::cout<<temp<<std::endl; //this temp is all right, say, temp is 12
std::cout<<-(someABC->someXYZ->someArray.size())<<std::endl; // it
will print a large unreasonable number

People suggests that this is due to the uninitialized variable when an
instance of a class is created.

No. Apparently your 'someArray.size()' returns an unsigned value (must be an
instance of 'std::vector<>'). When you apply the unary '-' to an unsigned value
you get a [normally large] positive value as a result. That's how '-' works on
unsigned values by design.

If you expect to get a negative result, you have to convert the result to
_signed_ type first and only then apply the unary '-'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top