2

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?
wndsr
  • 139
  • 1
  • 2
  • 1
    I would stick to C++ where all these vectors etc have been done you end up writing less code and it won't be any slower – mmmmmm Apr 10 '10 at 19:40
  • 7
    Why on earth would you switch? –  Apr 10 '10 at 19:41
  • Why on earth are the interpreters of all popular scripting languages written in C and not C++? – wndsr Apr 10 '10 at 19:52
  • 2
    @wndsr: Most interpreters are in C because most interpreters were written before good implementations of C++ and the STL were available. – Billy ONeal Apr 10 '10 at 19:59
  • @wndsr That's a bad reason for using C rather than C++. For good reasons see http://stackoverflow.com/questions/649789/why-artificially-limit-your-code-to-c –  Apr 10 '10 at 20:03
  • 3
    Rewriting version 0.1 is always a good idea when you hit the wall. Switching to C won't make any difference, just a different wall. Find out what *really* went wrong, fix that. – Hans Passant Apr 10 '10 at 20:11
  • 1
    @wndsr: Because most interpreters are written by people who don't understand C++. So switching from a language you *do* know, to one you've never used before strikes me as a very silly thing to do. – jalf Apr 10 '10 at 22:06

3 Answers3

3

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
  • 104,103
  • 58
  • 317
  • 552
3

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.

richq
  • 55,548
  • 20
  • 150
  • 144
2

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
  • 156
  • 5
  • No, STL strings in C++ are 100% more than just char arrays. Look at all the functions avaialable with them, natively. – wndsr Apr 10 '10 at 19:54
  • 2
    @wndsr: Guess what? In C, you have to do that yourself. C provides no such features. – Billy ONeal Apr 10 '10 at 20:00