Questions tagged [sbrk]

**sbrk** is a basic memory management system call used in Unix and Unix-like operating systems to control the amount of memory allocated to the data segment[clarification needed] of the process.

sbrk is a basic memory management system call used in Unix and Unix-like operating systems to control the amount of memory allocated to the data segment[clarification needed] of the process. These calls are typically made from a higher-level memory management library such as malloc. In the original Unix system, brk and sbrk were the only ways in which applications could acquire additional data space; later versions allowed this to also be done using the mmap call

97 questions
5
votes
2 answers

About sbrk() and malloc()

I've read the linux manual about sbrk() thoroughly: sbrk() changes the location of the program break, which defines the end of the process's data segment (i.e., the program break is the first location after the end of the uninitialized data…
Euclid Ye
  • 501
  • 5
  • 13
5
votes
2 answers

How do you dynamically allocate memory in Mac OS X assembly?

I would like to dynamically allocate memory from an assembly program that does not link against the standard C library. Since brk(2) and sbrk(2) are unavailable on Mac OS X (10.6.2), what are the alternatives? (I'm guessing that it involves a Mach…
Scott Bell
  • 51
  • 2
5
votes
3 answers

Memory allocation in C

the following is a very very simple version of malloc() and seems to allocate some space to me, but apart from the fact that there is no free() and I don't check if I've overrun the allocated space, how can I check that the code is correct? Any…
Radek
  • 3,913
  • 3
  • 42
  • 37
5
votes
2 answers

Linux sbrk() as a syscall in assembly

So, as a challenge, and for performance, I'm writing a simple server in assembly. The only way I know of is via system calls. (through int 0x80) Obviously, I'm going to need more memory than allocated at assemble, or at load, so I read up and…
Jon Weldon
  • 115
  • 2
  • 8
4
votes
1 answer

Memory allocator in C -- how to utilize sbrk()'ed space

I've been writing an implementation of malloc and was wondering if someone could help me with this problem. Basically, I would like to reuse memory after allocating it using sbrk(), and having made certain that the memory is free. So essentially,…
Navin Aggrawal
  • 189
  • 1
  • 3
  • 12
4
votes
2 answers

Why does `alloca` not check if it can allocate memory?

Why does alloca not check if it can allocate memory? From man 3 alloca: If the allocation causes stack overflow, program behavior is undefined. … There is no error indication if the stack frame cannot be extended. Why alloca does not / can not…
user4385532
4
votes
2 answers

malloc() in newlib : does it waste memory after one big failure allocation?

I am writing an embedded software for STM32F7 and my libc is newlib-2.4.0.20160527. I have implemented _sbrk() as follows: extern intptr_t g_bss_end; /* value after the last byte in .bss */ extern intptr_t g_msp_lim; /* stack buffer starts at this…
Piotr Jedyk
  • 361
  • 1
  • 10
4
votes
1 answer

Is there a way to use functions from unistd.h in Rust code?

I'm trying to implement a malloc type function, but I can't figure out what to use instead of the sbrk function found in unistd.h for C. Is there any way to FFI unistd.h into a Rust program?
Harvey Adcock
  • 929
  • 1
  • 7
  • 16
4
votes
1 answer

Check memory allocation w/o valgrind

writing my own version of malloc() (and his best friend Mr. free()), I need to know if I free'd my memory correctly. Seems that I can't use valgrind because it checks for the libc'malloc, and mine is in a shared library loaded with…
3
votes
0 answers

Efficient 2 pass using heap memory

I have an algorithm that requires making two passes a file's data. The file may be stdin or stream (like a |) since this is a command line tool, which makes me unfortunately (to the best of my knowledge) rule out mmap. I require the information from…
user129393192
  • 797
  • 1
  • 8
3
votes
4 answers

Where does malloc() allocate memory? Is it the data section or the heap section of the virtual address space of the process?

Ever since I was introduced to C, I was told that in C dynamic memory allocation is done using the functions in the malloc family. I also learned that memory dynamically allocated using malloc is allocated on the heap section of the…
Abhishek Ghosh
  • 597
  • 7
  • 18
3
votes
1 answer

How to free memory that has been allocated by sbrk()? Can I use munmap?

I am having a hard time understanding sbrk() and mmap(), as well as munmap(). This is closely related to How do I free memory obtained by sbrk()? but I have more questions. If I have the following program: int main(void) { void* first =…
Juan Pablo
  • 338
  • 2
  • 17
3
votes
1 answer

How does sbrk() work?

I'm trying to understand how sbrk works. Here is my little code: int main() { printf("end of the break : %p\n", sbrk(0)); printf("end of the break : %p\n", sbrk(10)); printf("new end of the break : %p\n\n", sbrk(0)); } This outputs:…
Myranova
  • 123
  • 1
  • 10
3
votes
2 answers

Replacing `sbrk` with `mmap`

I've read that sbrk is a deprecated call and one should prefer mmap with MAP_ANONYMOUS flag. I need one continous (logical) memory block that can grow. However, mmap treats first parameter as a hint, so it can make gaps, which is unacceptable in my…
Alexey Andreev
  • 1,980
  • 2
  • 17
  • 29
3
votes
1 answer

Casting a pointer to an int

I am writing my own functions for malloc and free in C for an assignment. I need to take advantage of the C sbrk() wrapper function. From what I understand sbrk() increments the program's data space by the number of bytes passed as an argument and…
user3268401
  • 319
  • 1
  • 7
  • 21