for (int i = 0; i < MAX_CHILDS; i++) { n->child[i] = NULL; }
Gavin Parker
It's fine. The cast is there because I tried to make it a cpp program instead of a c program.
Funny thing is that the code works in x64 bits as a cpp program.
I'm really not understanding why is this
Jaxson Lee
post full code
John Hughes
C is 32-bit. C++ is 64-bit.
Eli Thomas
Sorry it's a bit extensive and not a lot of comments. This is basically me trying to implement a b tree as in the book Introduction to Algorithms 3rd Edition
int main() { init_random_generator(); int *test_array = NULL; // { 3, 7, 1, 2, 5, 6, 8, 9, 4, 10, 11 }; printf(">loading test data\n"); load_array_data(&test_array);
if (x->leaf == true) { while (i > 0 && k < x->key[i - 1]) { x->key[i] = x->key[i - 1]; i = i - 1; }
x->key[i] = k; x->n = x->n + 1; // DISK WRITE X } else {
while (i > 0 && k < x->key[i - 1]) { i = i - 1; }
// DISK READ X->C[I]
// if chosen child is full if (x->c[i]->n == 2 * t - 1) { b_tree_split_child(x, i);
if (k > x->key[i]) { i = i + 1; } } b_tree_insert_nonfull(x->c[i], k); } }
void b_tree_insert(tree *T, int k) { // temp pointer to root node* r = T->root;
// if node is full if (r->n == (2 * t - 1)) { // create new node node* s = b_tree_new_node(); // new node is now the root T->root = s; // new node won't be leaf s->leaf = false; // new node is empty s->n = 0; // new node points to previous root s->c[0] = r;
This. Debug it you dumb fuck, there's probably UB somewhere.
Michael Hughes
Try compiling it with GCC (you can probably use an online IDE if you don't want to set it up). If that works then you can probably just chalk to it up to Microsoft's VS compiler being shit. If it doesn't then maybe you'll get a more useful error.
Gabriel Allen
fix this test.c: In function 'b_tree_new_node': test.c:26:19: warning: implicit declaration of function 'malloc' [-Wimplicit-function-declaration] node *n = (node*)malloc(sizeof(node)); ^~~~~~ test.c:26:19: warning: incompatible implicit declaration of built-in function 'malloc' test.c:26:19: note: include '' or provide a declaration of 'malloc' test.c:35:13: warning: assignment makes integer from pointer without a cast [-Wint-conversion] n->key[i] = NULL; ^ test.c: In function 'init_random_generator': test.c:181:2: warning: implicit declaration of function 'srand' [-Wimplicit-function-declaration] srand(time(NULL)); ^~~~~ test.c: In function 'random_number': test.c:186:2: warning: implicit declaration of function 'rand' [-Wimplicit-function-declaration] rand(); ^~~~ test.c: In function 'init_array': test.c:192:13: warning: incompatible implicit declaration of built-in function 'malloc' *n = (int*)malloc(sizeof(int)); ^~~~~~ test.c:192:13: note: include '' or provide a declaration of 'malloc' test.c: In function 'add_to_array': test.c:211:15: warning: implicit declaration of function 'realloc' [-Wimplicit-function-declaration] temp = (int*)realloc(*n, (sizeCount + 1) * sizeof(int)); ^~~~~~~ test.c:211:15: warning: incompatible implicit declaration of built-in function 'realloc' test.c:211:15: note: include '' or provide a declaration of 'realloc' test.c:207:6: warning: unused variable 'tempcount' [-Wunused-variable] int tempcount = sizeCount; ^~~~~~~~~ test.c: In function 'main': test.c:289:7: warning: unused variable 'random' [-Wunused-variable] int random = random_number(); ^~~~~~ test.c:285:6: warning: unused variable 'known_random_number' [-Wunused-variable] int known_random_number = random_number(); ^~~~~~~~~~~~~~~~~~~
Easton Martinez
So the compiler being wrong is more likely than the retard who can't even post his code being wrong?
Elaborate on your logic here
Brandon Price
LLVM
a.c:26:19: warning: implicitly declaring library function 'malloc' with type 'void *(unsigned long)' [-Wimplicit-function-declaration] node *n = (node*)malloc(sizeof(node)); ^ a.c:26:19: note: include the header or explicitly provide a declaration for 'malloc' a.c:35:13: warning: incompatible pointer to integer conversion assigning to 'int' from 'void *' [-Wint-conversion] n->key[i] = NULL; ^ ~~~~ a.c:181:2: warning: implicit declaration of function 'srand' is invalid in C99 [-Wimplicit-function-declaration] srand(time(NULL)); ^ a.c:186:2: warning: implicit declaration of function 'rand' is invalid in C99 [-Wimplicit-function-declaration] rand(); ^ a.c:211:15: warning: implicitly declaring library function 'realloc' with type 'void *(void *, unsigned long)' [-Wimplicit-function-declaration] temp = (int*)realloc(*n, (sizeCount + 1) * sizeof(int)); ^ a.c:211:15: note: include the header or explicitly provide a declaration for 'realloc' a.c:207:6: warning: unused variable 'tempcount' [-Wunused-variable] int tempcount = sizeCount; ^ a.c:289:7: warning: unused variable 'random' [-Wunused-variable] int random = random_number(); ^ a.c:285:6: warning: unused variable 'known_random_number' [-Wunused-variable] int known_random_number = random_number(); ^ 8 warnings generated.
Elijah Jones
GCC
a.c: In function ‘b_tree_new_node’: a.c:26:19: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration] node *n = (node*)malloc(sizeof(node)); ^~~~~~ a.c:26:19: warning: incompatible implicit declaration of built-in function ‘malloc’ a.c:26:19: note: include ‘’ or provide a declaration of ‘malloc’ a.c:35:13: warning: assignment makes integer from pointer without a cast [-Wint-conversion] n->key[i] = NULL; ^ a.c: In function ‘init_random_generator’: a.c:181:2: warning: implicit declaration of function ‘srand’ [-Wimplicit-function-declaration] srand(time(NULL)); ^~~~~ a.c: In function ‘random_number’: a.c:186:2: warning: implicit declaration of function ‘rand’ [-Wimplicit-function-declaration] rand(); ^~~~ a.c: In function ‘init_array’: a.c:192:13: warning: incompatible implicit declaration of built-in function ‘malloc’ *n = (int*)malloc(sizeof(int)); ^~~~~~ a.c:192:13: note: include ‘’ or provide a declaration of ‘malloc’ a.c: In function ‘add_to_array’: a.c:211:15: warning: implicit declaration of function ‘realloc’ [-Wimplicit-function-declaration] temp = (int*)realloc(*n, (sizeCount + 1) * sizeof(int)); ^~~~~~~ a.c:211:15: warning: incompatible implicit declaration of built-in function ‘realloc’ a.c:211:15: note: include ‘’ or provide a declaration of ‘realloc’ a.c:207:6: warning: unused variable ‘tempcount’ [-Wunused-variable] int tempcount = sizeCount; ^~~~~~~~~ a.c: In function ‘main’: a.c:289:7: warning: unused variable ‘random’ [-Wunused-variable] int random = random_number(); ^~~~~~ a.c:285:6: warning: unused variable ‘known_random_number’ [-Wunused-variable] int known_random_number = random_number();
Jeremiah Rivera
Don't cast the return value of malloc, dumbass.
You didn't include stdlib, so malloc will be defined implicitly i.e. have a return value of *int*. Since you casted it, the error is suppressed since C assumes you know what the fuck you're doing.
On a 32bit arch, int is 4 bytes (the same as a pointer) so the cast happens to work. On 64bit, the high 4 bytes are discarded and the pointer starts pointing to some undefined area.
Adrian King
n->key[i] = NULL; key is a tab of int, ou can't assign NULL to a int otherwise laucnh on my machine
Jayden Cooper
>loading test data >building tree >searching all keys >all tests passed successfully
just including stdlib.h seems to fix your problem
Owen Long
What compiler is this?
Adrian Powell
NULL is just a 0
Adrian Gomez
not for gcc
Owen Cook
gcc
Nolan Allen
GCC.
Commented out the unused variables and added stdlib.h and now you only have to fix:
GCC: a.c: In function ‘b_tree_new_node’: a.c:36:13: warning: assignment makes integer from pointer without a cast [-Wint-conversion] n->key[i] = NULL;
LLVM: a.c:36:13: warning: incompatible pointer to integer conversion assigning to 'int' from 'void *' [-Wint-conversion] n->key[i] = NULL; ^ ~~~~
Chase Torres
this
Isaac Torres
Use calloc if you're just going to set everything to null. Save yourself some typing.
Jaxon Wright
Compilers don't validate memory access And I doubt that anyone wil actually go through the code
The best thing you can do is to rewrite everything
good luck
Christian Stewart
Using stdlib.h and removing casts on malloc fixed the issue.
Thanks!
Andrew Bennett
>going for 64 bit >not knowing malloc requires stdlib
Is this why everyone thinks us C guys are pretentious faggots? Learn the fucking basics man.
Benjamin Hughes
how do you learn the basics if you don't make the basic mistakes?
Jeremiah Gray
>Don't cast the return value of malloc, dumbass. Why?
Jordan Martin
You have a whole bunch of retarded errors that most certainly are not "fixed" by this e.g. your check_for_duplicates() assumes the array always full size but you only expand it when adding numbers
Leo White
probably hides compiler warnings when you do things wrong i always cast my mallocs
William Ramirez
Wrong. It's not specific to any architecture
Parker Barnes
This is true. The check_for_duplicates loop is definitely bad coded. I just sketched it out for testing purposes.
Chase Hall
C is portable because it requires less bits. C++ was made for bigger and heavier programs that need to do specific tasks. That's why C is 32 and C++ is 64.
Wyatt Scott
>the joke your head.png
Christian Brooks
This is a really good point
Cooper Johnson
that explain a lot, thank you
Daniel Ross
>MAX_CHILDS >childs
Connor Howard
Did you change the reference libraries to the x64 version?
Jonathan Moore
>(you can probably use botnet if you don't want to set it up) Fixed and bump for OP.