There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer black screen boot bsod computer connection crash css dell display driver drivers email error ethernet excel explorer firefox firefox 3 game hard drive internet internet explorer itunes laptop linux malware monitor network networking nvidia outlook outlook 2003 outlook express partition password printer problem router slow software sound trojan usb video virus vista windows windows xp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Python getting sizeof from C


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
treydx's Avatar
Senior Member with 112 posts.
 
Join Date: Jan 2006
Experience: Intermediate
19-Mar-2008, 06:53 PM #1
Python getting sizeof from C
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/so...DiskCacheMap.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's Avatar
Computer Specs
Senior Member with 465 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
19-Mar-2008, 07:37 PM #2
You can use the len() function to get the length of a string. Is this what you want to achieve?
tomdkat's Avatar
Computer Specs
Distinguished Member with 3,715 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
19-Mar-2008, 08:49 PM #3
I think this is the thread for you to read. Hope that helps!

Peace...
treydx's Avatar
Senior Member with 112 posts.
 
Join Date: Jan 2006
Experience: Intermediate
20-Mar-2008, 09:25 PM #4
That sounds like just what I need tomdkat. Thanks
lotuseclat79's Avatar
Distinguished Member with 10,512 posts.
 
Join Date: Sep 2003
Location: -71.45091, 42.27841
21-Mar-2008, 02:22 PM #5
Quote:
Originally Posted by treydx View Post
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/so...DiskCacheMap.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!
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
__________________
The independence created by philosophical insight is - in my opinion - the mark of distinction between a mere artisan or specialist and a real seeker after truth. - Einstein wrote in 1944.

Some say knowledge is power, I say knowledge without action is powerless. - lotuseclat79

Don't confuse action with movement. - Hemingway to Gardner

Imagination is more important than knowledge. - Einstein
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 03:15 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.