0

So, as far as I know, malloc returns a pointer to a location in the heap.

sbrk() expands the heap by a minimum of 4k

If I could not use malloc, for reasons, would it be possible in c to allocate memory to a new member if a linked list (for example) by using an adress withing that 4k that has been gained by an sbrk() call, instead of a malloc?

Sorry if this is a dmub question :(

EDIT: The reason I can't use malloc is because my task is to create my own malloc and am told to use sbrk.

trincot
  • 317,000
  • 35
  • 244
  • 286
Real Name
  • 19
  • 5
  • 1
    One think I do sometimes, is to allocate an array of, say, node structures in one go, and then use them up without calling `malloc` again until I run out. – Weather Vane Nov 01 '19 at 20:50
  • But I can't use malloc at all, anywhere. – Real Name Nov 01 '19 at 20:51
  • 1
    It is common for embedded systems to manage their own memory--no malloc/free, no sbrk (that requires an OS). But if you are going to use the stdio library, it expects to have control of the heap. Doing things on your own could quite possibly screw up some other part of the standard library (for example, things like printf() and scanf() may call malloc() behind your back). – Lee Daniel Crocker Nov 01 '19 at 20:53
  • 1
    Out of curiosity, why the aversion to `malloc`? – Reticulated Spline Nov 01 '19 at 20:54
  • No `malloc`? You could use the same technique with a static array, of a size known to be enough. Just FYI I implement `free()` in that situation by adding the node to another linked list. So when I need a node, it comes from the following priority of actions a) from that linked list, b) from the array, c) reallocate the array. Then at the program end I just leave it for the OS to reclaim. – Weather Vane Nov 01 '19 at 21:04
  • Use `calloc()`? – LegendofPedro Nov 01 '19 at 21:05
  • The simple answer is that, yes, it's possible to create a linked list that allocates its own nodes from a pre-allocated (however you do it) block of memory. You could, for example, just allocate a huge `byte` array and assign your own pointers within that block. Whether you can use `sbrk` in your situation depends on whether anything else in your program will be calling `malloc` or related memory allocation functions, and how they interact with `sbrk`. – Jim Mischel Nov 02 '19 at 00:28

2 Answers2

2

brk and sbrk have been removed from POSIX; they are not portable. Some malloc implementations use sbrk, but don't necessarily expect application code to be moving the break address, and so using sbrk can break malloc. Even if your code doesn't use malloc, you might not be able to get away with breaking malloc, because some other code in your process might be calling it.

To obtain pages memory more directly from the operating system kernel relative to malloc in a modern POSIX environment, a better choice is the mmap function (with MAP_ANON and MAP_PRIVATE flags).

Kaz
  • 55,781
  • 9
  • 100
  • 149
0

brk and sbrk are an alternative (deprecated) way of allocating heap memory instead of using malloc. In general, you should never use them in a program that uses malloc, but if your program never uses malloc (and never uses any stdlib functions that use malloc, such as stdio), you can use them instead to manage the heap.

sbrk can be used like malloc to allocate some additional heap space (the amount specified by its argument). brk can be used like free, except it frees the block specified AND EVERYTHING ALLOCATED AFTER IT by later calls to sbrk.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226