Gamestudio Links
Zorro Links
Newest Posts
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Trading Journey
by 7th_zorro. 04/27/24 04:42
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
MT5 bridge not working on MT5 v. 5 build 4160
by EternallyCurious. 04/25/24 20:49
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (TipmyPip, Ayumi), 773 guests, and 3 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
[SOLVED]-Arrays - get number of slots #467080
07/14/17 21:24
07/14/17 21:24
Joined: Jun 2017
Posts: 78
B
BobbyT Offline OP
Junior Member
BobbyT  Offline OP
Junior Member
B

Joined: Jun 2017
Posts: 78
Looks who's back, back again....

I'm trying to get the length of an array. According to SO posts and C manuals this is achievable using sizeof()

I have got the size of a single array element but the size of the whole array is not retrieved properly. I'm certain I've done this correctly according to C rules but maybe it's different in lite-C? Is there a way to get an array length in lite-C?
Code:
void main()
{	
	int tfArray[3] = {5,60,1};

	//get the size of the whole array
	int arSize = sizeof(tfArray);
	//get the size of the first element in the array
	int arElementSize = sizeof(&tfArray[0]);
	//get the array length
	int arLen = sizeof(tfArray)/sizeof(&tfArray[0]);
	
	//report what we find
	printf("nArray size in bytes = %i", arSize);
	printf("nArray element size in bytes = %i", arElementSize);
	printf("nArray length = %i", arLen);
}


Cheers,
BobbyT

EDIT: I have also tried the malloc/free trick (I think) but the values returned are way off. I feel I'm closer but, not by much. Here's the code implementing the use of malloc/free/memory:
Code:
function main()
{	
	int tfArray[3] = {5,60,1};
	
	printf("nMemory before malloc is %i", memory(0));
	
	int array = malloc(tfArray);
	int mem = memory(0);
	
	printf("nMemory after malloc is %i", memory(0));
	
	free(array);
	
	printf("nMemory after free is %i", memory(0));
	
	//get the size of the whole array
	int arSize = mem - memory(0);	
	//get the size of the first element in the array
	int arElementSize = sizeof(&tfArray[0]);
	//get the array length
	int arLen = arSize/sizeof(&tfArray[0]);
	
	//report what we find
	printf("nArray size in bytes = %i", arSize);
	printf("nArray element size in bytes = %i", arElementSize);
	printf("nArray length = %i", arLen);
}


Last edited by BobbyT; 07/18/17 03:28.
Re: Arrays - get number of slots [Re: BobbyT] #467101
07/15/17 15:55
07/15/17 15:55
Joined: Jun 2017
Posts: 78
B
BobbyT Offline OP
Junior Member
BobbyT  Offline OP
Junior Member
B

Joined: Jun 2017
Posts: 78
It dawned on me that including some outputs may be of use.

Output from test block 1)

Array size in bytes = 4
Array element size in bytes = 4
Array length = 1

So I get that the issue with arrays is they degrade to pointers at the time of creation. But, according to C rules, sizeof(array) should get the size of the array that the array variable now points to (in bytes). That doesn't seem to be the case in lite-c and it just returns the size of the pointer.

Output from test block 2)

Memory before malloc is 4059136
Memory after malloc is 7479296
Memory after free is 4059136
Array size in bytes = 3420160
Array element size in bytes = 4
Array length = 855040

In this block I tried using the 'malloc/free' trick that is referred to in the Variables section of the manual. However I cannot find reference to what that actual method is and improvised. You can see that there is space allocated for the array but it is way too much. I have a feeling that my typecasting or how I'm passing the pointer around that is causing the problem however pointers and memory functions are completely foreign to me smirk

Cheers,
BobbyT

Re: Arrays - get number of slots [Re: BobbyT] #467103
07/15/17 17:05
07/15/17 17:05
Joined: Jun 2017
Posts: 78
B
BobbyT Offline OP
Junior Member
BobbyT  Offline OP
Junior Member
B

Joined: Jun 2017
Posts: 78
OK. So maybe a step closer here. I have managed to implement sizeof() ala lite-C style by using it on a structure.
This gets the correct values however it raises the question of how to do this dynamically. Even if it's just at runtime.
As it stands, I would need to modify several lines of code every time I modify the array length (I'm only interested in detecting the array length at runtime.
Surely there is a way to do this. The manual indicates there is but dammed if I can find the information in there smirk

Cheers,
BobbyT

PS: Here's the code (for laughs sake)
Code:
#include <stdio.h>
#include <default.c>

typedef struct {
	int slot1;
	int slot2;
	int slot3;
} arSTRUCT;

arSTRUCT* stuff = {slot1 = 5; slot2 = 15; slot3 = 30;}

function main()
{	
	//get the size of the whole array
	int arSize = sizeof(arSTRUCT);	
	//get the size of the first element in the array
	int arElementSize = sizeof(arSTRUCT*);
	//get the array length
	int arLen = arSize/sizeof(arSTRUCT*);
	
	//report what we find
	printf("nArray size in bytes = %i", arSize);
	printf("nArray element size in bytes = %i", arElementSize);
	printf("nArray length = %i", arLen);
}


Last edited by BobbyT; 07/15/17 17:05.
Re: Arrays - get number of slots [Re: BobbyT] #467115
07/16/17 20:31
07/16/17 20:31
Joined: Jun 2017
Posts: 78
B
BobbyT Offline OP
Junior Member
BobbyT  Offline OP
Junior Member
B

Joined: Jun 2017
Posts: 78
Well, problem solved.

Here's how to get the length of an array. It's amazing what a PROPER UNDERSTANDING of how functions work will do for you. while() returns a bool. If you keep the last slot in the array as NULL, and NULL values are false. Kinda writes itself really haha.

Code:
int getArrayLength(int *array) 
{
 	int n = 0;
 	while (array[n]) n++;
 	return n;
}



And here's a block that gets the minimum or maximum value from an array. Requires #define MIN 1 and #define MAX 2 to work as is.

Code:
int getArrayLimit(int *array, size_t length, int dir) //set dir to MAX or MIN
{
  	size_t i = 1;	//set idx to 1 as we initialise val to first slot [0]
	int *val = array[0];
	
	switch(dir)
	{
		case 1: 	for(i; i<length; i++)
  					{
    					if((int)val > array[i]) {
      					val = array[i];
    					}
  					} break;
  		case 2:	        for(i; i<length; i++)
  					{
    					if((int)val < array[i]) {
      					val = array[i];
      				}
  					} break;		
  		default:	printf("nSelect MAX or MIN for getArrayLimit()");			
	}
  	return val;
 }



In all honesty though, getArrayLength was found somewhere here on the forum (in the GS3D area) and getArrayLimit was adapted from a C code block found on SO. Gets the job done though laugh

Cheers,
BobbyT


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1