Segmentation fault in malloc replication
Segmentation fault in malloc replication
I am trying to implement an example from the book The C programming language. The example replicates a simplified version of malloc. My implementation produces a Segmentation fault. The functions alloc and afree are basically copied from the book. I am trying to use those functions in main. As far as I understood, the *(pointer+i) expression gives me the value stored in the address next to the pointer as long as both addresses are in the same array. It should be implicitly satisfied but obviously, it is not enough.
How can I actually use the functions alloc and afree to create dynamic array?
#include <stdio.h>
int *alloc(int);
void afree(int *);
/* RETURNS SEGMENTATION FAULT, WHY? */
int main()
int *dyna_arr;
dyna_arr = alloc(50);
/* fill the array with integers */
for (int i = 0; i < 50; i++)
*(dyna_arr+i) = i;
for (int i=49; i>=0;i--)
printf(" %d", *(dyna_arr+i));
afree(dyna_arr);
#define ALLOCSIZE 10000
static int allocbuf[ALLOCSIZE];
static int *allocp =allocbuf; /* next free position in buffer */
/* alloc: return pointer to n ints */
int *alloc(int n)
if (allocbuf + ALLOCSIZE - allocp >= n) /* is there enough space in the buffer? */
allocp += n;
return allocp - n;
else /* not enough space */
return 0;
/* afree: free the storage pointed to by p */
/* Only possible in LIFO fashion */
void afree(int *p)
if (p >= allocbuf && p < allocbuf + ALLOCSIZE)
allocp = p;
2 Answers
2
The problem is
if (allocbuf + ALLOCSIZE - allocp <= n) /* is there enough space in the buffer? */
your comparison is wrong.
It should be available memory >= n
but your checking if available memory <= n
and returning 0
;
available memory >= n
if available memory <= n
0
Below modification should work
if (((allocbuf + ALLOCSIZE) - allocp) >= n) /* is there enough space in the buffer? */
@JAV Yes, I 'm able run your program without any issue.
– kiran Biradar
Aug 26 at 15:55
@JAV What was the problem ?
– kiran Biradar
Aug 26 at 15:57
Oh alright, my macro for compilation did not delete the old binary. Now everything works fine. Thank you very much :)
– JAV
Aug 26 at 15:57
Ok, You are welcome.
– kiran Biradar
Aug 26 at 15:58
Your issue is in alloc(50);
wich gives you 50 bytes of memory, not space for 50 int
. You need to use alloc(50*sizeof(int));
.
alloc(50);
int
alloc(50*sizeof(int));
I tried to change it, but still got segfault. But my alloc works with a buffer array defined to hold ints. So should not that be enough? By multipliing the sizeof int by 50, much more integer boxes of memory, than necessary. Or am i missing something?
– JAV
Aug 26 at 15:41
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You are right, the comparison is wrong. I changed it, but still get segfault. Am I using the alloc function in main the right way?
– JAV
Aug 26 at 15:53