Results 1 to 14 of 14

Thread: Allocating memory in C++

  1. #1
    ChimpFace9000
    Guest

    Post Allocating memory in C++

    I need to allocate a 64000 byte array to use as a virtual screen. I tried using "new char[64000]" but that returns a null pointer every time. How do i allocate that much memory in C++? (Im trying to stay away from C code because id have to include extra header files.)

  2. #2
    Zaei
    Guest
    Code:
    void* p = malloc(64000);
    Using "new" includes the malloc.h header anyway.

    Z.

  3. #3
    jim mcnamara
    Guest
    malloc returns a void pointer (like Zaei showed).

    For chars try:

    char *p;
    p = (char *) malloc(64000);
    Sine they are chars
    p = (char *) calloc(64000);

    This inits memory to all zero (null terminators) as well as allocating it. That way, various kinds of string operation will not choke.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    uses a lot of heap memory. If you want virtual memory:
    Code:
    BYTE* pByte = (PBYTE) VirtualAlloc(NULL, 65000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
    but for 65 k, this could be overkill, it is rather for memory amounts starting with 100k
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Megatron
    Guest
    There's also the LocalAlloc function.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    LocalAlloc and GlobalAlloc are deprecated.

    Anyway, new doesn't return NULL - it throws an exception. I just tried, and I could allocate more than 64K...are you using a 16-bit, non-standard compiler by any chance?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    ChimpFace9000
    Guest
    Yup, im using turbo c++ 3.0. Right now though im downloading watcom c/c++, hope its better.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Ah...if it's 16-bit I don't think you can allocate an array larger than one segment...although 64000 is less than 65535

    Watcom came with a 32-bit DOS extender so you should be fine
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9
    Zaei
    Guest
    I dont know if it will work, but you may try "byte huge scr[64000]". I found this in some 16-bit code I have laying around on the HDD. It is C code, but you might as well try it =).

    Z.

  10. #10
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Hi ChimpFace9000, if you always want to have 64000 chars, why not, instead of allocating memory, declare

    char XX[64000];

    Ok back to the point, I don't see why it always return null pointer.
    Is it bcos of 16 bit compiler(TC 3.0)? The total data memory used by the program is more than 65535?

    Let's hope watcom C++ solve your problem.
    I'm a VB6 beginner.

  11. #11
    ChimpFace9000
    Guest
    Ok i downloaded Watcom c++, and it sucks.
    Are there any other modern dos C++ compilers? Dont say DJGPP, i hate that compiler. An exe from it is like 200 kb! Thats un-acceptable.

  12. #12
    Zaei
    Guest
    In DOS, you should be able to just take the memory, without allocating it. Just assign the memory location to a pointer, and use it. Sure, you might screw up some other program, but big deal (it's DOS =) =).

    Z.

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    maybe you should check the compile options of DJGPP. Jump'n'Bump was compiled with DJGPP and it is only 67k.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I think the linker will accept COFF .obj files, so if that's what the assembler produced. You'd also need a header file for them (set up with whatever calling convention your assembler procs use).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width