Programming by c for a cpp programmer

This article try to describe some differences between the c and cpp from the programming perspective.

No stl

you could not use the vector and map conveniently, which means you need to make hash map and linked list by yourself or use some third part libraries. There are some details here, such as the next pointer, some knowledge about using linked list coreaccltly, it sounds easy since it is the knowledge discussed at the text book, but it is hard to make everything right at one time.

One popular linked list in c is the lined list used by linux, there are all kinds of related resource online. Another one is this uthash.

Recently, there is a cpp project, and I need to adapt it to the c version, one complexity part is to update the vector that contains a list of addresses. We need to consider more in c version. The typical struct can include two parts, the first one can be a integer that signify the size of the object, and then second one is a raw pointer that contains the actual data. It is quite general, the char pointer can contains all kinds of data. But it also increase the complaxity to manage differnet things. When I use the vector string, I do not need to care too much about the size of string, but for c version, we need to set them explicitly, such as specifying that there are 256 byte for each string and split the whole banch of memory into different partitions manually. In that case, I need to use a lot memcpy things when extract every string, which increases the programming complexity.

In fact, you could always ask your self, whhy not use the stl. I remember at one class for the graduate study, I use the c to finish project (I was not familir with the cpp at that time). Then it consumes me a lot of time to debug the list implementaiton which can be processed by cpp conveniently actually.

Since we need to process string manaully, the temination of the string may become important, such as distinction between null, \0 and 0. how we init the memory to make every details correct. refer to this. how to explain the 0 in different cases. When we set the charater as 0 or \0, we set the to int zero in memory actually. When we set the ‘0’, it is actually an char that represent the 0 which is the ascii code of character 0, the memory value is not 0, it is 48, which is the ascii value of 0.

Compiling c and cpp, name mangling

When you compile the c with cpp, try to consider that your program may be called by the c program. Becasue the name mangling, we may use this in the header file of the cpp

#if defined(__cplusplus)
extern "C" {
#endif

......

#if defined(__cplusplus)
}
#endif

The defination of the function shoule be in c style, and the implementation of them can be in the cpp style. By this way, this header file can be used both by c and cpp program.

No reference, only pointer

In the c world, the * and the ** can be the common one for memory allocation management and the data transfer. For the cpp word, the lvalue reference & and the rvalue reference && can be common case for data transfer in function call.
Sometimes, we tend to use the typedef struct to define a pointer such as typedef struct margo_instance* margo_instance_id, then we can avoid some complex expression such as **.

Here are some interesting discussion about the typedef and the struct

Buffered output

In cpp, there are no issue when you use cout to output sth and write them into the log. In c, we need to use the fflush in order to output what we want. Sometimes, we just ust the stderr to replace the stdout since it is unbuffered, check this.

References

https://stackoverflow.com/questions/20267244/why-fprintf-doesnt-write-directly-into-the-file-unless-fflush-is-used

https://c-for-dummies.com/blog/?p=3675

推荐文章