Malloc question

R

Rick

Hi,

I'm having troubles with allocating memory. I need to declare a large buffer
(at least 60.000 entries of unsigned int16 type). The problem is, the
computer is quit simple (64 mb ram though) and the programming language
is... TurboC from a few centuries ago. The first thing I tried was to
declare an array like this:
unsigned int data[60000];
But, the maximum range is the maximum of a signed int16 so 60.000 is too
large. Declaring 2 arrays is possible but it gives problems because the
second array doesn't exactly start where the first array ends. So, I tried
it with malloc:
unsigned int *adress;
adress = malloc( sizeof( unsigned int ) * 60000 );
if (!adress)
{
printf("Cannot allocate memory!");
exit( 1 );
} else...
But that didn't work either, * 60000 is way too big, the maximum lies
somewhere between 1000 and 2000 (?). Maybe there's something wrong with my
code? Or does somebody knows a neat trick to reserve a large amount of
memory?

Greetings,
Rick
 
F

Frane Roje

This is the code I used in my code, compiled with VS.NET, my computer has
192MB RAM and it succeeded! And sizeof(unsigned int); on my computer is 4
bytes.

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
unsigned int *adress;
adress = malloc( sizeof( unsigned int ) * 60000 );
if (!adress)
{
printf("Cannot allocate memory!");
exit( 1 );
} else printf("Success!");
system("pause");
return 0;
}
 
M

Mark A. Odell

I'm having troubles with allocating memory. I need to declare a large
buffer (at least 60.000 entries of unsigned int16 type). The problem is,
the computer is quit simple (64 mb ram though) and the programming
language is... TurboC from a few centuries ago. The first thing I tried
was to declare an array like this:
unsigned int data[60000];
But, the maximum range is the maximum of a signed int16 so 60.000 is too
large. Declaring 2 arrays is possible but it gives problems because the
second array doesn't exactly start where the first array ends. So, I
tried it with malloc:
unsigned int *adress;
adress = malloc( sizeof( unsigned int ) * 60000 );
if (!adress)
{
printf("Cannot allocate memory!");
exit( 1 );
} else...
But that didn't work either, * 60000 is way too big,

You're hosed. Move to a real platform or ask in one of the Borland
newsgroups how to do this in a non-standard (from C's perspective) way.
The answer for that compiler is off-topic here.
 
M

Mark A. Odell

This is the code I used in my code, compiled with VS.NET, my computer
has 192MB RAM and it succeeded! And sizeof(unsigned int); on my computer
is 4 bytes.

And now we see why implementations don't describe the language. One
implementation can allocate gobs of memory another can't. But C imposes no
restrictions on how much can be allocated. Don't use an implementation as
the end-all be-all answer to how C works.
 
K

Kenneth Brody

Rick said:
Hi,

I'm having troubles with allocating memory. I need to declare a large buffer
(at least 60.000 entries of unsigned int16 type). The problem is, the
computer is quit simple (64 mb ram though) and the programming language
is... TurboC from a few centuries ago. The first thing I tried was to
declare an array like this:
unsigned int data[60000];
[...]

This is platform-specific, however...

The 16-bit memory model of the ancient TurboC you are using is limited
to objects of 64K in size or less. _Unless_ you declare the object as
being "huge", that is. ("Large" allows a total of more than 64K, but
each thing is still limited to 64K.)

As to _how_ you declare it as such, I forget, as it's been too many years
since I've been limited to such a platform.

There's probably a TurboC newsgroup, or perhaps comp.os.msdos.progammer
may be of help.

--

+---------+----------------------------------+-----------------------------+
| Kenneth | kenbrody at spamcop.net | "The opinions expressed |
| J. | http://www.hvcomputer.com | herein are not necessarily |
| Brody | http://www.fptech.com | those of fP Technologies." |
+---------+----------------------------------+-----------------------------+
 
R

Rick

You're right,

First of all, I used farmalloc instead of malloc. It still didn't work under
TurboC but the *exe worked! Talking about TurboC, does somebody knows a good
help / newsgroup for this since I have more questions (multithreading...)?

Thanks for helping!
Rick
 
C

CBFalconer

Rick said:
I'm having troubles with allocating memory. I need to declare a
large buffer (at least 60.000 entries of unsigned int16 type). The
problem is, the computer is quit simple (64 mb ram though) and the
programming language is... TurboC from a few centuries ago. The
first thing I tried was to declare an array like this:
unsigned int data[60000];

Compiler/system specifics are OT on c.l.c, and you need to find a
newsgroup that deals with your system.

That said, you will have problems finding TC advice today, and you
may need the old farts group alt.folklore.computers.

At any rate, try changing the model option for TC to compact or
large. You will need the corresponding library files. You can
download the whole schmeer from the Borland museum pages.
 
D

Default User

Rick said:
You're right,

First of all, I used farmalloc instead of malloc. It still didn't work under
TurboC but the *exe worked! Talking about TurboC, does somebody knows a good
help / newsgroup for this since I have more questions (multithreading...)?

<OT>
You can usually get rid of some of the nonstandard things by choosing
the correct memory model in TC. Also, I don't think it supported
multithreading. Why are you trying to use this compiler?
</OT>



Brian Rodenborn
 
R

Rick

Why are you trying to use this compiler?

You're right, no multi threading. But I could fix it in another way :) Why
do I use this compiler? I don't know, my bos asked me to help on a very
simple industrial computer with DOS and TurboC installed.

Greetings,
Rick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top