ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
malloc is responsible for maintaining the brk pointer for the process. this pointer points to the top of the heap(it grows up on linux). malloc obtains more heap space in PAGE_SIZE chunks, usually 0x1000. it then gives this memory to you when u call malloc. when you allocate more memory than malloc has left in its current chunk, it will move the brk pointer ahead by calling brk() to get another 0x1000 size chunk. when you free some memory, it doesnt necessarily mean that malloc will call brk() to shrink the heap. so, even tho u gave back the memory, it is still below the brk pointer so its still a valid reference. example:
Code:
#include <unistd.h>
int main()
{
int y = 0;
char *x = NULL;
x = malloc(100);
if(!x)
return 1;
for(y = 0; y < 0x2000; y++)
printf("x[%d] = %c \n", y, x[y]);
return 0;
}
...
x[6838] =
x[6839] =
Segmentation fault
just showing you can way overstep your allocated memory as long as you stay below the brk pointer for the process. depending on how much you give back when you free, it may or may not actually move back the brk pointer.
Last edited by infamous41md; 02-13-2004 at 04:25 PM.
ohhh, one more thing - how do we get the position offff this brk ptr? is the brk ptr the same as the stack ptr? or are they different things? (yeah, im a bit confused.)
You can't really, it's an implementation detail -- as far as the ANSI C standard is concerned, writing to memory which has been free'd always has the potential to cause the program to crash, and the behaviour will vary from OS to OS. The brk pointer isn't the same as the stack pointer, btw (the stack pointer is actually held in a processor register, it's part of the inner workings of x86 processors).
sbrk increments the program's data space by increment
bytes. sbrk isn't a system call, it is just a C library
wrapper. Calling sbrk with an increment of 0 can be used
to find the current location of the program break.
As a piece of advice, following calls to free with a NULL assignment is not a bad thing to do. It is not foolproof, but can help you catch issues more easily.
Originally posted by cjcuk As a piece of advice, following calls to free with a NULL assignment is not a bad thing to do. It is not foolproof, but can help you catch issues more easily.
eg:
free(ptr);
ptr = (type *)NULL;
whenever i wrote a linked-list with structs, i would use free, and i never really checked to see if that memory was actually freed or not.
for some reason, im associating this behaviour more with native data types, than with objects i create, say with struct definitions.
2 questions here - should malloc be called there with a sizeof(*s)? or should it be sizeof(struct sss)? wont what has been coded be allocating 4 bytes, and not the size of the struct?
also, is there supposed to be an order in which we free() the allocated mem? would reversing the two free() calls above make a difference?
2 questions here - should malloc be called there with a sizeof(*s)? or should it be sizeof(struct sss)? wont what has been coded be allocating 4 bytes, and not the size of the struct? [1]
also, is there supposed to be an order in which we free() the allocated mem? would reversing the two free() calls above make a difference? [2]
thanks.
[1] - No, *s is a dereferenced pointer, thus is considered the type it is pointing to. It is probably better to use sizeof(struct sss) -- as I am not sure if the compiler will always be successful in dealing with a dereferenced pointer in such a way ( though, it should be ).
[2] - No, reversing the calls is fine. Dynamic memory allocation does not work like the stack ( which is LIFO ), you can allocate and deallocate as and when necessary -- otherwise it would not be very dynamic . The point behind malloc(3) is to try to speed up this process by using clever algorithms ( eg, doug lea's or phk's ) on how memory has been and will be ( predicted ) allocated.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.