Thread: The variable 'strTeste' is being used without initialization.

  1. #1
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184

    The variable 'strTeste' is being used without initialization.

    Code:
    eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
    {
        eMBErrorCode    eStatus = MB_ENOERR;
        int             iRegIndex;
    
        if( ( usAddress >= REG_HOLDING_START ) &&
            ( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) )
        {
            iRegIndex = ( int )( usAddress - usRegHoldingStart );
            switch ( eMode )
            {
                /* Pass current register values to the protocol stack. */
            case MB_REG_READ:
                while( usNRegs > 0 )
                {
                    *pucRegBuffer++ = ( UCHAR ) ( usRegHoldingBuf[iRegIndex] >> 8 );
                    *pucRegBuffer++ = ( UCHAR ) ( usRegHoldingBuf[iRegIndex] & 0xFF );
                    iRegIndex++;
                    usNRegs--;
                }
                break;
    
                /* Update current register values with new values from the
                 * protocol stack. */
            case MB_REG_WRITE:
                while( usNRegs > 0 )
                {
                    usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
                    usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
                    iRegIndex++;
                    usNRegs--;
                }
            }
        }
        else
        {
            eStatus = MB_ENOREG;
        }
        return eStatus;
    }
    
    //Calling TEST:
    UCHAR * strTeste;
    eMBRegHoldingCB(strTeste, 11, 1, MB_REG_WRITE);
    VSS Warning:
    Run-Time Check Failure #3: The variable 'strTeste' is being used without initialization.

    I tried, strcpy, but the compiler dont allow UCHAR arg.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    What you have here:

    Code:
    *pucRegBuffer++ = ( UCHAR ) ( usRegHoldingBuf[iRegIndex] >> 8 );
    First assigns a value to a completely random memory location, since strTeste was uninitialized, then it increments this random address by one. So you begin with an address violation, and proceed to violate it more and more throughout the function.

    What are you trying to do?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    this is a function of communication using the Modbus protocol.
    In the argument in question must pass an array of bytes to trigger digital outputs.

    Basically, I need to send the ASCII character 1

  4. #4

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sergioms View Post
    this is a function of communication using the Modbus protocol.
    In the argument in question must pass an array of bytes to trigger digital outputs.

    Basically, I need to send the ASCII character 1
    It's not clear what UCHAR typedefs to, so I am assuming:

    Code:
    typedef unsigned char UCHAR;
    That being the case, you are not passing "an array of bytes". You are passing an uninitialized pointer.

    Of course, "an array of bytes" is not "the ASCII character 1", so I am still confused, and the prototype for eMBRegHoldingCB() indicates that the first parameter is a pointer, not a single char. If strTeste is supposed to be a single char, you would probably want:

    Code:
    UCHAR strTeste = '1';  // no *
    eMBRegHoldingCB(&strTeste ...
    Tho this does still not make much sense considering the code in eMBRegHoldingCB().

    If this helps: order of precedence is important here. Postfix ++ has a higher order of precedence than dereference *, but of course the postfix is not applied until the end. What that means in this case:

    Code:
    *pucRegBuffer++ = whatever
    is that the assignment applies to the dereferenced value of pucRegBuffer, and this takes place before the ++ increment. However, because ++ has a higher order of precedence than *, when it is applied, it applies to pucRegBuffer, not *pucRegBuffer. If that's what you want (to increment the value, not the address), then:

    Code:
    (*pucRegBuffer)++ = whatever
    Altho this is no good in an assignment because that is not a valid lvalue.

    If you do not understand what I am talking about, that's okay, but then I would presume you did not write this function. If that is the case, let us know, because what you need help with is understanding the code in eMBRegHoldingCB().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You almost certainly don't want to use strcpy, since you are probably sending binary data, and thus may want to copy a zero-byte character (which strcpy would use to mark end-of-string). The better alternative to strcpy would be memcpy (since you don't change byte order), as in
    Code:
    case MB_REG_READ:
        memcpy(pucRegBuffer, &usRegHoldingBuf[iRegIndex], usNRegs * sizeof(USHORT));
        break;
    and similiar for the MB_REG_WRITE case.

    As others have mentioned, the main probelm is that you are using strTeste uninitialized. Not only that, but strTeste is only a pointer, and doesn't point to any ModBus data. You could try something like:
    Code:
    UCHAR strTeste[5] = {'1', ...};  // reserve space for and initialize up to 10 UCHARS (bytes) of data
    eMBRegHoldingCB(strTeste, 11, 1, MB_REG_WRITE);
    Do take note that your eMBRegHoldingCB reads/writes 2 bytes at a time, but you say you only want to send one byte (ASCII character '1'), so you will be sending a second, garbage byte. Make sure that is ignored on the other end.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Auto variable initialization
    By cnickp in forum C Programming
    Replies: 1
    Last Post: 11-07-2009, 07:11 AM
  2. Array initialization with int variable
    By tsantana in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2009, 02:48 PM
  3. member variable initialization
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2007, 09:52 AM
  4. variable initialization error...
    By sunoflight77 in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2005, 12:01 AM
  5. Variable Declaration & Initialization :: C++
    By kuphryn in forum C++ Programming
    Replies: 6
    Last Post: 12-26-2001, 10:22 AM