Memory Functions - Still Learning

This is a discussion on Memory Functions - Still Learning within the C Programming forums, part of the General Programming Boards category; Originally Posted by tabstop Whenever you ask yourself "How many of these do I need?" and your answer is "I ...

  1. #16
    Odd Mental Process Alexander's Avatar
    Join Date
    Aug 2008
    Posts
    22

    Unhappy

    Quote Originally Posted by tabstop View Post
    Whenever you ask yourself "How many of these do I need?" and your answer is "I don't know."
    Well to be honest, I don't really know what the memory the is allocated by malloc and such is used for. I'm that far behind. Do I put variables in it or something? Because isn't that what the "int" and "double" is for? o.o I've looked it up and all it continues to say is memory. There is clearly something I'm not getting. o.o

  2. #17
    Odd Mental Process Alexander's Avatar
    Join Date
    Aug 2008
    Posts
    22
    Quote Originally Posted by cpjust View Post
    If you know you need exactly 5 ints, you can do this:
    Code:
    int nums[5];
    but if you don't know how many ints you need until you actually run the program, you need to allocate enough space at runtime:
    Code:
    int* nums = malloc( size * sizeof(int) );
    So, it makes a dynamic array type thing? o.o

  3. #18
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Alexander View Post
    Well to be honest, I don't really know what the memory the is allocated by malloc and such is used for. I'm that far behind. Do I put variables in it or something? Because isn't that what the "int" and "double" is for? o.o I've looked it up and all it continues to say is memory. There is clearly something I'm not getting. o.o
    int x; and double d; also allocate memory, but they allocate it on the stack (which gets deleted automagically when it goes out of scope), while malloc() allocates memory from the heap (a different area of memory that you need to deallocate yourself).

  4. #19
    Odd Mental Process Alexander's Avatar
    Join Date
    Aug 2008
    Posts
    22
    Quote Originally Posted by cpjust View Post
    int x; and double d; also allocate memory, but they allocate it on the stack (which gets deleted automagically when it goes out of scope), while malloc() allocates memory from the heap (a different area of memory that you need to deallocate yourself).
    Ah ok. I get most of that. I don't know much about "the heap" I guess then. So it's like higher priority global memory or something?

    (lol'd at the double d)

  5. #20
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Alexander View Post
    So, it makes a dynamic array type thing? o.o
    Yes, you can create an array or a single variable; it just depends on how much memory you allocate.
    i.e. malloc( 1 * sizeof(int) ) will allocate enough space for one int, but malloc( 5 * sizeof(int) ) will allocate an array of 5 ints.

  6. #21
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Alexander View Post
    Ah ok. I get most of that. I don't know much about "the heap" I guess then. So it's like higher priority global memory or something?

    (lol'd at the double d)
    Here's some pictures... that describe stack vs heap: http://www.cs.jcu.edu.au/Subjects/cp...pAndStack.html

  7. #22
    Odd Mental Process Alexander's Avatar
    Join Date
    Aug 2008
    Posts
    22
    Quote Originally Posted by cpjust View Post
    Yes, you can create an array or a single variable; it just depends on how much memory you allocate.
    i.e. malloc( 1 * sizeof(int) ) will allocate enough space for one int, but malloc( 5 * sizeof(int) ) will allocate an array of 5 ints.
    So if it's different from int, and double type things, how do I put data into it then? Is it different?

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Alexander View Post
    Well to be honest, I don't really know what the memory the is allocated by malloc and such is used for. I'm that far behind. Do I put variables in it or something? Because isn't that what the "int" and "double" is for? o.o I've looked it up and all it continues to say is memory. There is clearly something I'm not getting. o.o
    You put whatever the heck you want in there. "int" says "I want an int" and the compiler gives you exactly enough memory for an int. malloc says "Give me this much memory" and the compiler gives you that much memory for you to do whatever you need it to do. You can make it to 10 ints, or 5 doubles, or 40 characters (this is how strings work, for the most part), or just whatever.

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Alexander View Post
    So if it's different from int, and double type things, how do I put data into it then? Is it different?
    So you have a pointer to memory: int *bob means that bob is a pointer to an int (the memory that we get from malloc can store whatever, but this is telling us that we want to interpret it as an int); to actually refer to that memory we use the star: *bob = 11.

  10. #25
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Alexander View Post
    So if it's different from int, and double type things, how do I put data into it then? Is it different?
    Yes, you need to de-reference the pointer to get at the actual data that's being pointed to by using the * operator:
    Code:
    int num;
    int* pNum;
    pNum = malloc( sizeof(int) ); /* Assigning the pointer address. */
    num = 5;
    *pNum = 10; /* Assigning a value to the address stored in the pointer. */
    free( pNum );

  11. #26
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by Alexander View Post
    Well to be honest, I don't really know what the memory the is allocated by malloc and such is used for. I'm that far behind. Do I put variables in it or something? Because isn't that what the "int" and "double" is for? o.o I've looked it up and all it continues to say is memory. There is clearly something I'm not getting. o.o
    ok, say i have a program, and i want to open a text file (test.txt), but I dont know how big the file is. I could just make an F Huge array liek char data[104857600];, but that woudl eat up 100 MB and the texc file migth be much much shorter. If it is shorter, which it almost alwasy will be, then i haveneedlessly wastes system resources by allocating such a huge amoutn of memory. The solution is to allocate tHe memory i need at run time by using malloc(). So instead i use char* data; adn then allocate a much smaller amoutn of memory like 1KB, if the text fiel grows beyond that size I realloc() more memory until i get to the end fo the file, in this way i dont use more memory than i have to, and yet can have as much as I need. Not only that but the static array size is limited to text files smaller than 100MB, while the malloc method is limited only by available memory.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  12. #27
    Odd Mental Process Alexander's Avatar
    Join Date
    Aug 2008
    Posts
    22
    Quote Originally Posted by cpjust View Post
    Yes, you need to de-reference the pointer to get at the actual data that's being pointed to by using the * operator:
    Code:
    int num;
    int* pNum;
    pNum = malloc( sizeof(int) ); /* Assigning the pointer address. */
    num = 5;
    *pNum = 10; /* Assigning a value to the address stored in the pointer. */
    free( pNum );
    So wait, how can I continue to put more data in that variable then?

  13. #28
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Alexander View Post
    So wait, how can I continue to put more data in that variable then?
    You mean you want to make the array bigger and add more items? Use realloc()
    Code:
    int* pNums = malloc( sizeof(int) ); /* An array of 1 int. */
    pNums[0] = 5;
    pNums = realloc( pNums, 3 * sizeof(int) ); /* Now it can hold 3 ints. */
    pNums[1] = 10;
    pNums[2] = 666;
    But of course in real code (not just an example) you'd want to save the result of realloc() to a temporary pointer first and check if it's NULL, otherwise you could have a memory leak if you overwrite your pointer with NULL without freeing it first.

  14. #29
    Odd Mental Process Alexander's Avatar
    Join Date
    Aug 2008
    Posts
    22
    Quote Originally Posted by cpjust View Post
    You mean you want to make the array bigger and add more items? Use realloc()
    Code:
    int* pNums = malloc( sizeof(int) ); /* An array of 1 int. */
    pNums[0] = 5;
    pNums = realloc( pNums, 3 * sizeof(int) ); /* Now it can hold 3 ints. */
    pNums[1] = 10;
    pNums[2] = 666;
    But of course in real code (not just an example) you'd want to save the result of realloc() to a temporary pointer first and check if it's NULL, otherwise you could have a memory leak if you overwrite your pointer with NULL without freeing it first.
    So according to that code, I can't just do this?

    Code:
    int* pNums = malloc( sizeof(int) ); /* An array of 1 int. */
    pNums[0] = 5;
    pNums[1] = 10;
    pNums[2] = 666;
    So I do have to add the realloc? Or will this still work?

  15. #30
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Yes, you need to realloc() there, otherwise you'll be writing off the end of your array into memory that doesn't belong to that variable.
    It's like trying to pour 3 cups of coffee into 1 coffee cup. First you need to get 2 more cups, otherwise you'll just make a big mess.

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  2. Reallocating Memory
    By Booie2k1 in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 07:09 AM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Accessing Video Memory Information...need help
    By KneeLess in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2003, 04:53 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 10:37 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21