Questions tagged [brk]

brk() and sbrk() change 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 segment).

From the Linux man page:

brk() and sbrk() change 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 segment). Increasing the program break has the effect of allocating memory to the process; decreasing the break deallocates memory.

brk() sets the end of the data segment to the value specified by addr, when that value is reasonable, the system has enough memory, and the process does not exceed its maximum data size (see setrlimit(2)).

sbrk() increments the program's data space by increment bytes. Calling sbrk() with an increment of 0 can be used to find the current location of the program break.

See also this SO question.

46 questions
1
vote
0 answers

Can the pointer returned by sys _brk overlap the stack on Linux?

Based on info in https://man7.org/linux/man-pages/man2/brk.2.html, the brk Linux system call works like this: void *sys_brk(void *desired_break) { return (void*)syscall(SYS_brk, desired_break); } It changes the virtual memory mapping of the…
pts
  • 80,836
  • 20
  • 110
  • 183
1
vote
1 answer

In what circumstances can malloc_trim(0) cause a crash?

I have a piece of code where I am using malloc_trim(0) to release any unused memory back to the system. But very intermittently I am see that it causes a crash. Backtraces below: Program terminated with signal SIGSEGV, Segmentation fault. #0 mtrim…
Praveen
  • 13
  • 3
1
vote
1 answer

Own Malloc implementation freeze at brk

for practice I'm currently trying to write my own malloc function in c. So my question is, after some allocation from the heap my program will freeze. I found the code segment which will couse the freeze and it'll freeze when i call the brk sys…
XaniXxable
  • 109
  • 1
  • 9
1
vote
1 answer

Why isn't argument of brk(void *end_data_segment), rounded up to the next page boundary?

From The Linux Programming Interface: int brk(void * end_data_segment ); The brk() system call sets the program break to the location specified by end_data_segment. Since virtual memory is allocated in units of pages, end_data_segment is…
Rick
  • 7,007
  • 2
  • 49
  • 79
1
vote
0 answers

In which case, echo can append the NULL characters unexpectedly

I'm working on a mini machine(MEM: 256M), its OS is linux. I wrote a bash script, which would be executed while the system is booting (executing command is added into /etc/rc.local). In this script, there is a command echo "a_long_string" >>…
Yves
  • 11,597
  • 17
  • 83
  • 180
1
vote
2 answers

multiple malloc calls internally calling mmap only once

When I try the below code I am not clearly able to analyze malloc api internal calls.What I am not clear is about the system call mmap is called only once for 2 or more malloc calls.If I am assigning more then 4069 bytes also it is calling only one…
Harish
  • 341
  • 1
  • 13
1
vote
2 answers

How to find the current location of the program break

I tried adding this inside the brk system call function : void *addr = sbrk(0); printk("current-add-is-%p-\n", addr); But it returned error during kernel compilation that implicit declaration of sbrk function. And I could not find where sbrk is…
Arjun Bora
  • 439
  • 2
  • 8
  • 20
1
vote
2 answers

Is memory cleared by the Linux kernel when brk is reduced then increased again?

I'm just wondering about what happens to memory that a user program releases through a brk system call, then gets back again. Does the kernel clear it out or is the contents left undefined? I believe that the kernel clears out pages when they are…
Timothy Jones
  • 133
  • 1
  • 6
1
vote
1 answer

Opinions and suggestions regarding my approach to first fit malloc function

I'm writing a malloc function for a college assignment. Here's a basic layout of my idea: 1)Define a node struct with pointers to previous node, next node, as well as a char for size and vacancy. Each region in the heap will contain a hidden node…
rmh52
  • 193
  • 1
  • 1
  • 4
0
votes
0 answers

segmentation fault when calling brk() and printf()

Note: sys_brk returns the program break while brk() returns 0 or -1.(According to manual brk(2), NOTES) The following code can pass. #include #include #include #include #include int main…
barbyQAQ
  • 45
  • 5
0
votes
1 answer

Why using both malloc/calloc/realloc and brk functions will results in undefined behavior?

Does this means using one of (malloc or calloc or realloc) and one of(brk/sbrk) concurrently results in UB or using both malloc and calloc can also cause UB? This happends through the entire program or just a source file?
0
votes
0 answers

Too many brk() noticed in strace

I have a c++ service, where I am trying to debug the cause of high service startup time. From strace logs I notice a lot of brk() (which contributes to about 300ms, which is very high for our SLA). On studying further , I see brk() to be a memory…
0
votes
2 answers

Heap break will not change after free()?

Code: int main() { printf("entering main. %p\n", sbrk(0)); void* ptr = malloc(300 * 1024); memset(ptr, 0xBE, 300 * 1024); printf("Allocated memory. %p\n", sbrk(0)); free(ptr); printf("Freed memory. %p\n", sbrk(0)); …
Praveen
  • 13
  • 3
0
votes
0 answers

Memory allocation of program without any allocation syscalls

I am currently working on a C program in Debian. This program at first allocates several gigabytes of memory. the problem is that after the startup of the program, still it is allocating memory. I checked and there is no malloc or calloc or etc. in…
hpirlo
  • 133
  • 1
  • 12
0
votes
0 answers

Problems with malloc

int main(int argc, char ** argv) { char *p[10]; for(int i =0 ; i < 10; ++i) { ssize_t msize = pow(2,i) * sizeof(char); p[i] = (char*)malloc(msize); printf("%p\n",sbrk(0)); } printf("malloc done!\n"); …
CodeFish
  • 1
  • 1