Post

Replies

Boosts

Views

Activity

Strange execution results running program in Xcode
Hi; Here is a simple program that reads a file and counts number of 1s bits in it. The file it reads, simple.txt, is a static text file, resides in the file system, and nothing is touching it during the program execution. The file's length is 436 bytes. When the program is run in the terminal it produces consistent results. Each run counts 1329 1s bits. It behaves differently when run in Xcode. A simple command-line tool project having a single file. Running the program multiple times, via Command-R, produces something strange. That is each execution randomly produces one of two results. One is expected 1329 1s bits, another one shows 1333 1s bits. That is it counts four bits more. Again, each run randomly produces one of those two results. And it happens only when I run the program in Xcode. I tried a different sample text file and experienced the same behavior, but the difference in 1s bits count was five bits. Any idea of how this behavior can be explained? func countOnes(in byte: UInt8) -> Int { var nOnes = 0 var mask: UInt8 = 0x1 while mask != 0 { if (byte & mask) > 1 { nOnes += 1 } mask <<= 1 } return nOnes } func countOnes(in buffer: UnsafeMutableRawBufferPointer, length: Int) -> Int { var nOnes = 0 for byte in buffer[0...length] { nOnes += countOnes(in: byte) } return nOnes } var fd = try FileDescriptor.open("sample.txt", .readOnly) var buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 4096, alignment: 4) var nBytes = try fd.read(into: buffer) var nOnes = 0 while nBytes > 0 { nOnes += countOnes(in: buffer, length: nBytes) nBytes = try fd.read(into: buffer) } buffer.deallocate() try fd.close() print(nOnes) My machine Darwin MacBookPro 24.6.0 Darwin Kernel Version 24.6.0: Wed Oct 15 21:08:19 PDT 2025; root:xnu-11417.140.69.703.14~1/RELEASE_ARM64_T8103 arm64 macOS Sequoia 15.7.2 Xcode 26.1.1 (17B100) Thanks, Igor
0
0
112
1d