strcat and char initialized with ""

Pages: 12
Obviously, it fixed it...
Open to critics / leraning mode: On

I do not have a lot of time to write the shorter and most efficient on first time.
Also, whole program is not mine. I add functions that must rely on existing ones

With that in mind, what is so "stupid" in code post 1?
For example what is it?

int total_mb = (int)(Total_Size / 1048576LLU);
int used_mb = (int)(Used_Size / 1048576LLU);
int free_mb = (int)(Free_Size / 1048576LLU);

What is the magic constant 1048576LLU? Why a value of unsigned long long is asigned to int?

I have no any desire to spend my time explaining why this code is a good example how code shall not be written. It shall be thrown out.
This doesn't look too horrible.

Those constants are used everywhere and should be replaced by a constant symbol. Too easy to mess it up by accident.

Use of strcat() is somewhat dangerous if you can't prove that you won't overflow. And even if you can, during maintenance, this can still be a problem and bite you in the ass later.

The way you use suffix increment would be better as prefix increment (though the compiler would probably optimize it as a prefix).

There are probably other things, but I only skimmed the code.

Last edited on
Yes, then there are habits about the prefix in increment...
strcat, in this case, it is really controlled as source is constant and target enough big. But using strncat is obviously a better option

The constants: I always grep the source to avoid any issue, but you are right

Now about those LLU
Total_Size, Used_Size and Free_Size are unsigned long long integers
They contain partition size in bytes calculated through statfs or by reading and parsing /proc/partitions

For some reasons, I keep them in bytes. Inside this specific function, they are converted into Mb (1024 * 1024 = 1048576)
Since Total_Size... are LLU, right element should be specified to the same type

Now, as alternative to converting ull to integer, I could either define a new ull to hold the Total_Size/(1024*1024) or keep repeating same cumbersome calculation through the code

I did not see the need to define 3 new ull on the stack for this, so I went with integer as it is far than enough for what the function needs to display
Last edited on
Topic archived. No new replies allowed.
Pages: 12