| | |
malloc does not allocate beyond 65872
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
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.
•
•
Join Date: Sep 2008
Posts: 62
Reputation:
Solved Threads: 0
•
•
•
•
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.
Yes.It is an array of short ints
Here is the entire function
•
•
•
•
int 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 <size; i++)
{
myRIFF->data[i] = waveform[i];
}
•
•
•
•
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;
Last edited by AutoC; Aug 25th, 2009 at 8:10 am.
•
•
Join Date: Aug 2009
Posts: 7
Reputation:
Solved Threads: 0
well malloc is defined as
and size_t is probably 64kb is your computer.
use farmalloc.
http://vitaly_filatov.tripod.com/ng/tc/tc_000.78.html
this is a function that allows you to allocate more than 64kb
hope it helps
C Syntax (Toggle Plain Text)
void *malloc(size_t size);
use farmalloc.
http://vitaly_filatov.tripod.com/ng/tc/tc_000.78.html
this is a function that allows you to allocate more than 64kb
hope it helps
Last edited by aflneto; Aug 25th, 2009 at 8:18 am.
•
•
Join Date: Aug 2009
Posts: 7
Reputation:
Solved Threads: 0
What I was trying to say malloc cant allocate that much memory. It can allocate a maximum of 64kb of memory in one go.
Hence why
works, but
fails
answering your original question.
farmalloc() function on the other hand allows you to allocate blocks larger than 64kb.
Hence why
C Syntax (Toggle Plain Text)
(short int*)malloc(sizeof(short int)*65000);
C Syntax (Toggle Plain Text)
(short int*)malloc(sizeof(short int)*66000);
answering your original question.
farmalloc() function on the other hand allows you to allocate blocks larger than 64kb.
Last edited by aflneto; Aug 25th, 2009 at 9:26 am.
As far as I can tell farmalloc() is only implemented on 16-bit compilers. There is no such thing as a "far heap" in 32 (or more)-bit programs because they use the flat memory model where all memory is considered near.
Last edited by Ancient Dragon; Aug 25th, 2009 at 10:09 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- free() = delete or delete[]? (C++)
- Malloc Function (C)
- reasons why malloc fails? (C)
- creating big arrays in c (C++)
- Array problems (C++)
- Interesting problem : please see this and reply (C)
- reading a file into code (Java)
- Array limit (C)
Other Threads in the C Forum
- Previous Thread: Input validation and Error handling in C?
- Next Thread: remove duplicates in a sorted array
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush fgets file fork forloop framework frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o incrementoperators kernel kilometer km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue multi mysql number odf open opensource openwebfoundation owf pattern pdf performance pointer pointers posix probleminc process program programming pyramidusingturboccodes read recursion recv repetition research scanf scheduling scripting segmentationfault send shape socket socketprograming stack standard string strings systemcall testautomation unix user voidmain() wab win32api windows.h






