-2

I'm trying to implement stack in C.

What I'm going for:

  1. Design a Stack structure with push and pop functions for the implementation.

  2. Create a Memory structure owns number of block, block size, and stack attributes. Stack attribute represents memory blocks. Number of block attribute represents the length of the stack. Block size represents the maximum value that each element in the stack can get.

  3. Write an allocate function that takes a size parameter. If the given size is bigger than block size of the Memory, the allocation will be distributed to the different blocks in the stack attribute.

For example, calling allocate(27) updates the stack as:

allocate(27) = [10, 10, 7, 0, 0]

for a Memory with number of block = 5, block size = 10. The remaining of the elements which don't have maximum value can be sealed until the element is flushed. Therefore, the next allocation can start from next element position after 7 given above.

  1. Write a deallocate function that flushes the last used block.

My Work:

First, created the structs:

  • stack attribute represents memory blocks.
  • number of block attribute represents the length of the stack.
  • Block size represents the maximum value that each element in the stack can get.
#include <stdio.h>
#include <stdlib.h> // Provides function For memory locating. allocate & deallocate.
 
struct Stack {
    int top;
};

struct Memory{
    int stack;
    int number_of_block; // 5
    int block_size; // 10
};

Then I have tried to create allocate & push function but they are not working.

int main(){
    allocate(30);
    return 0;
}

int allocate(int size){

    struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
    struct Memory* memory = (struct Memory*)malloc(sizeof(struct Memory));
    memory->block_size = 10;
    stack->top = -1;
    memory->stack = (int*)malloc(memory->block_size * sizeof(int));

    struct Memory memory = {1, 5, 10};

    for(int i = 0; i < 5; i++){
        if(size > 10){
            size = size - 10; //27 - 10 = 17 -> 17 - 10 = 7
            push(stack, 10);
        }
    }
    if(size % 10 != 0){
        int size_mod = size % 10; //27 % 10 = 7
        push(stack, size_mod);
    }
}


void push(struct Stack* stack, struct Memory* memory, int item){
    if(stack->top == memory->block_size - 1){
        return;
    }
    memory->stack[++stack->top] = item;
    printf("%d ", item);
}
White Puzzle
  • 19
  • 2
  • 6

1 Answers1

0

The Memory structure contains a pointer to the first block, as well as the number of blocks and the maximum size of each block. Each block then contains the data and a pointer to the next block, meaning all the blocks are stored in a linked list.

The function allocate returns a pointer to the Memory structure created.

#include <stdio.h>
#include <stdlib.h>

struct Block {
    int data;
    struct Block *next;
};

struct Memory {
    int block_count;
    int block_size;
    struct Block *head;
};

/* Push a new block onto the stack */
void push(struct Block **head, int data)
{
    struct Block *new = malloc(sizeof *new);
    if (!new) {
        printf("Error: memory allocation failed");
        exit(EXIT_FAILURE);
    }
    new->data = data;
    *head = new;
    /* `printf` is not needed */
    printf("%d\n", data);
}

/* Returns a pointer to the `Memory` structure */
struct Memory *allocate(int size)
{
    struct Memory *memory = malloc(sizeof *memory);
    if (!memory) {
        printf("Error: memory allocation failed");
        exit(EXIT_FAILURE);
    }
    memory->block_count = 5;
    memory->block_size = 10;

    struct Block *head = NULL;

    for (int i = 0; i < memory->block_count; ++i) {
        int data = 0;
        if (size > 10)
            data = 10;
        else if (size > 0)
            data = size;
        size -= data;
        push(&head, data);
    }
    memory->head = head;

    return memory;
}

int main(void)
{
    struct Memory *memory = allocate(27);
    return EXIT_SUCCESS;
}

And as you can see, you don't need to cast malloc, because it returns void *, which is automatically and safely promoted to any other pointer.

Andy Sukowski-Bang
  • 1,402
  • 7
  • 20