473,327 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Malloc question

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
9 4022
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
"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
"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
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
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
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
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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
66
by: Knady | last post by:
Hi, I have the following problem, I must to do my assignment, but I really do not know how to use the malloc. I need create a program that will be used to do some algebrical computation on the...
36
by: Martin Andert | last post by:
Hello, I have a question regarding malloc and free. Here my code sample: int main() { /* allocating dynamic memory for array */ int* array = (int*) malloc(5 * sizeof(int)); /* ... program...
23
by: puzzlecracker | last post by:
Why is "new" a C++ language-level construct while "malloc" is a library function?
8
by: Snis Pilbor | last post by:
First, let me announce that this is very possibly off-topic because malloc is a specific third party accessory to c, etc. I spent about an hour trying to find a more appropriate newsgroup and...
12
by: gooch | last post by:
I originally posted the following code in a group discussing threads but after further research I think I have a c question about the code. I know there are a couple of non standard c includes here...
25
by: Why Tea | last post by:
Thanks to those who have answered my original question. I thought I understood the answer and set out to write some code to prove my understanding. The code was written without any error checking....
24
by: VijaKhara | last post by:
hi all, i am trying to create a dynamic 2D array with size N x 3 (N will be put in as a parameter) using the following code: int **xyz; int i,N; N=30000; xyz=malloc(3*sizeof(int*));
17
by: Christopher Benson-Manica | last post by:
Some recent posts got me thinking about how one might have dealt with simplistic malloc() implementations which might return NULL for a 64K request but might accept two 32K requests or four 16K...
71
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.