[PD-dev] Segfaults with FLEXT code

Thomas Grill t.grill at gmx.net
Tue Aug 5 11:39:57 CEST 2003

Hi Ben,

it would be great if you could send a patch that demonstrates some intended
behavior of your external..... otherwise i can't really figure out what it
does.... Anyway, having a short look at the code i spotted some things:

1) RAND_MAX is defined in stdlib.h to represent the largest number rand()
will ever deliver. This means that it is guaranteed that 0 <=
rand()/RAND_MAX <= 1. If you redefine RAND_MAX in your code to some
different value this is no longer true. rand() doesn't know about the
redefinition! So, if you want your random numbers to be in a range from a to
b then you should use something like  r = a+(float)rand()/RAND_MAX*(b-a). If
the granularity of rand() is too large for your application you might also
want to check out better algorithms (see for example
http://www.library.cornell.edu/nr/bookcpdf.html , chapter 7).

2) You have some tricky array indexing in your code, which could be a
potential cause of trouble (e.g. because of a simple typo). Maybe you would
want to insert some assertion statements (or other means) into your code to
verify that the indexing is correct.

for example:
int ix = p*hoodsize+n;
FLEXT_ASSERT(ix < hoodsize*popsize); // check if index is within the array
neighbors [ix] = p-1+n;

3) Are you aware of the fact that within class members the this-pointer can
be omitted?  This means that e.g. "popsize" means exactly the same as
"this->popsize" in any member function of the pso class. On the other hand
explicit usage of this can make things clearer, of course.

best greetings,

----- Original Message ----- 
From: "B. Bogart" <ben at ekran.org>
To: <pd-dev at iem.at>
Sent: Tuesday, August 05, 2003 5:10 AM
Subject: [PD-dev] Segfaults with FLEXT code

> Hello all,
> I'm having trouble with segfaults with my FLEXT external, I'm new to flext
> and C++ and I really don't have an idea how to narrow it down. I've got it
> down to two lines that commented out remove the segfault, but I can't find
> anything wrong with those lines.
> I've attached my flext class (pso.cpp) and the SDL C program I'm
> re-implimenting. The SDL program has no segfault problems, and most of the
> code is copy-pasted with only variable and array subscript changes...
> Any help would be greatly appreciated.
> Thanks
> Ben

More information about the Pd-dev mailing list