Thread: Allocate a big dynamic memory location

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    54

    Allocate a big dynamic memory location

    I want to allocate a big memory location, up to 4kb, and want it to be dynamic. However, I don't know how. Please help me! Thank you very much any help.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I want to allocate a big memory location, up to 4kb, and want it to be dynamic.
    Just 4kb?
    Code:
    T *mem = malloc ( sizeof *mem * 4096 );
    -Prelude
    Last edited by Prelude; 10-08-2002 at 10:44 AM.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    54
    Hi Prelude,

    Thanks! What is T?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Thanks! What is T?
    The data type of the block of memory you want to allocate. You didn't specify, so I used the generic notation T which means "Your data type here".

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    54
    O.k. Thanks you guys. I got it.

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Don't forget to free the memory when you don't need it anymore.

    free (mem);

  7. #7
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531
    Using type of void
    you can change it to other types in future..

    Isn't it possible Salem?
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Prelude
    >I want to allocate a big memory location, up to 4kb, and want it to be dynamic.
    Just 4kb?
    Code:
    T *mem = malloc ( sizeof *mem * 4096 );
    -Prelude
    I answered this same question for the same person in another thread. *mutter* Anyway, they asked how to allocate 4KB, not how to allocate four thousand integers, characters etc.

    So to be picky, this would be the preferred method, since it would give you exactly 4KB:

    T *mem = malloc( 4096 );

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You can also do allocations using a function called calloc() which allocates a zeroed out memory buffer.

    For instance
    Code:
    //i don't care what ppl say I still type cast
    int *mem = (int *)malloc(50 * sizeof(int));
    could also be done like this
    Code:
    int *mem = (int *)calloc(50, sizeof(int));
    The only difference is that calloc would make every byte in the array a zero whereas malloc leaves you with random values.

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    23
    Hi,

    Just out of intrest what happens if you dont free memory allocated in a program. I thought that the OS frees all resources given to a program on exit. So by this logic it would be ok not to free memory.

    I know this is bad thinking but suddenly realised that I dont know why?

    Brif

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I thought that the OS frees all resources given to a program on exit.
    Most do, but you can't be sure of this. My system will free resources after my program exits, but it will usually do so slowly so if I allocate huge amounts of memory, the computer will run sluggishly for about ten minutes after my program ends. It's best to assume that your system will not reclaim memory and clean up after yourself.

    -Prelude
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    23
    Thanks Prelude,

    youve sent me on the first steps to enlightenment,

    brif

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 03:06 PM
  2. dynamic memory allocating error
    By valhall in forum C Programming
    Replies: 2
    Last Post: 04-04-2003, 10:49 AM
  3. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  4. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM