inialisation problem.

M

muser

The following function gives me the following error: warning C4700:
local variable 'Newirrecord' used without having been initialized.

Newirrecord is the instance of a structure. I get the same in another
function as well, where an ordinary variable gives me the same
warning.
isn't strncpy passing a string to partnum which in turn I access with
Newirrecord.partnum? Shouldn't Newirrecord.partnum contain something
at run time?

Thank you in advance for your help.










bool CheckDigit(ofstream& prnfile, char* record)
{

int weightingfactor;
int remainder;
int weightitem1, weightitem2, weightitem3, weightitem4, weightitem5;
int product;
int Mod11 = 11;
int checkdigit;
char partnum[6];

irrecord Newirrecord;


strncpy(partnum, &record[7], 6);
partnum[6] = '\0';
Newirrecord.partnum[6] = atol( partnum );


weightingfactor = 6;


weightitem1 = (Newirrecord.partnum[1] * weightingfactor);

weightingfactor = 5;

weightitem2 = (Newirrecord.partnum[2] * weightingfactor);

weightingfactor = 4;

weightitem3 = (Newirrecord.partnum[3] * weightingfactor);

weightingfactor = 3;

weightitem4 = (Newirrecord.partnum[4] * weightingfactor);

weightingfactor = 2;

weightitem5 = (Newirrecord.partnum[5] * weightingfactor);




product = (weightitem1 + weightitem2 + weightitem3 + weightitem4 +
weightitem5);

remainder = (product % Mod11);

checkdigit = (Mod11 - remainder);
if(! Newirrecord.partnum[6] == checkdigit){
prnfile<< "Invalid part number";
prnfile<< record << endl;
}
return false;


return true;

}
 
M

Mike Wahler

muser said:
The following function

gives me the following error: warning C4700:
local variable 'Newirrecord' used without having been initialized.

This would result from code such as:

int i;

cout << i;
Newirrecord is the instance of a structure. I get the same in another
function as well, where an ordinary variable gives me the same
warning.

Yes, this issue is independent of the object's type.
isn't strncpy passing a string to partnum which in turn I access with
Newirrecord.partnum?

Shouldn't Newirrecord.partnum contain something
at run time?

Not if you haven't explicitly caused it to.
The only types of objects which when instantiated
always have an initial value are those which have
default constructors or which are supplied an initializer
at point of definition. Since you mention 'strncpy()'
I assume the data member in question is type 'char*',
which is a built-in type. Built-in types do not
have constructors, default or otherwise.
Thank you in advance for your help.

bool CheckDigit(ofstream& prnfile, char* record)
{

int weightingfactor;
int remainder;
int weightitem1, weightitem2, weightitem3, weightitem4, weightitem5;
int product;
int Mod11 = 11;
int checkdigit;
char partnum[6];

I recommend replacing char arrays with 'std::string' objects.
Life will become much simpler.
irrecord Newirrecord;

What is the definition of type 'irrecord'?
Does it have a default constructor? Does
this constructor initialize all the data
members?

If not, then the above instantiates a type
'irrecord' object whose data members are *not*
initialized, except for those with types that
have default ctors.
strncpy(partnum, &record[7], 6);

I think this will write off the end of your 'partnum' array.
partnum[6] = '\0';

And this is certainly out of bounds. Valid indices
for an array of six elements range from zero to five.

All these array mistakes can be eliminated by using
'std::string' object instead.
Newirrecord.partnum[6] = atol( partnum );

Another out of bounds array access.

Also, your direct access of 'Newirrecord' tells me you
have public data. Not illegal, but typically poor
design.

-Mike
 
R

Ron Natalie

muser said:
Newirrecord is the instance of a structure. I get the same in another
function as well, where an ordinary variable gives me the same
warning.

It's most likely a POD structure. POD types are bogusly not initialized in
some cases (this is one of them). Stupid feature of the language #1.
Can't really tell for sure though, you don't show us what a "irrecord" is.
isn't strncpy passing a string to partnum which in turn I access with
Newirrecord.partnum? Shouldn't Newirrecord.partnum contain something
at run time?

strncpy doesn't pass anything. It copies data starting at one poitner to
char to another pointer to char, stopping when it hits either the specified
number of chars or a null.
char partnum[6];
strncpy(partnum, &record[7], 6);
partnum[6] = '\0';

partnum[6] is one past the end of the array. You are causing undefined
behavior to access this.
Newirrecord.partnum[6] = atol( partnum );

What is the type of partnum? I hope it's an array of at least seven integral types.
weightitem1 = (Newirrecord.partnum[1] * weightingfactor);

You do know that:
1. C++ array indices start at 0.
2. It's not necessary to wrap every subexpression in parens.
3. You can use literals directly in expressions:
weightnum1 = Newireconrd.partum * 6;


You also seem to like to type a lot

int weigthinfactor = 6;
int product = 0;
for(int i = 1; i <= 5; ++i) {
product += Newicrecord.partnum * weigthingfactor;
weigtingfactor--;
}
remainder = (product % Mod11);

I had to hunt around to find how Mod11 was defined. What's wrong with
remainder = product % 11;
 

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

Similar Threads

passing a parameter 2
The program/ header file contents 2
Reading a file 1
function error 6
Access violation error 10
Reading a file. 3
char conversion problem 18
Problem with codewars. 5

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top