3

Is there a way to actually create dynamic arrays in C without having to use the stdlib?

Malloc requires the stdlib.h library, I am not allowed to use this in my project.

If anyone has any ideas, please share? Thanks.

2
  • Well, you can always roll your own... Commented Feb 6, 2013 at 17:58
  • wow, than you should write some assembly code Commented Feb 6, 2013 at 18:00

2 Answers 2

8

malloc is not just a library, it is the way you interface with the Operating System to ask for more memory for the running process. Well, you could ask more memory and manage free/occupied memory yourself, but it would be wrong on many levels.

But, I am inclined to believe that your project is going to run in some kind of platform which does not have an operating system, is it?1 In that case, the faster solution is to first allocate statically some memory in a big global array, and every time you need memory you would ask for a manager responsible for this big array.

Let me give you an example, for the sake of simplicity it will be tiny and not very functional, but it is a very good quick start.

typedef char bool;

#define BLOCK_SIZE 1024 //Each allocation must have in max 1kb
#define MAX_DATA 1024*1024*10  //Our program statically allocates 10MB
#define BLOCKS (MAX_DATA/BLOCK_SIZE)

typedef char Scott_Block[BLOCK_SIZE];

Scott_Block Scott_memory[BLOCKS];
bool Scott_used_memory[BLOCKS];

void* Scott_malloc(unsigned size) {
    if( size > BLOCK_SIZE )
        return NULL;
    unsigned int i;
    for(i=0;i<BLOCKS;++i) {
        if( Scott_used_memory[i] == 0 ) {
            Scott_used_memory[i] = 1;
            return Scott_memory[i];
        }
    }
    return NULL;
}

void Scott_free(void* ptr) {
    unsigned int pos = ((char*)(ptr)-Scott_memory[0])/BLOCK_SIZE;
    printf("Pos %d\n",pos);
    Scott_used_memory[pos] = 0;
}

I wrote this code to show how to emulate a memory manager. Let me point out a few improvements that may be done to it.

First, the Scott_used_memory could be a bitmap instead of a bool array. Second, it does not allocate memory bigger than BLOCK_SIZE, it should search for consecutives blocks to create a bigger block. But for that you would need more control data to tell how much blocks an allocated void* occupies. Third, the way free memory is searched (linearly) is very slow, usually the blocks creates a link list of free blocks.

But, like I said, this is a great quick start. And depending on your needs this may fulfill it very well.

1 If not, then you have absolutely no reason to not use malloc.

Sign up to request clarification or add additional context in comments.

Comments

2

Well why not this program (C99)

#include <stdio.h>

int main(int argc, char *argv[])
{
   int sizet, i;
   printf("Enter size:");
   scanf("%d",&sizet);
   int array[sizet];
   for(i = 0; i < sizet; i++){ 
     array[i] = i;
   }
   for(i = 0; i < sizet; i++){
     printf("%d", array[i]);
   }
   return 0;
}

Like a boss! :-)

10 Comments

Will the stack be dynamically re-sized to fit the requested array?
@AndréPuel Yep. SUB ESP, EAX ;-)
@AndréPuel gcc.gnu.org/onlinedocs/gcc/Variable-Length.html it is standard C :-)
I swear I had a code that did not compile a few weeks ago because it was using exactly that... Let me find it in my git history.
@AndréPuel that's because you did not compile your program with -std=c99 prefix
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.