Im kinda new to C and I was trying to make a something like a malloc function just to practice pointers and all that stuff. everything worked fine but when I deleted printf
functions in my main functions it returned SIGBUG error. from what I have read SIGBUS error is when processor tries to read from or write to memory that is not physically avaivable and I dont know what this have to do with deleting printf functions. I also tried the same code on other C and C compilers and it worked just fine without any errors.
here is the code:
#include <stdio.h>
#define heap_size 4096
#define m_availble 0x00
#define m_allocated 0x01
#define m_freed 0x02
typedef unsigned int uint;
char heap[heap_size]; //memory for Alloc function
void* Alloc(uint size);
void StoreInt(uint position, int value);
int GetInt(uint position);
void ShowHeap(uint position);
int main()
{
int* MyPtr = (int*)Alloc(sizeof(int) * 4);
MyPtr[0] = 69420;
//printf("myptr adr1: %p\n", &MyPtr[1]);
MyPtr[1] = 6;
//printf("myptr adr2: %p\n", &MyPtr[2]);
MyPtr[2] = 11;
ShowHeap(0);
}
void* Alloc(uint size) //allocates memory inside heap
{
uint position = 0;
size = sizeof(int) sizeof(char);
while(position size 40 < heap_size )
{
if(heap[position] == m_availble)
{
/*m_availble means it can store memory at this position*/
heap[position] = m_allocated;
/*m_allocated means memory at this is position
is allocated*/
StoreInt(position 1, size);
/*stores the size requied by user*/
return &heap[(position sizeof(int)
sizeof(char))];
/*returns pointer to adress that the user
can write to or read from*/
}
}
return NULL;
}
void StoreInt(uint position, int value)
{
/*stores valeu in to the heap*/
int* ptr = (int*)&heap[position];
ptr[0] = value;
}
int GetInt(uint position)
{
/*returns value from the heap*/
int* ptr = (int*)&heap[position];
return ptr[0];
}
void ShowHeap(uint position)
{
/*display adresses with values stored at those
adresses*/
printf("HEAP:\n");
for(int y = 0; y < 10; y )
{
printf("adr: %p - ", &heap[position
y * 4]);
for(int x = 0; x < 4; x )
{
printf("%i, ", heap[position 4 * y x]);
}
printf("\n");
}
}
CodePudding user response:
Alignment
If the alignment is not correct the below is undefined behavior and may cause a bus fault.
char heap[heap_size]; //memory for Alloc function
// StoreInt()
void StoreInt(uint position, int value) {
int* ptr = (int*)&heap[position];
ptr[0] = value;
// GetInt()
int* ptr = (int*)&heap[position];
return ptr[0];
Alternative, use memcpy()
:
// StoreInt()
void StoreInt(uint position, int value) {
memcpy(&heap[position], &value, sizeof value);
// GetInt()
int x;
memcpy(&x, &heap[position], sizeof x);
return x;
I also tried the same code on other C and C compilers and it worked just fine without any errors.
Other compilers may have different alignment requirements for types like int
.
Casting
Casting is a whiff of code smell. Cast is only sporadically needed in C. In OP's code, all the casts smell.
char
may be signed
Likely more informative output in ShowHeap()
had heap[]
been unsigned char
than char
.
Really want negative output -128 to 127 from printf("%i, ", heap[position 4 * y x]);
or 0 to 255?