inialisation problem.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • muser

    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.par tnum? Shouldn't Newirrecord.par tnum contain something
    at run time?

    Thank you in advance for your help.










    bool CheckDigit(ofst ream& 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.par tnum[6] = atol( partnum );


    weightingfactor = 6;


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

    weightingfactor = 5;

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

    weightingfactor = 4;

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

    weightingfactor = 3;

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

    weightingfactor = 2;

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




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

    remainder = (product % Mod11);

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


    return true;

    }
  • Mike Wahler

    #2
    Re: inialisation problem.

    "muser" <charlie12345@h otmail.com> wrote in message
    news:f9a2a258.0 309181019.30289 2b5@posting.goo gle.com...[color=blue]
    > The following function[/color]

    [color=blue]
    > gives me the following error: warning C4700:
    > local variable 'Newirrecord' used without having been initialized.[/color]

    This would result from code such as:

    int i;

    cout << i;
    [color=blue]
    >
    > 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.[/color]

    Yes, this issue is independent of the object's type.
    [color=blue]
    >
    > isn't strncpy passing a string to partnum which in turn I access with
    > Newirrecord.par tnum?[/color]

    [color=blue]
    >Shouldn't Newirrecord.par tnum contain something
    > at run time?[/color]

    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.
    [color=blue]
    >
    > Thank you in advance for your help.
    >
    > bool CheckDigit(ofst ream& prnfile, char* record)
    > {
    >
    > int weightingfactor ;
    > int remainder;
    > int weightitem1, weightitem2, weightitem3, weightitem4, weightitem5;
    > int product;
    > int Mod11 = 11;
    > int checkdigit;
    > char partnum[6];[/color]

    I recommend replacing char arrays with 'std::string' objects.
    Life will become much simpler.
    [color=blue]
    >
    > irrecord Newirrecord;[/color]

    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.
    [color=blue]
    >
    >
    > strncpy(partnum , &record[7], 6);[/color]

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

    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.
    [color=blue]
    > Newirrecord.par tnum[6] = atol( partnum );[/color]

    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


    Comment

    • Ron Natalie

      #3
      Re: inialisation problem.


      "muser" <charlie12345@h otmail.com> wrote in message news:f9a2a258.0 309181019.30289 2b5@posting.goo gle.com...
      [color=blue]
      > 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.[/color]

      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.
      [color=blue]
      > isn't strncpy passing a string to partnum which in turn I access with
      > Newirrecord.par tnum? Shouldn't Newirrecord.par tnum contain something
      > at run time?[/color]

      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.
      [color=blue]
      > char partnum[6];
      > strncpy(partnum , &record[7], 6);
      > partnum[6] = '\0';[/color]

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

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

      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.par tum[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.par tnum[i] * weigthingfactor ;
      weigtingfactor--;
      }[color=blue]
      > remainder = (product % Mod11);[/color]

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



      Comment

      Working...