Moderators: phlip, Prelates, Moderators General
#!/usr/bin/env python
import gtk
import gtk.glade
class HelloWorld:
def __init__(self):
#Set glade file
self.gladefile = "helloworld.glade"
self.wTree = gtk.glade.XML(self.gladefile)
#Get main window and connect destroy even with quit
self.window = self.wTree.get_widget("window1")
if (self.window):
self.window.connect("destroy", gtk.main_quit)
dic = { "on_btnbutton1_clicked": self.click,
"on_window1_destroy": self.quit }
self.wTree.signal_autoconnect(dic)
def clicked(self, widget):
print "Booyah."
def quit(self, widget):
gtk.main_quit()
if __name__ == "__main__":
hwg = HelloWorld()
gtk.main()thebeanie wrote:This is evil. My very first pyGTK script and it refuses to work. Well, not quite.As you can see, I've linked a button signal(clicked) to the function clicked(). But the callback fails. When I click the button clicked() does not run. WHHHHY?Spoiler:
static mv_task_t *new_task(nx_closure_t func, U32 stack_size) {
mv_task_t *t;
nx_task_stack_t *s;
NX_ASSERT_MSG((stack_size & 0x3) == 0, "Stack must be\n4-byte aligned");
t = nx_calloc(1, sizeof(*t));
t->stack_base = nx_calloc(1, stack_size);
/* The current stack pointer for a new task is the top of the stack minus one
* stack descriptor, which contains the initial register values for the task.
*/
t->stack_current = t->stack_base + stack_size - sizeof(*s);
s = (nx_task_stack_t*)t->stack_current;
s->pc = (U32)func;
/* Start in System mode. */
s->cpsr = 0x1F;
/* If the function is Thumb code, twiddle the CPU state accordingly. */
if (s->pc & 0x1) {
s->pc &= ~1;
s->cpsr |= 0x20;
}
return t;
}
t->stack_current = t->stack_base + stack_size - sizeof(*s);
t->stack_current = t->stack_base + ((stack_size - sizeof(*s)) >> 2);
PlayerOne wrote:
- Code: Select all
t->stack_current = t->stack_base + ((stack_size - sizeof(*s)) >> 2);
8 character diff. Now what truly puzzles me is that it somehow managed to work properly up to 3 tasks, despite having me stepping all over memory allocator structures and kernel memory.
Pointer arithmetic, why dost thou hate me?
t->stack_current = t->stack_base + (stack_size - sizeof(*s)) / sizeof(*t->stack_base);
EvanED wrote:May I make a recommendation?
- Code: Select all
t->stack_current = t->stack_base + (stack_size - sizeof(*s)) / sizeof(*t->stack_base);
That way:
1) It's clearer what it's doing. (I had to think about why you were dividing by 4 for a second. And while we're on it, I also don't think people should use >> for division by a power of two. Again, it's less clear, and compilers have only been smart enough to do that substitution for you for speed for a decade and a half.)
add r1, r0, r2, lsl #2
2) If you change the type of t->stack_base, recompile the code on a system with something other than 32-bit ints, etc., it will not re-break.
thebeanie wrote:This is evil. My very first pyGTK script and it refuses to work. Well, not quite.Spoiler:
As you can see, I've linked a button signal(clicked) to the function clicked(). But the callback fails. When I click the button clicked() does not run. WHHHHY?
uint64_t num = 317584931803LLU;
uint64_t i = 0, result = 0;
for (i=0; i<num; ++i)
{
if (!(num%i))
{
Rysto wrote:You're dividing by 0 in the first iteration of the loop.
while ((bridgePosition != 5) || (bridgePosition != -5))#pragma pack(4)
struct Foo {
// ...
};
#pragma pack(0)hic erro wrote:The craziest bug I ever (successfully) debugged involved #pragma pack. A header file declared a class:
- Code: Select all
#pragma pack(4)
struct Foo {
// ...
};
#pragma pack(0)
Now, there are two important things about this:
1. #pragma pack(0) does not 'turn off' packing. #pragma pack() does.
2. This header file was included in some compilation units but not in others.
The result of this was that a second class, minding its own business, was packed by the #pragma pack(0). In some compilation units. But not others. The class had different layouts in different compilation units.
This led to apparent memory corruption which was confused me completely: in instances of the inconsistently-packed class, a member would be set in one function, and set correctly, and everything would be fine, and then when the function returned, it would be magically corrupted!
Unfortunately, this bug happened when switching from 32 to 64 bits, months after the #pragma pack(0) was added, so the entire code-base was suspect.
Memory corruption always seems to have such bizarre symptoms. I find fopen() or fclose() will often crash. In one case, I did this:PlayerOne wrote:8 character diff. Now what truly puzzles me is that it somehow managed to work properly up to 3 tasks, despite having me stepping all over memory allocator structures and kernel memory.
Pointer arithmetic, why dost thou hate me?
poxic wrote:You suck. And simultaneously rock. I think you've invented a new state of being.
poxic wrote:You suck. And simultaneously rock. I think you've invented a new state of being.
mabufo wrote:I'm trying to count the number of vowels in a word... but I'm getting a string index out of range error as a result of my code. I'm having a hard time spotting the problem, could someone give it a look? The problem lies where I check for 'y' being a vowel:Spoiler:
int countVowels(char *u)
{
int n=0;
char *p, *q, s="aeiouy";
for (p=u; *p; p++) for( q=s; *q; q++ ) if (*p == *q ) n++;
return n;
}
thoughtfully wrote:What's worng with this (besides that it is a candidate for an obfuscated code contest!)
EvanED wrote:thoughtfully wrote:What's worng with this (besides that it is a candidate for an obfuscated code contest!)
You don't handle 'y' correctly.
(Where 'correctly' is defined as 'the way the original programmer intended')
anonymous sig wrote:Light a man a fire, you'll keep him warm for a day. Light a man on fire, and you'll keep him warm the rest of his life!
public static int vowelCount(String word){
int vowels = 0;
int n = 0;
while (n < (word.length())){
if (isConsonant(word.charAt(n)) == false){
vowels++;
}
n++;
}
// increment vowels if y is at the end of a word or surrounded by consonants
n = 1
while (n < (word.length()-1)){
if(word.charAt(n)) == 'y') {
if((isConsonant(word.charAt(n-1)) == true) && (isConsonant(word.charAt(n+1)) == true)){
vowels++;
}
}
n++;
}
if (word.charAt(n) == 'y'){
vowels++;
}
return vowels;
}phlip wrote:(BTW: thoughtfully: your code increments vowels if isConsonant returns true... I think that's a little backwards)
Rysto wrote:The problem is here:
if((isConsonant(word.charAt(n-1)) == true) && (isConsonant(word.charAt(n+1)) == true)){
If the word begins or ends with a 'y', one of the calls to charAt will be out of bounds.
public static int vowelCount(String word){
int vowels = 0;
int n = 0;
while (n < (word.length())){
if (isConsonant(word.charAt(n)) == false){
vowels++;
}
// increment vowels if y is at the end of a word or surrounded by consonants
if (word.charAt(n) == 'y'){
if (0<n && n<word.length()-1){
if((isConsonant(word.charAt(n-1)) == true) && (isConsonant(word.charAt(n+1)) == true)){
vowels++;
}
}
else {
if (n==word.length()-1){
vowels++
}
}
}
n++;
}
return vowels;
public static int vowelCount(String word)
{
int vowels = 0;
bool previousConsonant = false;
int wordlen = word.length();
for (int n = 0; n < wordlen; n++)
{
if (word.charAt(n) == 'y')
{
// increment vowels if y is at the end of a word or surrounded by consonants
if (previousConsonant && (n >= wordlen-1 || isConsonant(word.charAt(n+1))))
vowels++;
previousConsonant = true; // Makes it consistent with previous versions... may or may not be what you're after
}
else if (!isConsonant(word.charAt(n)))
{
previousConsonant = false;
vowels++;
}
else
{
previousConsonant = true;
}
}
return vowels;
}thoughtfully wrote:However.. your code makes the same out of bounds error as the OP's, unless you can rely on the n+1 not getting evaluated if the other half of that "||" isn't evaluated, (and the ">=" should be a "<="). I don't recall if this is in the standard or not, I think it probably is, but its a subtle trick to pull without comment
phlip wrote:Also: short-circuit ands and ors are a standard language feature of C, C++, Java, perl... I think the only language I've used that had ands/ors that didn't short-circuit is QBASIC/VB, which only had bitwise and/or, no logical and/or. Short-circuit logical operators are just one of those idioms you have to learn... at least, learn enough to understand them when someone else uses them.

poxic wrote:You suck. And simultaneously rock. I think you've invented a new state of being.
isset(!$_GET['something'])?$some_var=null:$some_var=$_GET['something'];!isset($_GET['something'])?$some_var=null:$some_var=$_GET['something'];Users browsing this forum: No registered users and 9 guests