   dynamic arrays in C
  When doing something like loading an image, you don't know beforehand how much memory you need, because the image can have any size.In C++ it's very simple to deal with that: use an std container as dynamic array and fill it up with the data.I'm thinking about porting some stuff that uses std::vectors to C however. What is the best way to deal with such things in C? Is it done with malloc and free there? Who usually has the responsability to do the "free" in C, if there's no RAII in it?   
  First: Why would you go back to C?And yes, in C it's done with malloc and free. You don't have constructors and destructors but RAII isn't realy tied to this. For instance in SDL_image you load an image with IMG_Load which returns a pointer to an SDL_Surface. You free this surface with SDL_FreeSurface. So you have one function for the construction of the image and another for the destruction. Usually the subsystem who allocated the memory is responsible for deleting it.   ;  It seems like in the open source world, people favor C over C++. I don't know why, maybe because of the speed...   ;Quote:Original post by LodeIt seems like in the open source world, people favor C over C++. I don't know why, maybe because of the speed...If you don't know whythey use C, perhaps you shouldn't switch to it.One reason why it's used a lot in open-source projects is because it's used in Linux. And it's used in Linux because C++ isn't straightforward to use for kernel development and such. To some extent at least, it has simply grown from there.The kernel uses C, so it's nice and convenient to build the stuff around it with C as well. And then why not build new applications in C as well, since the OS is in C? And besides, "most people seem to favor C over C++, so we'd better use it as well" [wink]And of course, all this breeds a lot of C programmers. And C programmers have this odd habit of using C when they program [grin][Edited by - Spoonbender on September 8, 2007 7:30:38 AM];  Even in C, the method of choice in open source development for dealing with this kind of thing is to use a library that handles it. For example,  glib contains a lot of data structures such as hash tables, linked lists and dynamic arrays.   ;  "Who usually has the responsability to do the "free" in C, if there's no RAII in it?"--Someone?!Maybe you want to roll your own "resource management".It strongly depends on the situation.You should use valgrind to check if you lose memory.€: don't forget realloc, guys[Edited by - hydroo on September 8, 2007 11:15:15 AM];  [Obviously I'm awake today; I just posted a two paragraph post in reply to a different thread]   ;  For dynamic memory allocation in C, you should use malloc() and friends.   