CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2009
    Posts
    219

    Expression: (:"string is not null terminated" && 0)

    Hello,

    I've got a question. When i try to compile this code:

    Code:
    int main()
    {
    	char CPUBrandString[0x40];
        char CPUString[0x20];
        int CPUInfo[4] = {-1};
    	int nCacheSizeK = 0;
    	int nL2Associativity = 0;
    	int nCacheLineSize = 0;
        unsigned    nIds, i,nExIds;
        bool    bSSE3NewInstructions = false;
        bool    bMONITOR_MWAIT = false;
        bool    bCPLQualifiedDebugStore = false;
        bool    bThermalMonitor2 = false;
    
    	char FinalNumber[100];
    	int numberzero=0, firstnumber=0, secondnumber=0, thirdnumber=0;
    	char numbernul[50], numberone[50], numbertwo[50], numberthree[50];
    
        __cpuid(CPUInfo, 0);
        nIds = CPUInfo[0];
        memset(CPUString, 0, sizeof(CPUString));
        *((int*)CPUString) = CPUInfo[1];
        *((int*)(CPUString+4)) = CPUInfo[3];
        *((int*)(CPUString+8)) = CPUInfo[2];
    
     for (i=0; i<=nIds; ++i)
        {
    	__cpuid(CPUInfo, i); 
    
    	numberzero += CPUInfo[0];
    	firstnumber += CPUInfo[1];
    	secondnumber += CPUInfo[2];
    	thirdnumber += CPUInfo[3]; 
    	}
    
            itoa(numberzero, numbernul, 16);
    	itoa(firstnumber, numberone, 16);
    	itoa(secondnumber, numbertwo, 16);
    	itoa(thirdnumber, numberthree, 16);
    
    	strncat_s(numberone, numbernul, 50);
    	strncat_s(numbertwo, numberone, 50);
    	strncat_s(numberthree, numbertwo, 50);	
    	strncat_s(FinalNumber, numberthree, 50);
    it compiles fine, but when i run it, microsft visual c++ debug library error (Title)

    Any ideas

    ITs not the entire coded, but it isnt needed right?

  2. #2
    Join Date
    Apr 2009
    Posts
    598

    Re: Expression: (:"string is not null terminated" && 0)

    char CPUBrandString[0x40];
    char CPUString[0x20];
    It's the first time I see the length of arrays of characters defined using hexadecimals. Maybe you could replace these lines with:
    Code:
        char CPUBrandString[64];
        char CPUString[32];
    How is defined strncat_s?

    Edit: I see strncat_s is defined with four arguments, but you don't write the second one in your program:
    Code:
    errno_t strncat_s(
       char *strDest,
       size_t bufferSizeInBytes,
       const char *strSource,
       size_t count
    );
    // (source http://msdn.microsoft.com/fr-fr/libr...af(VS.80).aspx)
    Last edited by olivthill2; May 17th, 2010 at 05:37 AM.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Expression: (:"string is not null terminated" && 0)

    Quote Originally Posted by olivthill2 View Post
    Edit: I see strncat_s is defined with four arguments, but you don't write the second one in your program:
    There's a version of most _s functions which uses templates to automatically detect the second parameter. He's probably using that. It works whenever you pass a non-decayed array in.

    As for the problem, it would have been clearer if you had told us which line the debug assertion occurred on; however, I suspect it was the final strncat_s is the issue----FinalNumber needs to be initialized to a valid size-0 string. Essentially that means you need to set FinalNumber[0] to 0.

    It would be simpler and more robust (and you wouldn't need to use those _s functions) if you used std::strings rather than char arrays.
    Last edited by Lindley; May 17th, 2010 at 09:26 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured