0 registered members (),
1,459
guests, and 9
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: sizeof() behaviour
[Re: Kartoffel]
#456733
12/07/15 11:30
12/07/15 11:30
|
Joined: Mar 2012
Posts: 927 cyberspace
Wjbender
User
|
User
Joined: Mar 2012
Posts: 927
cyberspace
|
rest assured.
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
OP
Expert
|
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
Wjbender
User
|
User
Joined: Mar 2012
Posts: 927
cyberspace
|
ask jcl ..
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
OP
Expert
|
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:
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: txesmi]
#456739
12/07/15 15:19
12/07/15 15:19
|
Joined: Sep 2003
Posts: 6,861 Kiel (Germany)
Superku
Senior Expert
|
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:
///////////////////////////////
#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
Florastamine
Newbie
|
Newbie
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:
#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: 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
Wjbender
User
|
User
Joined: Mar 2012
Posts: 927
cyberspace
|
///////////////////////////////
#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
Wjbender
User
|
User
Joined: Mar 2012
Posts: 927
cyberspace
|
ask jcl ..
obj.stool[0]=0;
//should this work ?
///////////////////////////////
#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
|
|
|
|