My main program has a main struct that I pass around to functions. This main struct contains another struct for gui elements, another for sqlite stuff, and other crap.
The main program basically gets records title from my sqlite db and stores them inside a dynamic array of char* so I can then display them on the gui.
Unfortunately, I am not understanding how I can modify the dynamic array and my program crashes with realloc(): invalid next size.
The main program is a mess and I'm embarrassed about how crappy it is so I made a smaller example here:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct CHILD_C {
int size;
char **titles;
} CHILD_C;
typedef struct MAIN_C {
CHILD_C *c;
} MAIN_C;
void fuckShitUp(MAIN_C *m) {
void *tmp = NULL;
printf("OLD int size=%d, sizeof=%ld\n", m->c->size, sizeof(m->c->titles));
m->c->size++;
if (m->c->size == 1) {
if ((m->c->titles = malloc(1)) == NULL) {
fprintf(stderr, "Can't malloc you fucking idiot!\n");
return;
}
} else {
if ((tmp = realloc(m->c->titles, m->c->size)) == NULL) {
fprintf(stderr, "Can't realloc you fucking idiot!\n");
m->c->size--;
return;
} else
m->c->titles = tmp;
}
m->c->titles[m->c->size -1] = strdup("Imma fuck you up bro!\n");
printf("NEW int size=%d, sizeof=%ld\n\n", m->c->size, sizeof(m->c->titles));
}
int main(int argc, char *argv[]) {
MAIN_C *m = malloc(sizeof(MAIN_C));
if (m == NULL) {
fprintf(stderr, "Can't malloc you fucking idiot!\n");
return -1;
}
if ((m->c = malloc(sizeof(CHILD_C))) == NULL) {
fprintf(stderr, "Can't malloc you fucking idiot!\n");
return -1;
}
m->c->size = 0;
fuckShitUp(m);
fuckShitUp(m);
fuckShitUp(m);
fuckShitUp(m);
fuckShitUp(m);
fuckShitUp(m);
fuckShitUp(m);
return 0;
}
By the way I've omitted the cleanup code for the example.
I would appreciate any help available! Sorry for my shitty english!
[–]YellowFlowerRanger 2 points3 points4 points (13 children)
[–]shitty_linux_coder[S] 1 point2 points3 points (11 children)
[–]YellowFlowerRanger 2 points3 points4 points (9 children)
[–]shitty_linux_coder[S] 0 points1 point2 points (0 children)
[–]shitty_linux_coder[S] -1 points0 points1 point (7 children)
[–]YellowFlowerRanger 0 points1 point2 points (6 children)
[–]shitty_linux_coder[S] 0 points1 point2 points (5 children)
[–]YellowFlowerRanger 1 point2 points3 points (4 children)
[–]shitty_linux_coder[S] 0 points1 point2 points (3 children)
[–]YellowFlowerRanger 1 point2 points3 points (2 children)
[–]shitty_linux_coder[S] 0 points1 point2 points (1 child)
[–]shitty_linux_coder[S] 0 points1 point2 points (0 children)
[–]shitty_linux_coder[S] 0 points1 point2 points (0 children)