tags:

views:

164

answers:

3

I'm switching from C++ to C because I'm rebuilding my toy interpreter. I was used to vectors for dynamic allocation of objects like tokens or instructions of my programs, stacks and mainly strings with all their aspects.

Now, in C I'm not going to have all these anymore. I know that I will have to use a lot of memory management, too.

  • I'm completely new to C, I only know the high-level easy-life data structures from the STL, how can I get started with strings and dynamic memory allocation?
+3  A: 

In C, the landscape is much simpler. You have only malloc, calloc, realloc, and free.

malloc allocates a number of bytes and returns it to you, returning NULL on failure.

calloc is the same thing as malloc, but performs the size multiplication for you. (You give it sizeof(mytype) and the number, and it gives you the right size). It also fills the memory block with zeros.

realloc takes a pointer previously malloc'd and changes the size of the underlying memory block. If the block can be expanded, it is, and if it can't, then a new block is allocated and the contents of the old block are copied to the new block. It returns NULL on failure.

free gives the memory back previously allocated with malloc, calloc, or realloc.

Billy ONeal
`In C, the landscape is much simpler` means that you have to roll your own... for each friggin' project.
Aviral Dasgupta
@aviraldg: Simpler does not always mean better :(
Billy ONeal
calloc() also fills the memory with zeros
Alexandre Jasmin
@Alexandre Jasmin: Thank you. I have noted that in my answer.
Billy ONeal
+2  A: 

Strings are just char arrays terminated with 0 (also '\0') (pretty hard core), but you can make anything you want with your own functions, and maybe your own structure - you can make your own string.

Functions for ansi strings that are natively available: http://cplusplus.com/reference/clibrary/cstring/

hamax
No, STL strings in C++ are 100% more than just char arrays. Look at all the functions avaialable with them, natively.
wndsr
@wndsr: Guess what? In C, you have to do that yourself. C provides no such features.
Billy ONeal
+3  A: 

As you may have guessed, there will be a great deal more work involved if you are used to C++ and want to change to C.

For your vectors, the usual approach is to use linked lists when you want a dynamic array-like structure. Though this is not the same as a vector - O(1) access here vs O(n) in a regular linked list - it usually "does the job". Alternatively: don't use a dynamic array. Lots of situations can get by with a fixed array and a MAX_ARRAY-style constant. For v0.2 at least :-)

For strings, you will probably end up with something like:

struct string {
    char *buf;
    size_t length;
}

With more fields to account for the buffer allocated vs the actual buffer used, and so on. Then a whole raft of routines to append to the string, free it, copy another string, and so on.

Stacks can be implemented in terms of a linked list or an array.

Have you spotted the pattern? Computer Science 101. Plenty of wheel reinvention. The advantage is that you can optimize the data structures for your program. The disadvantages are that you will have to write a whole bunch of code just to get back to where you are now, probably. And you're going to need a lot more unit tests.

rq