On 18/12/12 04:25, Marefe Serentas wrote:

int some_data[] =
{
     -288,   -153,     31,     24,    205,    110,     39,     88,
     -281,    145,     35,    266,     63,    -79,   -103,    -25,
       53,    145,   -114,   -274,     46,     60,    220,    205
};

typedef struct MainStruct
{
     A var1;
     u32 var2;
     u32 var3[max];
     u32 var4;
     u32 var5[max];
}MainStruct;

void generate()
{
     MainStruct myMain = {0};
...


     (void)memcpy((void *)myMain.var4,
                  (void *)some_data,
                  sizeof(some_data));

Won't this probably crash? It's trying to copy a table of data into an uninitialised pointer.


This is a work-problem. Given a c file as input, the customer wants me
to write a python script that will retrieve the values of myMain.

The values at what point in time? MyMain only exists for the lifetime of the generate function. It gets thrown away.

He wants those values converted to binary data and write it in a .bin
file.

Is there a real purpose for this exercise? It sounds like somebody is
trying to do something in a very convoluted way that almost certainly has a better solution. Parsing a C file to try and guess what its internal data structures contain and write it to a file - so that some other program can read it maybe? Do you even know what the machine architecture and compiler are?

myMain. The c file input content might change in the future like
different values assigned, added fields in the MainStruct, etc. Also I
am not permitted to change the c file.

That explains the desire to write a parser. But unless the changes to the C file are very restricted it is a futile task. You would need to build a full C language parser that exactly matches the C compiler that's being used (presumably) to build the program to read the bin file. And that's nearly impossible because C is not well defined, being full of implementation specific details.

About me, I'm a fresh graduate. I just started learning python a month
ago. I learned c in school, we had it for 2 years.

I'm using python 2.6. I'm running on Windows 7 64-bit OS.

What I did so far is parse the C source code.
But having a problem parsing the value of myMain.var4.

var4 will be a pointer which is an essentially random memory location.
I assume what you really want is the content that var4 points to, which should be a copy of some_data.

If it weren't for the dynamic nature of the C file I'd still suggest using your brain as parser and just writing out the *intended* data using the struct module. But if its dynamic you will need to try to parse it and hope nobody changes it too much.

Still sounds like the wrong design pattern to me!
C is a terrible data file format:-(


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to