Typos that compile, but come back to bite you in the ass

DSP, Plugin and Host development discussion.
Post Reply New Topic
RELATED
PRODUCTS

Post

So I was wondering why my 64 bit build was crashing, but 32 bit was fine. Then I found this hidden gem:

Code: Select all

memset(buffer,0,sizeof(float*)*bufferLength);

:dog:

Post

well, adding "memsets" in the code leads to bugs like the one you described
Better (in order of increasing complexity):
- having a template for doing it - allocation, set...
- writing a memory pool manager which takes in account everything (also alignement, logging, profiling, resource management)
- a raii template which does everything - shared/weak pointers of objects managed by a memory pool which wraps the allocation, set,...
- everything like above but multithreading and properly managed using mutex and critical regions

When you have that you cannot have such kind of issues any more - fixing this core code is easy because it works everything or nothing.
I learnt to do it from code written for videogame rendering engines

Post

Std::fill(buf.begin (), buf.end (), 0) ?

Works with std::array and even arrays,

std::fill(buf_ptr, buf_ptr+buf_size, 0).

If you must use C, memset(buf_ptr, 0, ((buf_ptr+buf_size) - buf_ptr)) works too, but also see suggestions above.

Post

Thanks for the good advice, guys. I think I'll be going with a template for allocation/setting of buffers. Potentially save myself from some future headaches.

Post

matt42 wrote:So I was wondering why my 64 bit build was crashing, but 32 bit was fine. Then I found this hidden gem:

Code: Select all

memset(buffer,0,sizeof(float*)*bufferLength);

:dog:
sizeof(float*) in 32bits is 32bits, but in 64bits, it's 64bits, which is twice the actual size of your buffer (explanation for those who hadn't seen it).
So my advice is to never use C calls if you can use the C++ versions instead.
Last edited by Miles1981 on Sat Aug 22, 2015 9:11 am, edited 1 time in total.

Post

avasopht wrote:If you must use C, memset(buf_ptr, 0, ((buf_ptr+buf_size) - buf_ptr)) works too, but also see suggestions above.
I don't think so, pointer arithmetic is done with float*, so the size would lack a *4 factor. This is what is interesting with range based C++ calls like std::fill.

Post

Miles1981 wrote:
avasopht wrote:If you must use C, memset(buf_ptr, 0, ((buf_ptr+buf_size) - buf_ptr)) works too, but also see suggestions above.
I don't think so, pointer arithmetic is done with float*, so the size would lack a *4 factor. This is what is interesting with range based C++ calls like std::fill.
Exactly, fill eliminates that error in logic.

Post

Wow that's one I would have been able to make :cry: :lol:

Very recently I have started to do things in a cleaner way, following all the RAII / concurrency programming good practices, and I'm glad I'm following this direction...

Personally, my nemesis typo is still the inversion of minus signs :love:

I had also in JUCE code a typical example of typo that compile but comes back to bite you in the ass, it's well documented there : http://www.juce.com/forum/topic/forgott ... -dangerous

This was evil, because if you forget one time, everything seems to be fine, and at the second time you got some crashes without (still) knowing why :lol:

Post

Recently I had this...

Base class with "virtual void Run() {}". Then did a subclass that had "void run() { ...stuff... }. This could have been avoided with C++11 by writing "void run() override" to cause a compile time error instead of me having to wonder a couple of times during runtime why nothing seemed to happen...

Post

In my professional application, we have now even a gcc 5.1 flag to make an error if your forget the override.
Really love C++11 :)

Post

Here's another nice one:

Code: Select all

void func(const std::string& s);
void func(bool v);
...

func("abcd"); // converts from char* to bool and calls the 2nd version of func.

Post

Code: Select all

if (val = 1) { blub }
(changed to yoda-notation to avoid that =) )

also very common when coding while brainafk

Code: Select all

if (strcmp(sz, "do_blub_if_same")) { blub }

Post Reply

Return to “DSP and Plugin Development”