7 comments on “Pretty printing a number of bytes in C/C++

  1. Convenient! Two minor comments:
    1) while (count >= 1024 && s < 7) // prevent overrunning suffixes[]
    2) write into a temporary buffer, then only update buf if there is enough room in buf.

    My inner nerd couldn't refuse to comment on these, sorry! It's just a helper function and it's one of those "customize for your own use" things too, so they're not major issues.

    • 1) Thanks, good point!
      2) How do I know if there is enough room in buf? I never defined the calling convention except for the implied “buf must be large enough”.

      • You can use strlen() to figure out how much free space you have in the buffer. strlen() assumes terminated strings though.

        Side Note: strlen() is basically
        len = 0;
        while (buff*) {
        buff++;
        len++;
        }
        return len;

        Pardon my C code, it’s been a while since I’ve needed to use pointers.

        • Yeah, I’m not sure if that would work, since it assumes terminated strings. I might just add another parameter for buf_len and use snprintf for safety.

          I also considered doing the malloc inside the function, and force the caller to free it. Or use a static buffer internal to the function, but that wouldn’t be thread-safe I suppose.

          • How do you know where the end of the string is if it’s not null terminated? Or is that just something you don’t worry about? If not, the strlen(buff) definitely won’t work.

            I’m always hesitant to allow the function to malloc and rely on the user to free, we’ve had some pretty bad experiences of “forgetting” to free memory. Writing to a user defined buffer with the available length is probably the safest way to go.

            RE temp variable: I also keep forgetting that you’re working on highly embedded systems. Allocating a temp buffer on the inside of that function probably isn’t a great use of resources. At the end of the day, it’s a debug/helper function and you generally know *exactly* what you’re passing into it.

            PS: Anyway you can define suffixes[] outside of that function as static or const?

            PPS: It’s amazing how much we can analyze super trivial code to death, but when it comes to the heart of program, we kinda just glaze over it and assume it works.

          • (I guess this blog has a comment nesting depth of 4, so I am replying here)
            I just assume that buf is large enough, no requirements on null termination or anything like that.
            This is actually for use in a GPU programming situation, but run on the CPU. There’s (only) 4GB of RAM in my GPU, so I need to keep track of how much I’m allocating there, so this is a nice function to print the memory allocation log.
            I figured I could/should put the suffixes outside the function, but it works this way and I think I’ll leave it here :- )
            A two-argument min(), eh? I’m not sure I’ve seen that before. Another solution I for this problem I found was to use log() to find which suffix to use, but I didn’t want to mess with that. Plus this function isn’t called inside any loops (like 6 calls total for a program that can take multiple hours depending on data set) so I’m not at all worried about performance.

Leave a Reply to Brian Shipe (@RunOfTheShipe) Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.