char *str = "hi!";
uList *l = (uList*) malloc(sizeof(uList));
uListIter *i = (uListIter*) malloc(sizeof(uListIter));
l->first = i;
i->val = strdup(str);
free(i->val);
i->val = NULL;
free(i); /* here is the error, it corrupts the heap */
free(l);
typedef struct Poo {
void* element;
int o;
} Poo;
int main(void)
{
char* str = "hey!";
Poo *p = (Poo*) malloc(sizeof(*p));
p->o = 3;
free(p); /* error here!!!!!!!!!!!!!!!!!!!!! */
scanf_s("h");
return 0;
}$ cat lol.c
#include <stdio.h>
#include <stdlib.h>
typedef struct Poo {
void* element;
int o;
} Poo;
int main(void)
{
char* str = "hey!";
Poo *p = (Poo*) malloc(sizeof(*p));
p->o = 3;
free(p); /* error here!!!!!!!!!!!!!!!!!!!!! */
return 0;
}
$ clang lol.c -o lol
$ ./lol
$ #no error
extern "C" {
void foobar()
{
}
}
#include <stdio.h>
#include <stdlib.h>
typedef struct Poo {
void* element;
int o;
} Poo;
int main(void)
{
char* str = "hey!";
Poo *p = (Poo*) malloc(sizeof(*p)); /* should be Poo *p = (Poo*) malloc(sizeof(Poo)); other wise you are declaring a space of memory the size of a pointer so on 32bit systems 4 bytes 64bit 8bytes */
p->o = 3;
free(p); /* error here!!!!!!!!!!!!!!!!!!!!! */
return 0;
}
I have created my own list container structure, but I don't know if I'm using strtok correctly.
uList* uS_towordlist(const char *str, const char *sep) {
char *w, s[500];
uList *l = uList_new();
strcpy(s, str);
w = strtok(s, sep);
while (w != NULL) {
strcpy(s, w);
uL_pbstring(l, s);
w = strtok(NULL, sep);
}
return l;
}
// heres the pbstring function
void uL_pbstring(uList *l, const char* s) {
uListIter *i = uLI_new(E_STRING);
pushback(l, i, (void*)s);
}
// and of course pushback
static void pushback(uList *l, uListIter *i, void *e) {
i->val = e;
if (l->count < 1) {
l->first = i;
l->last = i;
} else {
l->last->next = i;
i->prev = l->last;
l->last = i;
}
l->count++;
}
I don't know if that is enough code to go off of to help me, but here is the output when I try to
make a word list:
uList *l = uS_towordlist("I like to move it move it", " ");
uListIter *i;
uL_foreach(i,l) {
printf("%s\n", i->toString(i));
}
OUTPUT
it.
it.
it.
it.
it.
it.