C
cerr
Well yeah, that's the problem, I've tried using the remote GDB feature
which would work nicely but all the watchdogs prevent me from using
it, once the process halt, my system reboots rightaway....
Disable watchdogs for debugging.
int push(char ***list, char *str, int curlen){
char **temp;
int i=0;temp = realloc((*list),(curlen+1)*sizeof(*list));
Although it almost certainly doesn't matter, I'm pretty sure you want
sizeof(**list) here for consistency.
I would also say that this function is crying out to be cleaned
up, in a couple of very significant ways. You should not have the
length be a separate argument; "list" should be of a type such that
you can always do push(list, value); without having to know.
//-------------------------------------------------------
char *pop(char ***list, int *size) {
if(!*size) return 0;char *first = (*list)[0];memmove(*list, *list + 1, (*size) * sizeof(**list));
(*size) --;
(*list) = realloc(*list, (*size) * sizeof(**list));return first;
}
Same issues here -- size tracked elsewhere.
Suggestion (totally untested, etc.):
struct list {
struct list *next;
char *s;
};
void push(struct list **l, char *s) {
struct list *new;
if (!l)
return;
new = malloc(sizeof(*new));
new->next = *l;
new->s = s;
*l = new;
}
char *pop(struct list **l) {
struct list *old;
char *s;
if (!l)
return NULL;
old = *l;
if (old) {
*l = old->next;
s = old->s;
free(old);
return s;
} else {
return NULL;
}
}
Usage:
struct list *l = NULL;
push(&l, "foo");
push(&l, "bar");
pop(&l); /* => "bar" */
pop(&l); /* => "foo" */
pop(&l); /* => NULL */
Your current model is HUGELY vulnerable to possible clashes involving
the current length getting out of sync somehow, but you never need it if
you use an actual list.
If you do want to use an array, think about something like this:
struct list {
char **data;
size_t curlen;
size_t allocated;
};
void push(struct list *l, char *s) {
if (l->curlen >= l->allocated) {
char **new = malloc(allocated * 2 * sizeof(*data));
if (new) {
memcpy(new, data, curlen * sizeof(*data));
free(l->data);
l->data = new;
l->allocated *= 2;
} else {
assert(!"I wrote an error handler");
}
}
l->data[l->curlen++] = s;
}
char *pop(struct list *l) {
if (l->curlen > 0) {
return l->data[--l->curlen];
}
}
(And yeah, this code is probably full of errors.)
This actually makes a whole lot of sense yes!
Great idea and it's so much simpler!
Let me try ti implement this!
Ron