By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,418 Members | 1,088 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,418 IT Pros & Developers. It's quick & easy.

Malloc question

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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;
}

--

---------------------------
Time to get it done!

Remove (d*elete*) to reply
Nov 14 '05 #2

P: n/a
"Rick" <as******@hotmail.com> wrote in
news:3f*********************@news.xs4all.nl:
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.

--
- Mark ->
--
Nov 14 '05 #3

P: n/a
"Frane Roje" <frane.roje(d*elete*)@st.hinet.hr> wrote in
news:bt**********@ls219.htnet.hr:
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.

--
- Mark ->
--
Nov 14 '05 #4

P: n/a
Rick wrote:
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.


Does this help?
http://www.eskimo.com/~scs/C-faq/q7.16.html
http://www.eskimo.com/~scs/C-faq/q19.23.html

Nov 14 '05 #5

P: n/a
Rick wrote:

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." |
+---------+----------------------------------+-----------------------------+

Nov 14 '05 #6

P: n/a
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
Nov 14 '05 #7

P: n/a
Rick wrote:

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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #8

P: n/a
Rick wrote:

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
Nov 14 '05 #9

P: n/a
> 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
Nov 14 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.