question

bryon-5025 avatar image
0 Votes"
bryon-5025 asked bryon-5025 commented

CFile - Binary Write

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++
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RLWA32-6355 avatar image
1 Vote"
RLWA32-6355 answered bryon-5025 commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Excellent, thank you. I was curious why that was.

0 Votes 0 ·