Re: MEX: using malloc with C++ in C wrapper, error conversion from void to myType
- From: "Stephen Decker" <incitemyriot@xxxxxxxxx>
- Date: Tue, 19 May 2009 18:00:21 +0000 (UTC)
James,
voltages is a variable that happens to be a pointer to Microvolt, whatever Microvolt is.
Somehow I was thinking of pointers as being just pointers, with an otherwise nebulous definition, but yes, pointers are variables containing an address. Microvolt is a double, fyi. CTypes.h contains:
//...
typedef double dB;
typedef double Hertz;
typedef double Microsec;
typedef double Millisec;
typedef double Percent;
typedef double Microvolt;
//...
voltages = mxMalloc( executed * sizeof(voltages));
No. This is wrong. Remember voltages is a pointer, so sizeof(voltages) simply returns the size of a pointer, probably 4 on your system. What you should be trying to do here is to allocate memory for an array of Microvolt objects, because that is the type of object voltages points to, not an array of Microvolt pointer objects. So you should be doing this:
voltages = mxMalloc( executed * sizeof(*voltages));
Using mxMalloc vs malloc has nothing to do with your crash & stack trace. The reason you are getting the crash is because you are corrupting memory with incorrect code.
Got it.
If you use malloc, then you need to call free. If you use mxMalloc, then you need to call mxFree.
Thanks. Forgot to update the rest of the code when I changed to mx.
Bruno,
voltages = (Microvolt*)mxMalloc ( executed * sizeof(Microvolt));
The above should be a correct syntax when the array size "executed" is correctly initialized, and overflowed use of voltage (remember C starts index from 0) is not occur later.
If it's not initialized it's not a surprise Malloc crashes.
In addition it is always recommended to check Malloc output is something different than NULL before process further.
the following code compiles and runs ok. Does everything appear to be properly initialized and checked?
//...
int executed;
int accumulated;
int *itmp;
Microvolt *voltages;
/* allocate memory */
itmp = mxMalloc( sizeof(*itmp));
if( itmp != NULL)
executed = (int)itmp;
else
mexPrintf("!!Error!! Malloc returned NULL to executed");
itmp = mxMalloc( sizeof(*itmp));
if( itmp != NULL)
accumulated = (int)itmp;
else
mexPrintf("!!Error!! Malloc returned NULL to accumulated");
voltages = mxMalloc( executed * sizeof(*voltages));
if( voltages == NULL){mexPrintf("!!Error!! Malloc returned NULL to voltages");}
//...
.
- Follow-Ups:
- References:
- MEX: using malloc with C++ in C wrapper, error conversion from void to myType
- From: Stephen Decker
- Re: MEX: using malloc with C++ in C wrapper, error conversion from void to myType
- From: James Tursa
- Re: MEX: using malloc with C++ in C wrapper, error conversion from void to myType
- From: Stephen Decker
- Re: MEX: using malloc with C++ in C wrapper, error conversion from void to myType
- From: Bruno Luong
- MEX: using malloc with C++ in C wrapper, error conversion from void to myType
- Prev by Date: Re: howto: programatically calibrate marker size in scatter
- Next by Date: Re: Database ToolBox Help Needed
- Previous by thread: Re: MEX: using malloc with C++ in C wrapper, error conversion from void to myType
- Next by thread: Re: MEX: using malloc with C++ in C wrapper, error conversion from void to myType
- Index(es):
Relevant Pages
|
Loading