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
3
votes
1 answer

MIPS dynamic memory allocation using sbrk

I was trying to use sbrk for dynamic memory allocation. But, being a newcomer to SPIM and MIPS, I was unable to do so. I sketched out a rough code for the same. .data var: .word 25 .text main: li $v0, 9 la $v0, var …
Sahil Babbar
  • 143
  • 1
  • 2
  • 8
3
votes
1 answer

can sbrk(0) fail?

I'd like to know if someone already seen sbrk(0) fail ? I mean, if you can reach this function you obviously had the rights to access the memory before, so to check the current break location should be ok, right ? EDIT : Should I consider an error…
2
votes
2 answers

Why malloc and sbrk returns address from seperate segments?

I am trying to implement understand how dynamic memory allocation is happening. So I thought of implementing malloc of my own using sbrk() system call. My Question here is when i try to allocate dynamic memory, sbrk() and malloc() returns different…
2
votes
0 answers

Setting memory beyond the program break doesn't segfault

I'm trying to learn more about memory management (mmap, brk, sbrk) genuinely confused as to how: char *ptr = sbrk(0); char *ptr2 = ptr + 100000; *ptr2 = 8; printf("%d\n", *ptr2); Doesn't trigger a segmentation fault? I'm compiling…
tmte
  • 21
  • 2
2
votes
1 answer

Why is the return value of the first sbrk different than subsequent calls?

I am trying to understand how memory works in C, so am experimenting with the sbrk function now. I know that sbrk(0) should return the current program break, that is the end of the data segment. So I tried to call sbrk(0) multiple times and for some…
aboria
  • 123
  • 6
2
votes
2 answers

What is the difference between passing sbrk() an intptr_t vs int in C

It seems to have no impact on the functionality of sbrk, but in sbrk()'s documentation it says that it requires an intptr_t parameter. It works (or at least seems to work) when passing an int type as a parameter. This is in regular C.
Bob
  • 159
  • 1
  • 5
2
votes
1 answer

What is the aligment requirements for sys_brk

I'm using sys_brk syscall to dynamically allocate memory in the heap. I noticed that when acquiring the current break location I usually get value similar to this: mov rax, 0x0C mov rdi, 0x00 syscall results in rax 0x401000 The value usually 512…
St.Antario
  • 26,175
  • 41
  • 130
  • 318
2
votes
3 answers

Why virtual method generates undefined reference to _sbrk?

This compiles gracefully: class dummy { }; This complains about undefined reference to _sbrk: class dummy { virtual ~dummy(); }; Why virtual method generates undefined reference to _sbrk? I used to think that vtable is allocated somewhere…
Piotr Jedyk
  • 361
  • 1
  • 10
2
votes
1 answer

Alignment issues when assigning the result of a sbrk to a pointer - K&R

referencing this code from Kernighan and Ritchie (2nd edition page 188), static Header* morecore(unsigned nu) { char *cp, *sbrk(int); Header* up; if (nu < NALLOC) nu = NALLOC; cp = sbrk(nu * sizeof(Header)); if (cp…
Curious
  • 20,870
  • 8
  • 61
  • 146
2
votes
2 answers

Is there any chance of an address returned by mmap(2) clashing with the heap?

If I allocate some pages using mmap(2), without providing an address hint, and later allocate some memory using malloc(3) / calloc(3), is there a chance that malloc calls sbrk(2) and grows the heap in a way that it overlaps onto the address returned…
MiJo
  • 569
  • 5
  • 19
2
votes
1 answer

How does sbrk know what address to start allocation?

I'm a beginner at assembly, and I'm having a hard time getting sbrk to allocate memory the way I want it to. I ultimately want to create a 2D array, first by allocating one column and then going through each of the row, allocating one row at a time.…
NigoroJr
  • 1,056
  • 1
  • 14
  • 31
1
vote
2 answers

How to set the value of a void **

So I am trying to use the following code to add some memory to the heap without using malloc (size is a unsigned int parameter in the function, and is not a set number) void * temp = sbrk(sizeof(void*)+sizeof(unsigned int)+size); Now I want to set…
mrswmmr
  • 2,081
  • 5
  • 21
  • 23
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
2 answers

Mac OS Catalina sbrk is deprecated

I am trying to make my own implementation of a memory allocator in C. When I try to compile my code in the terminal on Mac OS Catalina it says that the function "sbrk" is deprecated. Does anybody know a solution to fix this? The other solutions…
joycem8845
  • 83
  • 9
1
vote
1 answer

brk() exceeding the heap when implementing malloc()

I'm trying to implement my own version of the memory allocator malloc(). However I was pointed that in my case the brk() has exceeded the max heap. I needed to run my code on a platform that does tests(so I can not see the tests). This is my…