I got a strange behavior in my XCode static library (objective c++ with libc++): i have an allocated ByteArray (unsigned char *) and it does not allocate to 0 (over the given size) from within the static library. If i directly build the source in the Design Project everything works fine and the pointer points to an unsigned char array with the given size and all bytes set to 0. From within the static library, it looks like the array gets overwritten, because some bytes in the array have random bytes set
I'm compiling in XCode (5.1.1) on OSX (10.9.4) for iOS (7.1), using libc++ as C++ Standard Library with language Dialect -std=c++11 with the Default Compiler (Apple LLVM 5.1).
I tried allocating in different ways:
typedef unsigned char byte;
class ByteArray {
private:
byte* mBytes;
long mSize;
public:
ByteArray(long Size){
//1
this->mBytes = new byte[Size]();
//2
this->mBytes = (byte*) calloc(Size, sizeof(byte));
//3
this->mBytes = (byte*)malloc(Size*sizeof(byte));
memset(this->mBytes, 0, Size);
//4
this->mBytes = (byte*)malloc(Size*sizeof(byte));
byte zero = 0;
for (int i = 0; i < Size; i++)
{
memcpy(this->mBytes + i, &zero, 1);
}
//Set size
this->mSize = Size;
}
//with Data
ByteArray(byte* Bytes, long Size){
this->mBytes = new byte[Size]();
memcpy(this->mBytes, Bytes, Size);
this->mSize = Size;
}
}
I'm initializing like this:
ByteArray* zeroBytes = new ByteArray(16);
The above Code is in the Class Initializer for an ByteArray holder Class: From within the library i get results like (HEX): 000000b0b1c9555190be923b00000900
instead of: 00000000000000000000000000000000
Does anyone already got into this strange behavior and has figured out what caused this?
edit: If i initialize with given data the byte* is never corrupt.
edit2: So, i figured it out, the Buffer owerflow happened in another Library and this killed everything. Thanks a lot for all of your help.