Talk:C Programming/Memory management

Untitled

 * Removed the casts that were used with some of the allocation examples. In C, the cast is superfluous and can hide the serious problem of a missing prototype.  Also, the explanation about free returning a value was incorrect and hence removed.  free's prototype is: void free(void*).  That is, it returns nothing. -- 67.170.85.37 04:00, 3 January 2006 (UTC)

How much memory can you allocate?
I've got a 64-bit (x86) computer, but a 32-bit compiler. Can I allocate the whole 32-bits (byte*2^32)?


 * No. If you told the compiler to create a 32-bit-address executable, then that program cannot successfully allocate the entire 32-bit-address address space.
 * If you are unlucky, when you try to allocate the full 2^32 bytes all a once, the compiler will truncate the amount of memory requested (2^32 bytes) into a 32-bit value of zero, then the malloc call (" ") will succeed, and later parts of your program will likely have confusing and difficult-to-diagnose problems.
 * A 32-bit address can access at most 2^32 possible addresses.
 * Even when your 64-bit computer has many times more than 2^32 bytes of unused RAM, a 32-bit-address executable will never see a full 2^32 bytes of unused RAM.
 * Some long-running programs allocate more and more memory a little at a time --
 * the early malloc calls will typically succeed, but eventually the executable will see malloc return NULL ("not enough consecutive free space available") once all free space is used up.
 * For a 32-bit-address executable, "all free space" is always *less* than "all 2^32 possible addresses", because the compiler and the OS puts various things where your program can access it, using up some of those addresses.


 * Yes. Your program can call malloc requesting 2^32 bytes, if you told the compiler to create a 64-bit-address executable and it links in a 64-bit-address version of malloc: " ". However, as always, malloc is not guaranteed to always give you what you want -- sooner or later, the heap will be fragmented enough that there is no block of 2^32 consecutive bytes available, and malloc will return NULL -- see C Programming/Memory management.


 * What should we say about "How much memory can you allocate?" in this "C Programming" book?


 * --DavidCary (discuss • contribs) 14:36, 17 December 2014 (UTC)

memset not mentioned
Every once in a while you run across memset, that is c? isn't it? we'll maybe it should be mentioned in this book. From my experience it "zeros" out a section of memory - zero really isn't accurate as you can write other values. Should memset be included? does it belong in the memory chapter or the in a section about buffers? I don't have a degree so, I just make the comments from the peanut gallery. 135.84.127.136 (discuss) 23:25, 17 March 2017 (UTC)

Assert
Could be used assert for managing memory allocation failures?

Clarification needed - "Sometimes it is not known at the time the program is written how much memory will be needed for some data."
Though this is likely very clear to an experienced C programmer, it is not to a novice, and in general I think vague statements like these weaken the text. A concrete example would do much good; does this mean "For example, when you have an array, but don't know how many elements it will have", or "When you have an array of fixed length, but don't know how many will actually end up being occupied, because it's dependent on user input", or whatever is correct. (I would but, being a novice to C, I don't know which it is!) Meatnordrink (discuss • contribs) 13:59, 17 December 2020 (UTC)

Use of `this` in final example of constructor/destructor functions confusing
As `this` is a reserved keyword in many languages, using it in this example (where, as far as I can tell, it's just a parameter name), is confusing for someone just coming to C, which this book is intended for. I held off on changing it myself, as I'm new to C, and not 100% sure I'm correct that it's just a parameter name here. Meatnordrink (discuss • contribs)