[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Compiling a C program in 64 bits

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 46
Thread images: 1

File: exception error.png (15KB, 553x359px) Image search: [Google]
exception error.png
15KB, 553x359px
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 /g/?

What's up with the x64 version of the compiled code?
>>
>casting the return value of malloc in C
>>
>>60142849

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;
}
>>
>>60142862
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
>>
>>60142849
C is 32-bit. C++ is 64-bit.
>>
>>60143026
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.
>>
>>60143196
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
}
>>
>>60143218
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);
}
}

>>
>>60143233
why not using https://pastebin.com/
>>
>>60143233
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);
}
}

}
>>
>>60143249
#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;

>>
>>60143247
good idea anon

https://pastebin.com/6xBzfM7W
>>
>>60143214
This. Debug it you dumb fuck, there's probably UB somewhere.
>>
>>60142849
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.
>>
>>60143287
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 '<stdlib.h>' 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 '<stdlib.h>' 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 '<stdlib.h>' 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();
^~~~~~~~~~~~~~~~~~~
>>
>>60143323
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 <stdlib.h> 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 <stdlib.h> 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 ‘<stdlib.h>’ 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 ‘<stdlib.h>’ 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 ‘<stdlib.h>’ 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.
>>
>>60142849
 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
>>
>>60143331
What compiler is this?
>>
>>60143438
NULL is just a 0
>>
>>60143484
not for gcc
>>
>>60143482
gcc
>>
>>60143482
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;
^ ~~~~
>>
>>60143402
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.
>>
>>60143677
how do you learn the basics if you don't make the basic mistakes?
>>
>>60143402
>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 >>60143637


e.g. your check_for_duplicates() assumes the array always full size but you only expand it when adding numbers
>>
>>60143706
probably hides compiler warnings when you do things wrong
i always cast my mallocs
>>
>>60143190
Wrong. It's not specific to any architecture
>>
>>60143735

This is true. The check_for_duplicates loop is definitely bad coded. I just sketched it out for testing purposes.
>>
>>60143769
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.
>>
>>60143769
>the joke your head.png
>>
>>60143595
This is a really good point
>>
>>60143836
that explain a lot, thank you
>>
>>60142849
>
MAX_CHILDS

>childs
>>
>>60142849
Did you change the reference libraries to the x64 version?
>>
>>60143323
>(you can probably use botnet if you don't want to set it up)
Fixed and bump for OP.
Thread posts: 46
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.