# Python getting sizeof from C



## treydx (Jan 4, 2006)

I have a file which is created using C++ code. I'm parsing that file with python and I need to know the size of a struct that is the header of the file. Is there any way I can import the C or C++ code into my python code and call the C function sizeof()? Or is there a better way to do this?

More specifically, line 336 of this file 
http://lxr.mozilla.org/mozilla1.8/source/netwerk/cache/src/nsDiskCacheMap.h
is the structure that I need the size of, but I honestly can't even fathom a way to get it from python. I can just assume little_endian is true (I think most platforms that run netscape & firefox are little endian??), which is not ideal, but I still wouldn't know how big that structure is...what's the sizeof() a function anyway--the same as the data type of the function plus all the sizes of the variables in it?

If anyone could help me out with this, I would really appreciate it. Thanks!


----------



## TheRobatron (Oct 25, 2007)

You can use the len() function to get the length of a string. Is this what you want to achieve?


----------



## tomdkat (May 6, 2006)

I think this is the thread for you to read. Hope that helps! 

Peace...


----------



## treydx (Jan 4, 2006)

That sounds like just what I need tomdkat. Thanks


----------



## lotuseclat79 (Sep 12, 2003)

treydx said:


> I have a file which is created using C++ code. I'm parsing that file with python and I need to know the size of a struct that is the header of the file. Is there any way I can import the C or C++ code into my python code and call the C function sizeof()? Or is there a better way to do this?
> 
> More specifically, line 336 of this file
> http://lxr.mozilla.org/mozilla1.8/source/netwerk/cache/src/nsDiskCacheMap.h
> ...


Hi treydx,

tomdcat gave you a great link. Howz it coming?

If the file was created using c++, it stands to reason that the sizeof(struct nsDiskCacheHeader) that Python is parsing is the same sizeof it that c++ created in the first place - does that reasoning make any sense?

Given that Python creates runtime structures dynamically, you would need to know that sizeof struct that c++ created - it seems to me.

The way I handled such things is to first create a small program that includes the .h header file you referenced in a small model program and then create the struct and print out its size in the small c++ model program as a test and to gain information on the size of the struct.

Typically, different languages can be linked together with interfaces if you know what you are doing - e.g. a high level language to assembler or C. Then when you compile at runtime, include the .o object file or something like that.

So, since things need to be computed at runtime, I would attempt to provide a small library interface that calls a new small c++ library with the sizeof function in it and is compiled with the header file containing the struct of interest that you will need to create from Python and pass the sizeof function the name of the struct and get the sizeof it returned at runtime to the Python interface.

Any of that make any sense? Or, maybe you have already solved the problem in which case let's hear how you solved it.

-- Tom


----------

