Malloc question

Discussion in 'C Programming' started by Rick, Jan 8, 2004.

  1. Rick

    Rick Guest

    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
     
    Rick, Jan 8, 2004
    #1
    1. Advertising

  2. Rick

    Frane Roje Guest

    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
     
    Frane Roje, Jan 8, 2004
    #2
    1. Advertising

  3. "Rick" <> wrote in
    news:3ffd5837$0$330$4all.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 ->
    --
     
    Mark A. Odell, Jan 8, 2004
    #3
  4. "Frane Roje" <frane.roje(d*elete*)@st.hinet.hr> wrote in
    news:btjmkp$2pj$:

    > 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 ->
    --
     
    Mark A. Odell, Jan 8, 2004
    #4
  5. Rick

    Grumble Guest

    Grumble, Jan 8, 2004
    #5
  6. 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." |
    +---------+----------------------------------+-----------------------------+
     
    Kenneth Brody, Jan 8, 2004
    #6
  7. Rick

    Rick Guest

    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
     
    Rick, Jan 8, 2004
    #7
  8. Rick

    CBFalconer Guest

    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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Jan 8, 2004
    #8
  9. Rick

    Default User Guest

    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
     
    Default User, Jan 8, 2004
    #9
  10. Rick

    Rick Guest

    > 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
     
    Rick, Jan 9, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    13
    Views:
    734
  2. ravi
    Replies:
    0
    Views:
    481
  3. Peter
    Replies:
    34
    Views:
    2,055
    Richard Tobin
    Oct 22, 2004
  4. porting non-malloc code to malloc

    , Feb 18, 2005, in forum: C Programming
    Replies:
    3
    Views:
    503
    Walter Roberson
    Feb 19, 2005
  5. Johs32

    to malloc or not to malloc??

    Johs32, Mar 30, 2006, in forum: C Programming
    Replies:
    4
    Views:
    343
    Captain Winston
    Mar 30, 2006
Loading...

Share This Page