943,813 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1185
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 21st, 2009
0

malloc does not allocate beyond 65872

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AutoC is offline Offline
74 posts
since Sep 2008
Aug 21st, 2009
0

Re: malloc does not allocate beyond 65872

What compiler do you use?
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Aug 21st, 2009
0

Re: malloc does not allocate beyond 65872

gcc
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AutoC is offline Offline
74 posts
since Sep 2008
Aug 21st, 2009
0

Re: malloc does not allocate beyond 65872

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 25th, 2009
0

Re: malloc does not allocate beyond 65872

Click to Expand / Collapse  Quote originally posted by Salem ...
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
Quote ...
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.

Quote ...
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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AutoC is offline Offline
74 posts
since Sep 2008
Aug 25th, 2009
0

Re: malloc does not allocate beyond 65872

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aflneto is offline Offline
7 posts
since Aug 2009
Aug 25th, 2009
0

Re: malloc does not allocate beyond 65872

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AutoC is offline Offline
74 posts
since Sep 2008
Aug 25th, 2009
0

Re: malloc does not allocate beyond 65872

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aflneto is offline Offline
7 posts
since Aug 2009
Aug 25th, 2009
-7

Re: malloc does not allocate beyond 65872

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Aug 25th, 2009
0

Re: malloc does not allocate beyond 65872

I just realized how stupid I was being! also,farmalloc is a function implemented for windows as well. Sorry for not being helpful!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aflneto is offline Offline
7 posts
since Aug 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Input validation and Error handling in C?
Next Thread in C Forum Timeline: remove duplicates in a sorted array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC