I'm trying to get the value for an index in a array and use simple_array_index_no_free but only manage to make a coredump. I guess i could have something with the stack to do since when I run a test program like this.
int main() { array a = ({3,4,5,6,7,8,9,10,11}); Test.ArrayAdapter ad = Test.ArrayAdapter(a); write(sprintf("array %d adapter %d\r\n", a[1], ad[1])); for (int n=0; n< 9; n++) { write(sprintf("array %d adapter %d\r\n", a[n], ad[n])); } }
It will produce a coredump. But if replace the ad[1] with ad[1]=11 then it will work.
Does anyone have a idea what I've forgotten? Or is simple_array_index_no_free the wrong way to go? Greatfull for any help. Code follows...
DECLARATIONS
PIKECLASS ArrayAdapter {
CVAR int pos; CVAR struct array *a;
PIKEFUN int `[](mixed index) { struct svalue *s;
simple_array_index_no_free(s, THIS->a, index);
if (s) { push_svalue(s); } else { push_int(0); Pike_sp[-1].subtype=NUMBER_UNDEFINED; } }
PIKEFUN int `[]=(mixed index, mixed value) { simple_set_index(THIS->a, index, value); }
PIKEFUN void create(array a) { add_ref(THIS->a=a); }
INIT { THIS->a=0; THIS->pos=0; }
EXIT { free_array(THIS->a); }
}
PIKEFUN int `[](mixed index) { struct svalue *s;
simple_array_index_no_free(s, THIS->a, index);
Here you are dereferencing an uninitialized pointer. The result of the index operation is written to the svalue pointed to by s, but s is not pointing to any svalue. => segfault.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2003-01-09 14:54: Subject: simple_array_index_no_free
I'm trying to get the value for an index in a array and use simple_array_index_no_free but only manage to make a coredump. I guess i could have something with the stack to do since when I run a test program like this.
int main() { array a = ({3,4,5,6,7,8,9,10,11}); Test.ArrayAdapter ad = Test.ArrayAdapter(a); write(sprintf("array %d adapter %d\r\n", a[1], ad[1])); for (int n=0; n< 9; n++) { write(sprintf("array %d adapter %d\r\n", a[n], ad[n])); } }
It will produce a coredump. But if replace the ad[1] with ad[1]=11 then it will work.
Does anyone have a idea what I've forgotten? Or is simple_array_index_no_free the wrong way to go? Greatfull for any help. Code follows...
DECLARATIONS
PIKECLASS ArrayAdapter {
CVAR int pos; CVAR struct array *a;
PIKEFUN int `[](mixed index) { struct svalue *s;
simple_array_index_no_free(s, THIS->a, index);
if (s) { push_svalue(s); } else { push_int(0); Pike_sp[-1].subtype=NUMBER_UNDEFINED; } }
PIKEFUN int `[]=(mixed index, mixed value) { simple_set_index(THIS->a, index, value); }
PIKEFUN void create(array a) { add_ref(THIS->a=a); }
INIT { THIS->a=0; THIS->pos=0; }
EXIT { free_array(THIS->a); }
}
/ Peta, jo det är jag
PIKEFUN int `[](mixed index) { struct svalue *s;
simple_array_index_no_free(s, THIS->a, index);
^ Here's your problem; this is C, not C++. The concept of implicitly passing arguments by reference doesn't exist.
Try something like:
PIKEFUN int `[](mixed index) { struct svalue sval;
simple_array_index_no_free(&sval, THIS->a, index);
instead.
/ Henrik Grubbström (Lysator)
Previous text:
2003-01-09 14:54: Subject: simple_array_index_no_free
I'm trying to get the value for an index in a array and use simple_array_index_no_free but only manage to make a coredump. I guess i could have something with the stack to do since when I run a test program like this.
int main() { array a = ({3,4,5,6,7,8,9,10,11}); Test.ArrayAdapter ad = Test.ArrayAdapter(a); write(sprintf("array %d adapter %d\r\n", a[1], ad[1])); for (int n=0; n< 9; n++) { write(sprintf("array %d adapter %d\r\n", a[n], ad[n])); } }
It will produce a coredump. But if replace the ad[1] with ad[1]=11 then it will work.
Does anyone have a idea what I've forgotten? Or is simple_array_index_no_free the wrong way to go? Greatfull for any help. Code follows...
DECLARATIONS
PIKECLASS ArrayAdapter {
CVAR int pos; CVAR struct array *a;
PIKEFUN int `[](mixed index) { struct svalue *s;
simple_array_index_no_free(s, THIS->a, index);
if (s) { push_svalue(s); } else { push_int(0); Pike_sp[-1].subtype=NUMBER_UNDEFINED; } }
PIKEFUN int `[]=(mixed index, mixed value) { simple_set_index(THIS->a, index, value); }
PIKEFUN void create(array a) { add_ref(THIS->a=a); }
INIT { THIS->a=0; THIS->pos=0; }
EXIT { free_array(THIS->a); }
}
/ Peta, jo det är jag
Actually, I think what he really ought to do is
simple_array_index_no_free(Pike_sp, THIS->a, index); Pike_sp++;
and not bother with s/sval at all.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2003-01-09 15:00: Subject: simple_array_index_no_free
PIKEFUN int `[](mixed index) { struct svalue *s;
simple_array_index_no_free(s, THIS->a, index);
^
Here's your problem; this is C, not C++. The concept of implicitly passing arguments by reference doesn't exist.
Try something like:
PIKEFUN int `[](mixed index) { struct svalue sval;
simple_array_index_no_free(&sval, THIS->a, index);
instead.
/ Henrik Grubbström (Lysator)
The way simple_array_index_no_free handles a string index is.. umm.. creative. I wonder why it doesn't treat arrays the same way, but most of all I wonder why it exists at all.
/ Martin Stjernholm, Roxen IS
Previous text:
2003-01-09 14:54: Subject: simple_array_index_no_free
I'm trying to get the value for an index in a array and use simple_array_index_no_free but only manage to make a coredump. I guess i could have something with the stack to do since when I run a test program like this.
int main() { array a = ({3,4,5,6,7,8,9,10,11}); Test.ArrayAdapter ad = Test.ArrayAdapter(a); write(sprintf("array %d adapter %d\r\n", a[1], ad[1])); for (int n=0; n< 9; n++) { write(sprintf("array %d adapter %d\r\n", a[n], ad[n])); } }
It will produce a coredump. But if replace the ad[1] with ad[1]=11 then it will work.
Does anyone have a idea what I've forgotten? Or is simple_array_index_no_free the wrong way to go? Greatfull for any help. Code follows...
DECLARATIONS
PIKECLASS ArrayAdapter {
CVAR int pos; CVAR struct array *a;
PIKEFUN int `[](mixed index) { struct svalue *s;
simple_array_index_no_free(s, THIS->a, index);
if (s) { push_svalue(s); } else { push_int(0); Pike_sp[-1].subtype=NUMBER_UNDEFINED; } }
PIKEFUN int `[]=(mixed index, mixed value) { simple_set_index(THIS->a, index, value); }
PIKEFUN void create(array a) { add_ref(THIS->a=a); }
INIT { THIS->a=0; THIS->pos=0; }
EXIT { free_array(THIS->a); }
}
/ Peta, jo det är jag
It exists for this reason:
void bork(array(object) x) { return x->bork(); return column(x,"bork")(); return map(x,"bork"); return `()(x[*]["bork"][*]); }
Which is easier to read?
/ Fredrik (Naranek) Hubinette (Real Build Master)
Previous text:
2003-01-09 15:05: Subject: simple_array_index_no_free
The way simple_array_index_no_free handles a string index is.. umm.. creative. I wonder why it doesn't treat arrays the same way, but most of all I wonder why it exists at all.
/ Martin Stjernholm, Roxen IS
I prefer the first or the third variant.
/ Niels Möller ()
Previous text:
2003-01-10 07:34: Subject: simple_array_index_no_free
It exists for this reason:
void bork(array(object) x) { return x->bork(); return column(x,"bork")(); return map(x,"bork"); return `()(x[*]["bork"][*]); }
Which is easier to read?
/ Fredrik (Naranek) Hubinette (Real Build Master)
For reading, I like number three. It shows very clearly what's going on.
I can't help but write the first alternative, though. :)
/ Mirar
Previous text:
2003-01-10 07:34: Subject: simple_array_index_no_free
It exists for this reason:
void bork(array(object) x) { return x->bork(); return column(x,"bork")(); return map(x,"bork"); return `()(x[*]["bork"][*]); }
Which is easier to read?
/ Fredrik (Naranek) Hubinette (Real Build Master)
Ah, I confused columns() with rows(). Sorry. ;)
/ Martin Stjernholm, Roxen IS
Previous text:
2003-01-10 07:34: Subject: simple_array_index_no_free
It exists for this reason:
void bork(array(object) x) { return x->bork(); return column(x,"bork")(); return map(x,"bork"); return `()(x[*]["bork"][*]); }
Which is easier to read?
/ Fredrik (Naranek) Hubinette (Real Build Master)
pike-devel@lists.lysator.liu.se