sizeof() behaviour

Posted By: Kartoffel

sizeof() behaviour - 12/07/15 10:52





Why isn't this printing out 4096?

I'm questioning my sanity right now.
Posted By: Wjbender

Re: sizeof() behaviour - 12/07/15 11:30

rest assured.

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

Posted By: Kartoffel

Re: sizeof() behaviour - 12/07/15 11:56

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.
Posted By: Wjbender

Re: sizeof() behaviour - 12/07/15 12:43

ask jcl ..

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

Posted By: Kartoffel

Re: sizeof() behaviour - 12/07/15 14:26

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
Posted By: txesmi

Re: sizeof() behaviour - 12/07/15 14:59

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
Posted By: Superku

Re: sizeof() behaviour - 12/07/15 15:19

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
}

Posted By: Florastamine

Re: sizeof() behaviour - 12/08/15 08:44

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.
Posted By: Wjbender

Re: sizeof() behaviour - 12/08/15 09:25

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 .
Posted By: Wjbender

Re: sizeof() behaviour - 12/08/15 10:18

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
}

Posted By: MasterQ32

Re: sizeof() behaviour - 12/08/15 14:05

The second example is wrong wink
typedef has the syntax:
typedef oldType newName;
in your case, newName is 'objects2', oldType is
Code:
struct objects2 {
    int a[1024];
}



so objects2 is actually a type name and not an object. you must declare
Code:
objects2 obj3;

to get an acutal object of type objects2
Posted By: Wjbender

Re: sizeof() behaviour - 12/08/15 14:29

it is in relation to this question



Originally Posted By: Wjbender
rest assured.

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



Originally Posted By: Kartoffel
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.


where :

Code:
typedef struct
{
   int stuff [1024];
}obj;


© 2024 lite-C Forums