473,382 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

inialisation problem.

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;

}
Jul 19 '05 #1
2 1877
"muser" <ch**********@hotmail.com> wrote in message
news:f9**************************@posting.google.c om...
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
Jul 19 '05 #2

"muser" <ch**********@hotmail.com> wrote in message news:f9**************************@posting.google.c om...
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[i] * 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[i] * weigthingfactor;
weigtingfactor--;
} remainder = (product % Mod11);


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

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
0
by: Refky Wahib | last post by:
Hi I need Technical Support I finished a Great project using .Net and SQL Server and .Net Mobile Control My Business case is to implement this Program to accept about 1 Million concurrent...
9
by: Sudesh Sawant | last post by:
Hello, We have an application which communicates using remoting. There is a server which is a Windows Service. The server exposes an object which is a singleton. The client is a Web Application...
117
by: Peter Olcott | last post by:
www.halting-problem.com
17
by: Jon Slaughter | last post by:
I'm having a little trouble understanding what the slicing problem is. In B.S.'s C++ PL3rdEd he says "Becayse the Employee copy functions do not know anything about Managers, only the Employee...
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.