Now why are you casting the result of malloc?
Could it be that you didn't include stdlib.h and thus the compiler considers malloc as returning an int rather than a pointer?
This can be a well hidden and very obscure bug.
> I need an array of approx. 2,00,000 elements.But malloc fails to allocate beyond 65872.
Is this the only malloc call in your program?
Is it really an array of short int's, as the casting suggests?
If any previously allocated memory was screwed up (say by a buffer overrun), then anything could be happening. Staring at one line of code which is perfect if run in isolation won't solve it.
I've included stdlib,no problem there.
Yes.It is an array of short ints
Here is the entire functionint WriteRIFF( char *waveFileName, short *waveform, long numsamples, long samplingRate, int bytesPerSample)
{
FILE *file;
RIFFWaveFile *myRIFF = NULL;
long i = 0;
long size = 0;
int result = SUCCESS;
file = fopen(waveFileName, "w");
myRIFF = (RIFFWaveFile *)malloc(sizeof(RIFFWaveFile));
myRIFF->header = (RIFFHeader *)malloc(sizeof(RIFFHeader));
strcpy(myRIFF->header->riffChunkID, "RIFF");
myRIFF->header->riffChunkDataSize = numsamples*2 + 36;
strcpy(myRIFF->header->riffType, "WAVE");
strcpy(myRIFF->header->fmtChunkID,"fmt ");
myRIFF->header->fmtChunkDataSize = 16;
myRIFF->header->compressionCode = 1;
myRIFF->header->numberOfChannel = 1;
myRIFF->header->sampleRate = samplingRate;
myRIFF->header->averageBytesPerSecond = bytesPerSample*samplingRate;
myRIFF->header->blockAlign = 2;
myRIFF->header->sigBitsPerSample = bytesPerSample*8;
strcpy(myRIFF->header->dataChunkID,"data");
myRIFF->header->dataChunkDataSize = numsamples*2;
size = myRIFF->header->dataChunkDataSize/2;
/* This is where the problem is */
myRIFF->data = (short int*)malloc(sizeof(short int)*size);
for(i=0; i data[i] = waveform[i];
}
RiffWaveFile and RiffHeader are structures as follows.typedef unsigned char UCHAR;
typedef short int INT;
typedef long DWORD;
typedef struct RIFFHeader
{
UCHAR riffChunkID[4]; //0x00
DWORD riffChunkDataSize; //0x04
UCHAR riffType[4]; //0x08
UCHAR fmtChunkID[4]; //0x0C
DWORD fmtChunkDataSize; //0x10
INT compressionCode; //0x14
INT numberOfChannel; //0x16
DWORD sampleRate; //0x18
DWORD averageBytesPerSecond; //0x1C
INT blockAlign; //0x20
INT sigBitsPerSample; //0x22
//@ This Element is not present for Uncompressed/PCM format
//INT numberOfExtraBytes __attribute__ ((packed)); //0x24
UCHAR dataChunkID[4]; //0x24
DWORD dataChunkDataSize; //0x28
}RIFFHeader __attribute__ ((packed));
typedef struct RIFFWaveFile
{
RIFFHeader *header; //0x00 to 0x2B
short int *data __attribute__ ((packed)); //0x2C to dataSize
}RIFFWaveFile;