# C/C++ and bitmaps



## kr651129 (Aug 28, 2012)

I've been trying to find a good tutorial that explains from newbie level on how to work with bitmaps.  I'm trying to write a malloc() and I *think* this is the best way to go?


----------



## fluca1978 (Aug 30, 2012)

It is not really clear to what you are referring, I suspect not the bmp file format! What is the problem you are trying to solve?


----------



## xibo (Aug 30, 2012)

kr651129 probably wants to use bit sequences to indicate page usage. I.e. use 1 Byte to indicate whether and which of 8 consequent pages are currently allocated.

I would recommend using memory segments in interval trees though.

The way "malloc" (or rather memory management as a whole) works in FreeBSD is explained in The Implementation of the FreeBSD Operating System by McKusick and Neville-Neil. I don't know any explicit guide to bitmaps, but thinking about it (and having knowledge about algorithms) will likely help.

EDIT:
C++ offers the std::vector<bool> data structure which is a bitmap.


----------



## expl (Aug 31, 2012)

kr651129 said:
			
		

> I've been trying to find a good tutorial that explains from newbie level on how to work with bitmaps.  I'm trying to write a malloc() and I *think* this is the best way to go?



Bitmap is just a simple array with some helper functions, ideally array of units with size of machine's WORD to avoid memory fragmentation. Since computers can not read/write single bit values directly you need to create some helper functions to act as 'getter' and 'setter' in your bitmap by loading each array unit and processing single bit values using bitwise operators. Instead of looking for bitmap tutorials just look up bitwise operators' tutorial to get a better understanding.


----------



## tommyjung (Sep 2, 2012)

Well on GIMP, if you save an image as C/C++ header file format, it will show how image file can be (though it doesn't look pretty) represented in struct.


----------

