porting non-malloc code to malloc

M

micromysore

Hi,
I want to post some code which has defn like

struct AVPicture {
int *data[4];
int linesize[4];
}
the is noe recognised by matlab, so i need to port to some form like
struct AVPicture {
int **data;
int *linesize;
}

Now whatz the best way to go about this process

1. write a wrapper header, but how to initialize the constants ?? like
... **data to *data[]
2. rewrites the source .. [impossible]

-madan
 
M

Michael Mair

Hi,
I want to post some code which has defn like

struct AVPicture {
int *data[4];
int linesize[4];
}
the is noe recognised by matlab, so i need to port to some form like
struct AVPicture {
int **data;
int *linesize;
}

Maybe it is just me but I cannot make much sense out of
this. Why do you want to post code but do not do it?
Matlab offers an interface for C code but this is a Matlab
thing, not a C thing.
What is the problem with the above structures?
That you have to pass only pointers to your data or that
you want to be able to size the arrays as you need them or ...?

Now whatz the best way to go about this process

1. write a wrapper header, but how to initialize the constants ?? like
.. **data to *data[]

What constants? Do you mean macros?
2. rewrites the source .. [impossible]

Please try to state your problem more clearly and provide
a minimal example which exhibits the erroneous behaviour or
shows the difference you need.


Cheers
Michael
 
M

micromysore

Matlab has the ability to load MSVC compliant DLL's, atleast thatz what
they claim.

matlab has a function called loadlibary, which can can load my DLL
for ex: loadlibrary('mylib.dll','mylib.h')
where mylib.h is the interface header file.

the problem with matlab is that, it works perfectly laod the dll as
long as there are pointers to functions , and sized constants (ex. int
linesize[4]) ..

Now my problem, is that the DLL i have already has those sized
constants. I need to write a wrapper, which basically converts the
single or 2D pointers, to the corresponding formats.
for ex. the matlab interface needs int *linesize, but the DLL has int
linesize[4].

-madan
 
W

Walter Roberson

:Now my problem, is that the DLL i have already has those sized
:constants. I need to write a wrapper, which basically converts the
:single or 2D pointers, to the corresponding formats.
:for ex. the matlab interface needs int *linesize, but the DLL has int
:linesize[4].

If you were to pass an arbitrary structure into a C function, by
casting the pointer to void*, then there is no standard way for the
function to be able to figure out what the structure looks like
internally. (Some implimentations might have non-standard ways.) You
will thus not be able to write a C function which can be passed an
arbitrary structure and which will construct the appropriate new
structure.

Similarily, if you try to work it at the preprocessor level, you will
find that the preprocessor has no way to refer to items such as "the
first element of the named parameter". You thus will not be able to do
what you want by a preprocessor macro being passed an arbitrary
structure.

If you were willing to rewrite all your structure definitions, then you
could perhaps [in newer versions of C] write something like a
"DEFINE_STRUCTURE" macro that took as arguments all the pieces of the
structure, and which emitted both the regular 'struct' definition and
the modified definition you needed. Except that you want more than just
that -- you would also want to emit a pair of function that converted
between the two forms. I don't know if that is possible with a variable
number of arguments even in C99 [I haven't looked at what the C99
preprocessor is capable of.]


What I would suggest to you is that your problem would be most easily
solved outside the C program itself, by using some preprocessing
of your header files. As the preprocessing would be largely
textual, I would suggest that a program such as perl might be
easier to impliment this in than in pure C. The idea would be
to write a program (e.g., perl script) which read your header
files that define your structures, and emitted code that
defined the modified header and defines functions to do the
conversion. If you have lots of code, you could get fancier and
add a script that read through the code, figured out which
structures were referenced, and inserted #include statements
into the code to import the modified structure definitions
and conversion functions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top