Re: MEX: using malloc with C++ in C wrapper, error conversion from void to myType



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");}

//...
.



Relevant Pages

  • Re: structures, structures and more structures (questions about nested structures)
    ... >>>typedef union { ... How do I calculate the size of the structure MotherStruct ... of all members, including pointers. ... How can sizeof know how much memory you have allocated for the char* ...
    (comp.lang.c)
  • Re: Question about a struct declaration
    ... typedef struct dummy *dummy; ... If want to use the type as a pointer, then hiding the fact that it is a pointer in a typedef is indeed a pretty useless idea. ... A classic example would be the interface of 'pthreads' library, where 'pthread_t', 'pthread_mutex_t' etc. might easily be pointers. ... work in case when you need to self-refer to the struct type from inside ...
    (comp.lang.c)
  • Re: Function Pointers
    ... If you feel the need for a generic pointer-to-function type, ... should probably create a typedef for it. ... Others have shown you how to typedef a function pointer type. ... I also prefer to see the "pointerlyness" of pointers, ...
    (comp.lang.c)
  • Re: Function Pointers
    ... is that it's better not to do this, because I don't like hiding pointers ... So I would typedef the function type itself: ... Pointers to functions are fundamentally different from pointers to ... object pointers behind a typedef is Evil, ...
    (comp.lang.c)
  • Re: Typdef pointers to structs or not?
    ... (This is straight from memory so it might not even compile.) ... The use case is AST representation which makes heavy use of pointers to ... structs ... modify these structs so either the whole typedef will be in a .h file ...
    (comp.lang.c)

Loading