3 registered members (Quad, VoroneTZ, PeroPero),
841
guests, and 3
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Xor Memory Problem.
#487932
11/23/23 18:41
11/23/23 18:41
|
Joined: Sep 2017
Posts: 82
TipmyPip
OP
Junior Member
|
OP
Junior Member
Joined: Sep 2017
Posts: 82
|
Dear Programmers, I have the following code of which I am trying to run in Zorro lite-C but it crashes all the time. Does anyone know how to implement dynamic memory allocation within lite-C? #include <stdio.h>
#include <stdlib.h>
#define INT_BITS 32
typedef struct TrieNode {
struct TrieNode* bit[2];
} TrieNode;
TrieNode* newTrieNode() {
TrieNode* newNode = (TrieNode*)malloc(sizeof(TrieNode));
if (newNode != NULL) {
newNode->bit[0] = newNode->bit[1] = NULL;
}
return newNode;
}
void insert(TrieNode* root, int number) {
TrieNode* current = root;
for (int i = INT_BITS - 1; i >= 0; i--) {
int bit = (number >> i) & 1;
if (current->bit[bit] == NULL) {
current->bit[bit] = newTrieNode();
}
current = current->bit[bit];
}
}
int findMaxXOR(TrieNode* root, int number) {
TrieNode* current = root;
int maxXOR = 0;
for (int i = INT_BITS - 1; i >= 0; i--) {
int bit = (number >> i) & 1;
if (current->bit[1 - bit] != NULL) {
maxXOR |= (1 << i);
current = current->bit[1 - bit];
} else {
current = current->bit[bit];
}
}
return maxXOR;
}
void freeTrie(TrieNode* root) {
if (root != NULL) {
freeTrie(root->bit[0]);
freeTrie(root->bit[1]);
free(root);
}
}
int getMaxXOR(int* arr, int size) {
TrieNode* root = newTrieNode();
int maxXOR = 0;
for (int i = 0; i < size; i++) {
insert(root, arr[i]);
int currentXOR = findMaxXOR(root, arr[i]);
if (currentXOR > maxXOR) {
maxXOR = currentXOR;
}
}
int result = maxXOR;
freeTrie(root);
return result;
}
int main() {
int arr[] = {3, 10, 5, 25, 2, 8};
int size = sizeof(arr) / sizeof(arr[0]);
int result = getMaxXOR(arr, size);
printf("Maximum XOR: %d\n", result);
return 0;
}
|
|
|
Re: Xor Memory Problem.
[Re: TipmyPip]
#487940
11/26/23 20:53
11/26/23 20:53
|
Joined: Feb 2017
Posts: 1,725 Chicago
AndrewAMD
Serious User
|
Serious User
Joined: Feb 2017
Posts: 1,725
Chicago
|
Thank you for your response, Have you tried running my code in a regular C compiler? Do you know what this code performs? and why? Why do you think I am asking the question in the forum? I'm not paid to lurk here. Now oP Group support is paid, so you can always try them. Also, jcl is right. Read the referenced manual page and run your own tests.
|
|
|
Re: Xor Memory Problem.
[Re: AndrewAMD]
#487941
11/27/23 00:24
11/27/23 00:24
|
Joined: Sep 2017
Posts: 82
TipmyPip
OP
Junior Member
|
OP
Junior Member
Joined: Sep 2017
Posts: 82
|
I am sorry, This was not a question for paid support, The question was for curious minds, who don't care for money... because the money comes back on its own. But you are right, if your time is very precious, I guess you would have other means of making money, every minute of the day.
We all need to improve on our logic Sir, I am here with the question because the reference manual will not answer tricky questions. Only programmers, who can go beyond their own limits of their imagination.
Thank you in any case for your precious time.
Last edited by TipmyPip; 11/27/23 00:25.
|
|
|
Re: Xor Memory Problem.
[Re: AndrewAMD]
#487945
11/28/23 10:36
11/28/23 10:36
|
Joined: Sep 2017
Posts: 82
TipmyPip
OP
Junior Member
|
OP
Junior Member
Joined: Sep 2017
Posts: 82
|
But you already gave very good suggestions of "putting a dummy struct variable upstream of your sizeof calls." Please show me in code what and how do you do that, if you understand what you mean.
Do you know what the code above performs?
Supervision can be given only on proper code. Not on poetic notions of code.
You always project your own boundaries on reality, which you want to go beyond, but only a few who walk the talk can put it into code...
Last edited by TipmyPip; 11/28/23 10:43.
|
|
|
|