How to allocate specific number of bytes in a char array - memory management
NickName:Riley F Ask DateTime:2013-04-07T05:22:51

How to allocate specific number of bytes in a char array - memory management

I have a char array that I'm using as a memory pool. I already have all the bytes flagged to signify that they are free/not free. My question is how do I go about allocating 4 bytes of the array for each int?

Small example of how I'm thinking this will work:

void* block;
char memPool[50];

block = &memPool[0];
return block;

This will return a pointer to a location that is big enough to hold 1 byte if I'm thinking correctly. So maybe I can use a void* array of 4 bytes instead and loop through the bytes until I get to the sizeof whatever type I'm being passed. I'm just trying to get this hashed out on paper before I sit down and start coding it.

I'm just looking for a bit of insight. Thanks.

Copyright Notice:Content Author:「Riley F」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/15856165/how-to-allocate-specific-number-of-bytes-in-a-char-array-memory-management

More about “How to allocate specific number of bytes in a char array - memory management” related questions

How to allocate specific number of bytes in a char array - memory management

I have a char array that I'm using as a memory pool. I already have all the bytes flagged to signify that they are free/not free. My question is how do I go about allocating 4 bytes of the array ...

Show Detail

Allocate memory to char *** in C

So, I'm having trouble in allocating memory for a char *** type variable. My objective is to create a matrix of strings and the code I currently have for memory allocation is the following: char ***

Show Detail

allocate dynamic memory for char array

I have to print some values stored in the struct to single char array , I am not getting an simple method to allocate a memory for a variable array values . below snnipet of the code which I need ...

Show Detail

Allocate memory to a specific "string"

I just started to learn memory management in C, and I didn't understand something. I want to allocate memory to a buffer that holds 12 bytes. which is the exact size of Hello World! without null

Show Detail

How to correctly allocate memory for an array of char pointers (strings of different length)?

Sorry if it sounds like a duplicate, but I have checked similar questions and to my eye my approach is correct, therefore I would like to ask you for advice. I want to allocate the memory for an ar...

Show Detail

How can I create memory management with a char array?

I have to create a memory management program using a char array. The code I need to edit is as follows: char mem[65536]; void initialize(void){ // TODO } void* allocate(int Size){ ...

Show Detail

How to allocate memory for specific number of strings?

I was given the task to program something like a dictionary, and the way I am allocating memory for the meanings is just to allocate for 100 meanings in the constructor, which works perfectly fine.

Show Detail

How to allocate memory for char strings in a struct array?

I am about to create a programm which stores data of students in a list. My question is, how to shall I allocate memory for each char string in my struct array. My code is down below. If there're ...

Show Detail

How to allocate memory to a char pointer in a struct in C?

I'm using this structure below, but it's limited if I want to get all String from a huge file... typedef struct arr { char name[200]; // Could be a number higher than 200 here... } array; Now...

Show Detail

How to allocate memory to a specific virtual address with DPMI?

I'd like to allocate memory to a specific virtual address with DPMI and clear it like this: mov edi, 0x400000 ; Base address. mov ecx, 0x2000 ; Number of bytes to allocate. ??? cld mov al, 0 rep

Show Detail