Gamestudio Links
Zorro Links
Newest Posts
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Help with plotting multiple ZigZag
by M_D. 04/26/24 20:03
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (1 invisible), 735 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 3 1 2 3
Xor Memory Problem. #487932
11/23/23 18:41
11/23/23 18:41
Joined: Sep 2017
Posts: 82
T
TipmyPip Offline OP
Junior Member
TipmyPip  Offline OP
Junior Member
T

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?



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


ZorroTraderGPT - https://bit.ly/3Gbsm4S
Re: Xor Memory Problem. [Re: TipmyPip] #487933
11/25/23 13:53
11/25/23 13:53
Joined: Feb 2017
Posts: 1,725
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,725
Chicago

Re: Xor Memory Problem. [Re: TipmyPip] #487934
11/25/23 16:36
11/25/23 16:36
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
The array size calculation looks wrong. See the remarks about the sizeof macro in the manual.

https://zorro-project.com/manual/en/structs.htm

Re: Xor Memory Problem. [Re: AndrewAMD] #487936
11/26/23 08:25
11/26/23 08:25
Joined: Sep 2017
Posts: 82
T
TipmyPip Offline OP
Junior Member
TipmyPip  Offline OP
Junior Member
T

Joined: Sep 2017
Posts: 82
>Did you troubleshoot your code?
>https://zorro-project.com/manual/en/chart.htm
>https://zorro-project.com/manual/en/trouble.htm
>https://zorro-project.com/manual/en/errors.htm

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?

>The array size calculation looks wrong. See the remarks about the sizeof macro in the manual.
>https://zorro-project.com/manual/en/structs.htm

Please explain and show me what is the right size of calculation in relation to the problem, and what calculation you expect, please, I would love to understand deeper, so we can solve the problem in lite-C,

Thank you


ZorroTraderGPT - https://bit.ly/3Gbsm4S
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 Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,725
Chicago
Originally Posted by TipmyPip
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
T
TipmyPip Offline OP
Junior Member
TipmyPip  Offline OP
Junior Member
T

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.

ZorroTraderGPT - https://bit.ly/3Gbsm4S
Re: Xor Memory Problem. [Re: TipmyPip] #487942
11/27/23 16:05
11/27/23 16:05
Joined: Feb 2017
Posts: 1,725
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,725
Chicago
Did you not see that your question was answered?

Try putting a dummy struct variable upstream of your sizeof calls. And then test sizeof to confirm it is outputting the expected values.

Re: Xor Memory Problem. [Re: AndrewAMD] #487943
11/28/23 09:56
11/28/23 09:56
Joined: Sep 2017
Posts: 82
T
TipmyPip Offline OP
Junior Member
TipmyPip  Offline OP
Junior Member
T

Joined: Sep 2017
Posts: 82
>Try putting a dummy struct variable upstream of your sizeof calls. And then test sizeof to confirm it is outputting the expected values.

Have you tried your solution? Did it create any errors, If not please share the code, so I could understand by your code how did you resolve the issue.

But if you have not tried your solution and you only rely on your imagination, Please explain to me the logic by code, and clear syntax so I can see and understand your solution.
Thank you very much for your precious time, and positive contribution solution, that way, I could become a serious programmer like you.


ZorroTraderGPT - https://bit.ly/3Gbsm4S
Re: Xor Memory Problem. [Re: TipmyPip] #487944
11/28/23 10:18
11/28/23 10:18
Joined: Feb 2017
Posts: 1,725
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,725
Chicago
I’m happy to give you free supervision as you troubleshoot your code that is incompatible with Lite C. Good luck!

Re: Xor Memory Problem. [Re: AndrewAMD] #487945
11/28/23 10:36
11/28/23 10:36
Joined: Sep 2017
Posts: 82
T
TipmyPip Offline OP
Junior Member
TipmyPip  Offline OP
Junior Member
T

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.

ZorroTraderGPT - https://bit.ly/3Gbsm4S
Page 1 of 3 1 2 3

Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1