inialisation problem.

Discussion in 'C++' started by muser, Sep 18, 2003.

  1. muser

    muser Guest

    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;

    }
     
    muser, Sep 18, 2003
    #1
    1. Advertising

  2. muser

    Mike Wahler Guest

    "muser" <> wrote in message
    news:...
    > 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
     
    Mike Wahler, Sep 18, 2003
    #2
    1. Advertising

  3. muser

    Ron Natalie Guest

    "muser" <> wrote in message news:...

    > 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;
     
    Ron Natalie, Sep 18, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Suurkuusk
    Replies:
    0
    Views:
    4,000
    Andreas Suurkuusk
    Jul 27, 2003
  2. Ted Miller
    Replies:
    0
    Views:
    5,179
    Ted Miller
    Sep 13, 2003
  3. Merek
    Replies:
    0
    Views:
    1,980
    Merek
    Dec 3, 2003
  4. Scott Meddows
    Replies:
    1
    Views:
    404
    John Saunders
    Jun 8, 2004
  5. Mike

    Problem problem problem :( Need Help

    Mike, May 7, 2004, in forum: ASP General
    Replies:
    2
    Views:
    558
    Bullschmidt
    May 11, 2004
Loading...

Share This Page