Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (dr_panther, 7th_zorro), 1,203 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
sizeof() behaviour #456732
12/07/15 10:52
12/07/15 10:52
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Kartoffel Offline OP
Expert
Kartoffel  Offline OP
Expert

Joined: Jun 2009
Posts: 2,210
Bavaria, Germany




Why isn't this printing out 4096?

I'm questioning my sanity right now.


POTATO-MAN saves the day! - Random
Re: sizeof() behaviour [Re: Kartoffel] #456733
12/07/15 11:30
12/07/15 11:30
Joined: Mar 2012
Posts: 927
cyberspace
W
Wjbender Offline
User
Wjbender  Offline
User
W

Joined: Mar 2012
Posts: 927
cyberspace
rest assured.

Code:
obj pluto;
printf ("%I",(int)sizeof (pluto));



Compulsive compiler
Re: sizeof() behaviour [Re: Wjbender] #456734
12/07/15 11:56
12/07/15 11:56
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Kartoffel Offline OP
Expert
Kartoffel  Offline OP
Expert

Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Thanks, I'm aware of this, but shouldn't it work the way I've written it?
In c++ it sure does and I'm pretty sure that it should work the same in standard c.

...well, this basically means that all of my memory allocations for user defined structs that I ever used are completely wrong, at least in lite-c.


POTATO-MAN saves the day! - Random
Re: sizeof() behaviour [Re: Kartoffel] #456735
12/07/15 12:43
12/07/15 12:43
Joined: Mar 2012
Posts: 927
cyberspace
W
Wjbender Offline
User
Wjbender  Offline
User
W

Joined: Mar 2012
Posts: 927
cyberspace
ask jcl ..

Code:
obj.shit [0]=0;
//should this work ?



Compulsive compiler
Re: sizeof() behaviour [Re: Wjbender] #456737
12/07/15 14:26
12/07/15 14:26
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Kartoffel Offline OP
Expert
Kartoffel  Offline OP
Expert

Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Seems like this is the only way to do it, if you want to allocate memory for a struct pointer:

Code:
Obj * A;

A = sys_malloc(sizeof((*Obj)));

Edit: note that you need the aditional () around *Obj, otherwise sizeof will return 0
Edit2: nvm. this one doesn't seem to work in all cases

Last edited by Kartoffel; 12/07/15 14:54.

POTATO-MAN saves the day! - Random
Re: sizeof() behaviour [Re: Kartoffel] #456738
12/07/15 14:59
12/07/15 14:59
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
This is pretty strange. I used sizeof intensively and never had memory leaks that I suppose it occurs when needed data lenght is not properly allocated... confused

Re: sizeof() behaviour [Re: txesmi] #456739
12/07/15 15:19
12/07/15 15:19
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Me too, using it regularly for allocating stuff dynamically and sending data through the network.
This is really strange indeed.

What I've tried:
Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>
///////////////////////////////

struct _OBJ
{
	int what;
	var superwhat;
	//int watchYourLanguage[4];	
};

typedef struct _OBJ OBJ;

OBJ myobj;

void main()
{
	int size = 5+(sizeof(OBJ))*5;
	printf("%d",size); // returns 5
}



"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
Re: sizeof() behaviour [Re: Superku] #456747
12/08/15 08:44
12/08/15 08:44
Joined: Apr 2015
Posts: 20
Vietnam
F
Florastamine Offline
Newbie
Florastamine  Offline
Newbie
F

Joined: Apr 2015
Posts: 20
Vietnam
I've had this problem before, and probably the best bet is you have to manage the array inside the struct itself (as sizeof() doesn't work correctly in Lite-C as it does in C/C++). Something like:
Code:
#define    SIZE    1000
typedef struct {
int member[SIZE];
} Object;

Object *ptr = (Object *) sys_malloc( sizeof(Object) + (SIZE * sizeof(int)) );
<...>
sys_free(ptr);



Edited, just checked the manual and it says:
Quote:
Arrays are internally treated as a pointer to a memory area. So, sizeof(any_array) always equals to 4 because that is the size of a pointer.


So wherever you defined your arrays as int i[4]; or int *i; it will have the same internal layout, and thus must be managed manually.

By the way int main() is a far more better practice.

Last edited by Florastamine; 12/08/15 09:02.
Re: sizeof() behaviour [Re: Florastamine] #456749
12/08/15 09:25
12/08/15 09:25
Joined: Mar 2012
Posts: 927
cyberspace
W
Wjbender Offline
User
Wjbender  Offline
User
W

Joined: Mar 2012
Posts: 927
cyberspace
Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>
///////////////////////////////

//works fine<----------
struct objects1
{
	int a[1024];	
}obj1,obj2;

//typedef doesnt work <------------
typedef struct objects2
{
	int a[1024];	
}objects2;

function main()
{
	printf("objects1 %d,%d",(long)sizeof(obj1),(long)sizeof(obj2));//works fine<----------
	printf("objects2 %d",(long)sizeof(objects2));//typedef doesnt work  <------------
}



problem comes with typedef .


Compulsive compiler
Re: sizeof() behaviour [Re: Wjbender] #456750
12/08/15 10:18
12/08/15 10:18
Joined: Mar 2012
Posts: 927
cyberspace
W
Wjbender Offline
User
Wjbender  Offline
User
W

Joined: Mar 2012
Posts: 927
cyberspace
Originally Posted By: Wjbender
ask jcl ..

Code:
obj.stool[0]=0;
//should this work ?



Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>
///////////////////////////////

//works fine<----------
struct objects1
{
	int a[1024];	
}obj1,obj2;

//typedef doesnt work <------------
typedef struct objects2
{
	int a[1024];	
}objects2;

function main()
{
	obj1.a[0]=100;//<----- correct
	objects2.a[0]=100;//<----syntax error
}



Compulsive compiler
Page 1 of 2 1 2

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

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