CFile - Binary Write

bryon 101 Reputation points
2021-09-22T16:20:22.013+00:00

Hello Everybody,
I am creating and writing a binary file, and am curious about a particular behavior. I am writing a struct data to the file, and the unitialized fields always come up as 0xCC in the binary file.

#pragma pack(push, 1)  
struct ExampleStruct  
{  
BYTE flag;   
short   index;  
short code;  
};  
#pragma pop()  
...Some class definitions  
class StuctTest : CFile  
....  
StuctTest::StuctTest(const CString &csFilename)  
{  
CFileException cFileException;  
m_FileStatus = 0;  
int RetryCount = 0;  
m_Filename = csFilename; // save the filename for the print function  
BOOL bStat = Open(csFilename, modeReadWrite | modeCreate | modeNoTruncate |  
shareDenyNone | typeBinary, &cFileException);  
}  
int StuctTest::WriteData(int loc, const ExampleStruct &exampleStruct)  
{  
long lOffset, lActual;  
int Status = 0;  
ExampleStruct example;  
int sizeOfStruct = sizeof(struct ExampleStruct);  
lOffset = (loc - 1) * sizeof(struct ExampleStruct);  
// put the data read into the correct structure to write to the file  
example.code = exampleStruct.code;  
example.flag = exampleStruct.flag;  
example.index = exampleStruct.index;  
TRY  
{  
lActual = Seek(lOffset, CFile::begin);  
Write((void *)&example, sizeof(struct ExampleStruct));  
return 0;  
}  
CATCH(CFileException, pFileException)  
{  
}  
END_CATCH  
return Status;  
}  
Here is where the file gets created...  
StuctTest test(_T("C:\\temp\\temp.dat"));  
for (int i = 0; i <  3; i++)  
{  
ExampleStruct example;  
example.flag = 0x20 * i + 1;  
//example.index = i;  
example.code = 0x1 * i;  
test.WriteData(i + 1, example);  
}  
test.~StuctTest();  
     

Here is the output:
134277-2021-09-22-11-17-01.png

Since the one field was commented out, is 0xCC typical behavior for an unitialized struct varaible?
Thanks!

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,519 questions
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 40,021 Reputation points
    2021-09-22T16:29:57.143+00:00

    In a debug build with the default settings for runtime checks VC++ will fill uninitialized local variables with 0xCC. See rtc-run-time-error-checks

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful