HI everyone,

   _SBLOCK *allocateMemBlock(size_t size)
[
_SBLOCK *block = (_SBLOCK*)sbrk(0);
void *memadr = (void*)sbrk(0);
void *allocate_mem = (void*)sbrk(BLOCK_SIZE + size);
if(allocate_mem == (void*)-1)
[
return NULL;
]
else
[
block->next = NULL;
block->isfree = false;
 block->size = size;
 block->memoryAddress = memadr+BLOCK_SIZE;
 return block;
  ]

If you want help with code

  1. post valid code (properly formatted)
  2. take the time to explain what you want
  3. if you have a specific problem please state with what (and where)
include stdio.h
include stdbool.h
typedef struct __s_block[
struct __s_block *next;
bool isfree;
size_t size;
void *memoryAddress;
]
_SBLOCK;
define BLOCK_SIZE sizeof(_SBLOCK)

_SBLOCK *allocateMemBlock(size_t size)
[
_SBLOCK *block = (_SBLOCK*)sbrk(0);
void *memadr = (void*)sbrk(0);
void *allocate_mem = (void*)sbrk(BLOCK_SIZE + size);
if(allocate_mem == (void*)-1){
return NULL;
]
else
[
block->next = NULL;
block->isfree = false;
block->size = size;
block->memoryAddress = memadr+BLOCK_SIZE;
return block;
]
]
//allocate next memory block
void allocateNextMemBlock(size_t size, _SBLOCK **head)
[
_SBLOCK *current = *head;
void *allocate_mem = NULL;
void *memadr = (void*)sbrk(0);
if(current==NULL){
*head = allocateMemBlock(size);
]else
[
while(current->next != NULL)[
current = current->next;
]
_SBLOCK *newblock = sbrk(0);
allocate_mem = (void*)sbrk(BLOCK_SIZE + size);
if(allocate_mem == (void*) - 1)[]
else[
newblock->next = NULL;
 newblock->isfree = false;
 newblock->size = size;
 newblock->memoryAddress = memadr+BLOCK_SIZE;
 current->next = newblock;
  ]
  ]
  ]
void freeMemBlock(_SBLOCK **head)
[
if(*head == NULL)[]
 else[
 (*head)->isfree = true;
void printMemBlocks(_SBLOCK *current)
[
while(current != NULL){
 printf("isfree = %d, size = %d, memoryAddress = %p, current = %p, next-node = %p\n",
 current->isfree, current->size, current->memoryAddress, current, current->next);
 current = current->next;
]
]
int main()
[
_SBLOCK *sMemBlock = NULL;
allocateNextMemBlock(10,&sMemBlock);
allocateNextMemBlock(35,&sMemBlock);
allocateNextMemBlock(62,&sMemBlock);
printMemBlocks(sMemBlock);
printf("\nAfter freeing second node\n");
freeMemBlock(&(sMemBlock->next));
 printMemBlocks(sMemBlock);
 return 0;
]

Still not properly formatted. Copy your original code into the clipboard then click on </> in the toolbar to paste it into your post. As a further note, C/C++ uses brace brackets, not square brackets. Indenting your code blocks would also help.

commented: Thanks +0

It may be helpful if you also revealed what you are trying to accomplish. If it's some homework or work then somewhere there's a specification that may fill in the blanks.

#include<stdio.h>
#include<stdbool.h>

typedef struct __s_block{
    struct __s_block *next;
    bool isfree;
    size_t size;
    void *memoryAddress;
}_SBLOCK;

#define BLOCK_SIZE sizeof(_SBLOCK)

_SBLOCK *allocateMemBlock(size_t size)
{
    _SBLOCK *block = (_SBLOCK*)sbrk(0);
    void *memadr = (void*)sbrk(0);
    void *allocate_mem = (void*)sbrk(BLOCK_SIZE + size);

    if(allocate_mem == (void*)-1){
        return NULL;
    }else{
        block->next = NULL;
        block->isfree = false;
        block->size = size;
        block->memoryAddress = memadr+BLOCK_SIZE;
        return block;
    }
}

//allocate next memory block
void allocateNextMemBlock(size_t size, _SBLOCK **head)
{
    _SBLOCK *current = *head;
    void *allocate_mem = NULL;
    void *memadr = (void*)sbrk(0);

    if(current==NULL){
        *head = allocateMemBlock(size);
    }else{
        while(current->next != NULL){
            current = current->next;
        }
        _SBLOCK *newblock = sbrk(0);

        allocate_mem = (void*)sbrk(BLOCK_SIZE + size);      
        if(allocate_mem == (void*) - 1){ }
        else{
            newblock->next = NULL;
            newblock->isfree = false;
            newblock->size = size;
            newblock->memoryAddress = memadr+BLOCK_SIZE;
            current->next = newblock;
      }
    }
}

void freeMemBlock(_SBLOCK **head)
{
    if(*head == NULL){}
    else{
        (*head)->isfree = true;
    }
}

void printMemBlocks(_SBLOCK *current)
{
    while(current != NULL){
        printf("isfree = %d, size = %d, memoryAddress = %p, current = %p, next-node = %p\n",
                current->isfree, current->size, current->memoryAddress, current, current->next);
        current = current->next;
    }
}

int main()
{
    _SBLOCK *sMemBlock = NULL;
    allocateNextMemBlock(10,&sMemBlock);
    allocateNextMemBlock(35,&sMemBlock);
    allocateNextMemBlock(62,&sMemBlock);
    printMemBlocks(sMemBlock);

    printf("\nAfter freeing second node\n");
    freeMemBlock(&(sMemBlock->next));
    printMemBlocks(sMemBlock);

    return 0;
}
commented: NEXT: take the time to explain what you want if you have a specific problem please state with what (and where) +15

Hi everyone, sorry for this inconvenience,
I am trying to work on contiguous memory allocation scheme, I found this code but it is in C++ and I need in C
If anyone can help :)
--

I would really want to help, but code looks like a C program, not in C++.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.