Copyright 2004 by M. Uli Kusterer Tue, 30 Dec 1969 07:58:58 GMT Comments on article book5 at Zathras.de http://www.zathras.de/angelweb/book5.htm book5 Comments witness_dot_of_dot_teachtext_at_gmx_dot_net (M. Uli Kusterer) witness_dot_of_dot_teachtext_at_gmx_dot_net (M. Uli Kusterer) en-us Comment 29 by Boyd http://www.masters-of-the-void.com/book5.htm#comment29 http://www.masters-of-the-void.com/book5.htm#comment29 Great tutorial!

GNU C extends standard C by permitting array size to be declared by using variables rather than only constants. Do these arrays get put on the stack or on the heap? Is there a down-side to doing this rather than using malloc?
Comment 28 by Mohammed Rizwan http://www.masters-of-the-void.com/book5.htm#comment28 http://www.masters-of-the-void.com/book5.htm#comment28 Wonderful article. Never seen clear once such explanation on stack and heap.

But I am little confused about heap-linked list.

1) As per my understanding, Starting first 4 byte of heap will have the address of the first "used" block of memory address. The first "used" memory block will have a header to indicate size and a next pointer.

What does that next pointer has? Is it the next available free memory chunk? If yes, is it required to go through the entire chain to find the next free available chunk in the heap?

2) Suppose if 1 allocated memory say starting at 1001 ending at 1005 between 2 chunks (1000 and 1006) needs some more memory. In that case, this will be moved from 1001 to 1005 with some bigger chunk say 3000 to 3010 and the next pointer of this new chunk, if it has to point to the next free available free chunk,

will it be pointing to the "freed up" chunk location 1001 or the new adjacent free location say 3011?

3) If above question(question 2) points to 1001, and if a request comes to allocate more memory chunk than the available size of 1001 to 1005 (which already exits between 2 allocated chunks),

How it will find the new free chunk of size > than 1001 to 1005?

(or)

If above(question 2) points to 3011,
How will the chunk 1001 to 1005 will be used?

Looking forward for your reply.

With regards,
Mohammed Rizwan N.
Comment 27 by ief2 http://www.masters-of-the-void.com/book5.htm#comment27 http://www.masters-of-the-void.com/book5.htm#comment27 ief2 writes:
@jeff
That's indeed right jeff. Try the code below and you'll see that that i is 3 at the end

int main (int argc, const char * argv[]) {
int i = 2;
int* pd;
pd = &i;
*pd = 3;

printf("i: %d", i);
}
Comment 26 by jeff http://www.masters-of-the-void.com/book5.htm#comment26 http://www.masters-of-the-void.com/book5.htm#comment26 nice article. I'm still trying to get my head around pointers. I'm sure there are many uses but all i can come up with is the following example for explanation purposes.

int i = 2;
int* pd;
pd = &i;
*pd = 3;

So if pd is a pointer to type int, and the address of i is assigned to pd, then we can directly manipulate i by changing the value that pd points which is i. So in a sense we have an "is a" relationship between the pointer and the address that it points to. Is that a correct understanding?
Comment 25 by Selim http://www.masters-of-the-void.com/book5.htm#comment25 http://www.masters-of-the-void.com/book5.htm#comment25 this is the best tutorial i've ever read
Comment 24 by jo.ke http://www.masters-of-the-void.com/book5.htm#comment24 http://www.masters-of-the-void.com/book5.htm#comment24 thanks Uli
just wondering about the last bit of this section (What does that have to do with pointers?) Is the address of the first character in the example (atkinson) not 27, you state that it is 7?

thanks again

jk
Comment 23 by Uli Kusterer http://www.masters-of-the-void.com/book5.htm#comment23 http://www.masters-of-the-void.com/book5.htm#comment23 Uli Kusterer writes:
Alex, thank you, I've corrected the typo.
Comment 22 by Alex Blewitt http://www.masters-of-the-void.com/book5.htm#comment22 http://www.masters-of-the-void.com/book5.htm#comment22 Alex Blewitt writes:
"walk over to that adress" is a typo - should be "address". Otherwise, good article :-)
Comment 21 by Uli Kusterer http://www.masters-of-the-void.com/book5.htm#comment21 http://www.masters-of-the-void.com/book5.htm#comment21 Uli Kusterer writes:
@Mahmoud: It's not a constant 8 bytes, but rather 8 bytes *per block* of memory you request (e.g. using malloc()). Because each block needs to remember its own size and the address of the next block in the linked list, and you need room to store this information, in addition to the actual data for the block.

However, you are right that, to find out which block of memory is still unused, you would probably have to look at more than 8 bytes, because you'd have to follow the list. OTOH, to actually use a block, you just remember its address elsewhere, so you can access it directly and don't have to walk the whole chain each time.
Comment 20 by Mahmoud http://www.masters-of-the-void.com/book5.htm#comment20 http://www.masters-of-the-void.com/book5.htm#comment20 Thanks man. Abselutely great!

I'm wondering why 8 bytes for heap. As far as I understood, this depends on the location of the heap memory assigned to the app by malloc.

as far as i understand, multiple mallocs, could get different locations, and due to "linked list", it may be more than 8 bytes to reach the ultimate chunk of heap memory.
Comment 19 by Benji Raps http://www.masters-of-the-void.com/book5.htm#comment19 http://www.masters-of-the-void.com/book5.htm#comment19 I am a bit confused about the "address of" operator. We have been using the "&" operator in our calculator program when we enter in values for our variables. It seems that when we enter a value for a variable with user inputted text, we are doing so by first identifying the address of the variable. However, we seem to be able to set our boolian variables to true or false without the "&" sign. We also seem to be able to recall the value of the variables without the "&". Can you clarify this for me?

Thanks - I am really enjoying your tutorial.
Comment 18 by Andrew Harwood http://www.masters-of-the-void.com/book5.htm#comment18 http://www.masters-of-the-void.com/book5.htm#comment18 So i am confused. I understand what a pointer is and that it merely points to piece of memory. But, why use pointers? What good are they?
Comment 17 by Frank http://www.masters-of-the-void.com/book5.htm#comment17 http://www.masters-of-the-void.com/book5.htm#comment17 Great job!!
Comment 16 by kiran parimi http://www.masters-of-the-void.com/book5.htm#comment16 http://www.masters-of-the-void.com/book5.htm#comment16 Excellent work clean, clear and good visual explanation, I would suggest this as first place for any one for memory management knowledge
Comment 15 by Richard Howell http://www.masters-of-the-void.com/book5.htm#comment15 http://www.masters-of-the-void.com/book5.htm#comment15 This tutorial is exactly what I need - thank you for putting it together.
As for pointers, I just looked over it once and my understanding of it is hazy, but coming together. I'll look over it again soon and it should start to clear up.
Thanks!
Comment 14 by Garotas* http://www.masters-of-the-void.com/book5.htm#comment14 http://www.masters-of-the-void.com/book5.htm#comment14 In the example how you would store the number 19 in 10 digits, I think you should write it the other way around. I don't know if I understood the concept, but I think it shouldn't be 1*10, and then 9*1, together 19.
If you got a variable int m; and m = 4, you can say *m += 1 and after that m = 5 ? right?
And the malloc() and realloc() functions only work an the heap? What do we use them for?
Comment 13 by Jeremy http://www.masters-of-the-void.com/book5.htm#comment13 http://www.masters-of-the-void.com/book5.htm#comment13 Shouldn't "As you will see later, this numbering scheme is used in C to refer to items in arrays, and to characters of strings." and actually read "characters in strings" or "strings of characters"?

BTW, thanks for the tutorial, I am learning quite a bit!
Comment 12 by Uli Kusterer http://www.masters-of-the-void.com/book5.htm#comment12 http://www.masters-of-the-void.com/book5.htm#comment12 Uli Kusterer writes:
"makes a note" means that is written into the compiled code. The computer generates machine instructions that take the stack pointer, add a fixed number to it, and then fetches the variable value you're referring to from that address, or writes it there.
Comment 11 by Charles Marshall http://www.masters-of-the-void.com/book5.htm#comment11 http://www.masters-of-the-void.com/book5.htm#comment11 If the stack pointer identifies the next unused chunk of memory, what tells the system where previously stored variables are? (in the stack) I understand about byte addresses but I'm unclear about: "whenever you declare a variable named "myCharacter", the computer picks the first empty box on the paper (e.g. box number 32), and makes a note that whenever you're saying "myCharacter", you're actually referring to the contents of box no. 32." What is "making a note"? Is that what malloc() does?
Comment 10 by shivram popalghat http://www.masters-of-the-void.com/book5.htm#comment10 http://www.masters-of-the-void.com/book5.htm#comment10 I want more information about the memory manegment,process & processor managment!
thank you!!!
Comment 9 by Uli Kusterer http://www.masters-of-the-void.com/book5.htm#comment9 http://www.masters-of-the-void.com/book5.htm#comment9 Uli Kusterer writes:
John,

0100000 is actually 32 and 0010000 is 16. 01000000 (one additional zero) would be 64. That's because it's: 0 * 128 + 1 * 64 + 0 * 32 + 0 * 16 + 0 * 8 + 0 * 4 + 0 * 2 + 0 * 1. So you have six zeroes to the right of a 1 (leading zeroes could be left out just like when writing any number, but I wrote it as 8 digits here because that's how much digits this byte can contain at most).
Comment 8 by John McAdams http://www.masters-of-the-void.com/book5.htm#comment8 http://www.masters-of-the-void.com/book5.htm#comment8 This is a wonderful tutorial. I am using it along with "Learn C on the Mac" by Dave Mark. As you mentioned, pointers are causing some head scratching and this article helps shed more light on them.

I have read this article a few times and think there is a mistake near the top.
0100000 = 64? Isn't that equal to 128?

Shouldn't that be
0010000 = 64?
Comment 7 by Scott http://www.masters-of-the-void.com/book5.htm#comment7 http://www.masters-of-the-void.com/book5.htm#comment7 "Many modern computers are smart enough to keep the stack and the heap from colliding, and thus do not really keep them at opposite ends of memory anymore. Still, the way they do it would involve lots of information about page tables and virtual memory, and aren't really relevant to understanding the basics of memory."

Any chance you could provide a link to an article that gives a good explanation? Or perhaps you might consider digging into this subject in one of your own blog articles. This is one of those black-box areas in my understanding of how all this works that would be interesting to learn more about. I totally understand why you don't get into the ugly details in a tutorial of this nature, but it might make for an interesting article in its own right.
Comment 6 by Mk12 http://www.masters-of-the-void.com/book5.htm#comment6 http://www.masters-of-the-void.com/book5.htm#comment6 Why do you prefix all the variables with "v"?
Comment 5 by Uli Kusterer http://www.masters-of-the-void.com/book5.htm#comment5 http://www.masters-of-the-void.com/book5.htm#comment5 Uli Kusterer writes:
Matt, I've rephrased a few spots, going over it with your question in mind, and I hope the article now makes it more obvious what a pointer is. Thanks for letting me know about your issues understanding it. There were one or two spots in there that really benefited from me looking at it again with the question "What is a pointer" in mind. Knowing this stuff already, I sometimes mentally add words that aren't really there, and you pointed out one such occasion to me.
Comment 4 by tadej5553 http://www.masters-of-the-void.com/book5.htm#comment4 http://www.masters-of-the-void.com/book5.htm#comment4 Mind your language! ... err ... Well, when you keep a memory address (i.e. a pointer) in a variable, what you have in this variable is not the memory itself. Rather, you have a "reference" there. Now, the problem with this reference is that all operations you do (like add, subtract ...) work on the reference, i.e. they change the address, they perform pointer arithmetics.

Thats your definitio mat
Comment 3 by Matt http://www.masters-of-the-void.com/book5.htm#comment3 http://www.masters-of-the-void.com/book5.htm#comment3 Maybe it's my attention span but...
After reading the whole article and having a headache, I didn't get the point or even the definition of what a pointer was.

Could you guys help me out?
Comment 2 by Ryan Stonebraker http://www.masters-of-the-void.com/book5.htm#comment2 http://www.masters-of-the-void.com/book5.htm#comment2 Ryan Stonebraker writes:
Awesome!!! thanks! maybe from what i've learned, i'll make a program that advertises your website!
Comment 1 by Balázs Szilvási http://www.masters-of-the-void.com/book5.htm#comment1 http://www.masters-of-the-void.com/book5.htm#comment1 Really good article!