Compiling a C program in 64 bits

Writing a small c program with the following data structure:

typedef struct node{
struct node* child[MAX_CHILDS];
} node;


and initializing node's childs in the following manner:

node *n = (node*)malloc(sizeof(node));

for (int i = 0; i < MAX_CHILDS; i++)
{
n->c[i] = NULL;
}


this compiles just fine in x86.

But for some reason when I'm trying to compile the same code in x64 I get access violation exception (pic related).

Why is this Sup Forums?

What's up with the x64 version of the compiled code?

Other urls found in this thread:

pastebin.com/
pastebin.com/6xBzfM7W
twitter.com/SFWRedditVideos

>casting the return value of malloc in C

Sorry for the init part the code is as follows:

node *n = (node*)malloc(sizeof(node));

for (int i = 0; i < MAX_CHILDS; i++)
{
n->child[i] = NULL;
}

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

post full code

C is 32-bit. C++ is 64-bit.

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);

node *x = b_tree_new_node();
x->leaf = true;
x->n = 0;

tree T;
T.root = x;

printf(">building tree\n");

int known_random_number = random_number();

// insert nodes
for (int i = 0; i < NUM_OF_KEYS; i++) {
int random = random_number();
b_tree_insert(&T, test_array[i]);
}


getchar();

return 0;
}

So the debugger opens but you choose not to debug, and instead post here.


Kill yourself.

node *b_tree_new_node()
{
node *n = (node*)malloc(sizeof(node));

for (int i = 0; i < 2 * t; i++)
{
n->c[i] = NULL;
}

for (int i = 0; i < 2 * t - 1; i++)
{
n->key[i] = NULL;
}

return n;

}

void b_tree_split_child(node* x, int i)
{
node* z = b_tree_new_node();

node* y = x->c[i];

z->leaf = y->leaf;
z->n = t - 1;

for (int j = 0; j < t - 1; j++)
{
z->key[j] = y->key[j + t];
}

if (y->leaf == false)
{
for (int j = 0; j < t; j++)
{
z->c[j] = y->c[j + t];
}
}

y->n = t;

for (int j = x->n; j > i; j--)
{
x->c[j + 1] = x->c[j];
}
x->c[i + 1] = z;

for (int j = x->n; j > i; j--)
{
x->key[j] = x->key[j - 1];
}

x->key[i] = y->key[t - 1];
x->n = x->n + 1;

// DISK WRITE Y
// DISK WRITE Z
// DISK WRITE X
}

void b_tree_insert_nonfull(node* x, int k)
{

int i = x->n;

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;

b_tree_split_child(s, 0);
b_tree_insert_nonfull(s, k);
}

// if node is not full
else
{
b_tree_insert_nonfull(r, k);
}
}

why not using pastebin.com/

void init_random_generator()
{
srand(time(NULL));
}

int random_number()
{
rand();
return rand() % RAND_NUMBER_MAX + 1;
}

void init_array(int **n, int numToAdd)
{
*n = (int*)malloc(sizeof(int));

if (*n == NULL)
{
printf("Error in malloc!");
return;
}

(*n)[0] = numToAdd;
}

void add_to_array(int **n, int numToAdd)
{
static int sizeCount = 0;
sizeCount++;
int tempcount = sizeCount;

int *temp;

temp = (int*)realloc(*n, (sizeCount + 1) * sizeof(int));

if (temp == NULL)
{
printf("Error in realloc!");
return;
}

*n = temp;

(*n)[sizeCount] = numToAdd;

}

bool check_for_duplicates(int **test_array, int a_number)
{
for (int i = 0; i < NUM_OF_KEYS; i++)
{
if ((*test_array)[i] == a_number)
{
return true;
}
}
return false;
}

void load_array_data(int **test_array)
{
for (int i = 0; i < NUM_OF_KEYS; i++)
{
if (*test_array == NULL)
{
init_array(test_array, random_number());
}
else
{
int a_number = random_number();

while (check_for_duplicates(test_array, a_number) == true)
{
a_number = random_number();
}

add_to_array(test_array, a_number);
}
}

}

#define t 2
#define NUM_OF_KEYS 1000
#define RAND_NUMBER_MAX 1000

typedef struct node
{
int n; // number of nodes in leaf
int key[2 * t - 1]; // array of keys

bool leaf; // is leaf

struct node* c[2 * t]; // if not leaf the pointers to childs

} node;

typedef struct tree {
node* root;
} tree;

good idea user

pastebin.com/6xBzfM7W

This. Debug it you dumb fuck, there's probably UB somewhere.

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.

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();
^~~~~~~~~~~~~~~~~~~

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

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.

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();

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.

n->key[i] = NULL;
key is a tab of int, ou can't assign NULL to a int
otherwise laucnh on my machine

>loading test data
>building tree
>searching all keys
>all tests passed successfully

just including stdlib.h seems to fix your problem

What compiler is this?

NULL is just a 0

not for gcc

gcc

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;
^ ~~~~

this

Use calloc if you're just going to set everything to null. Save yourself some typing.

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

Using stdlib.h and removing casts on malloc fixed the issue.

Thanks!

>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.

how do you learn the basics if you don't make the basic mistakes?

>Don't cast the return value of malloc, dumbass.
Why?

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

probably hides compiler warnings when you do things wrong
i always cast my mallocs

Wrong. It's not specific to any architecture

This is true. The check_for_duplicates loop is definitely bad coded. I just sketched it out for testing purposes.

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.

>the joke your head.png

This is a really good point

that explain a lot, thank you

>MAX_CHILDS
>childs

Did you change the reference libraries to the x64 version?

>(you can probably use botnet if you don't want to set it up)
Fixed and bump for OP.