malloc does not allocate beyond 65872

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 62
Reputation: AutoC is an unknown quantity at this point 
Solved Threads: 0
AutoC AutoC is offline Offline
Junior Poster in Training

malloc does not allocate beyond 65872

 
0
  #1
Aug 21st, 2009
Hi,
I need an array of approx. 2,00,000 elements.But malloc fails to allocate beyond 65872.
When I give
(short int*)malloc(sizeof(short int)*65000);
It works but seg faults when I give
(short int*)malloc(sizeof(short int)*66000);

whatsup with that?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: malloc does not allocate beyond 65872

 
0
  #2
Aug 21st, 2009
What compiler do you use?
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 62
Reputation: AutoC is an unknown quantity at this point 
Solved Threads: 0
AutoC AutoC is offline Offline
Junior Poster in Training

Re: malloc does not allocate beyond 65872

 
0
  #3
Aug 21st, 2009
gcc
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: malloc does not allocate beyond 65872

 
0
  #4
Aug 21st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 62
Reputation: AutoC is an unknown quantity at this point 
Solved Threads: 0
AutoC AutoC is offline Offline
Junior Poster in Training

Re: malloc does not allocate beyond 65872

 
0
  #5
Aug 25th, 2009
Originally Posted by Salem View Post
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 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];
}
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;
Last edited by AutoC; Aug 25th, 2009 at 8:10 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 7
Reputation: aflneto is an unknown quantity at this point 
Solved Threads: 0
aflneto aflneto is offline Offline
Newbie Poster

Re: malloc does not allocate beyond 65872

 
0
  #6
Aug 25th, 2009
well malloc is defined as
  1. void *malloc(size_t size);
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
Last edited by aflneto; Aug 25th, 2009 at 8:18 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 62
Reputation: AutoC is an unknown quantity at this point 
Solved Threads: 0
AutoC AutoC is offline Offline
Junior Poster in Training

Re: malloc does not allocate beyond 65872

 
0
  #7
Aug 25th, 2009
that doesnt seem to be the problem. The input to the function short int *waveform was initialized in a similar manner.It didnt cause a problem.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 7
Reputation: aflneto is an unknown quantity at this point 
Solved Threads: 0
aflneto aflneto is offline Offline
Newbie Poster

Re: malloc does not allocate beyond 65872

 
0
  #8
Aug 25th, 2009
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
  1. (short int*)malloc(sizeof(short int)*65000);
works, but
  1. (short int*)malloc(sizeof(short int)*66000);
fails
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,437
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1473
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: malloc does not allocate beyond 65872

 
-7
  #9
Aug 25th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 7
Reputation: aflneto is an unknown quantity at this point 
Solved Threads: 0
aflneto aflneto is offline Offline
Newbie Poster

Re: malloc does not allocate beyond 65872

 
0
  #10
Aug 25th, 2009
I just realized how stupid I was being! also,farmalloc is a function implemented for windows as well. Sorry for not being helpful!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC