Problem with C-MEX S-function and atoi (pointers)

3 views (last 30 days)
Hello everybody,
I have a problem with a C S-function code. After trying many times, my mdlOutputs code is in the momment the following one:
static void mdlOutputs(SimStruct *S, int_T tid)
{
int Input_Node_A_value, Input_Node_B_value, Input_Node_C_value, B_Failure_value, C_Failure_value;
char in_nodeA_char, in_nodeB_char, in_nodeC_char, in_failB_char, in_failC_char;
char *in_nodeA_char_ptr = &in_nodeA_char, *in_nodeB_char_ptr = &in_nodeB_char, *in_nodeC_char_ptr = &in_nodeC_char, *in_failB_char_ptr = &in_failB_char, *in_failC_char_ptr = &in_failC_char;
// 5 input ports (boolean):
InputBooleanPtrsType Input_Node_A_ptrs = ssGetInputPortSignalPtrs(S,0);
InputBooleanPtrsType Input_Node_B_ptrs = ssGetInputPortSignalPtrs(S,1);
InputBooleanPtrsType Input_Node_C_ptrs = ssGetInputPortSignalPtrs(S,2);
InputBooleanPtrsType B_Failure_ptrs = ssGetInputPortSignalPtrs(S,3);
InputBooleanPtrsType C_Failure_ptrs = ssGetInputPortSignalPtrs(S,4);
// 5 Output ports (boolean):
boolean_T *Voted_Output = ssGetOutputPortSignal(S,0);
boolean_T *ExactVotingLocalInputFailure = ssGetOutputPortSignal(S,1);
boolean_T *VotingNoInputsFailure = ssGetOutputPortSignal(S,2);
// display inputs
ssPrintf("INPUTS ORIGINALES\n");
ssPrintf("NodeA: %d ", *Input_Node_A_ptrs[0]);
ssPrintf("NodeB: %d ", *Input_Node_B_ptrs[0]);
ssPrintf("NodeC: %d ", *Input_Node_C_ptrs[0]);
ssPrintf("FailB: %d ", *B_Failure_ptrs[0]);
ssPrintf("FailC: %d\n", *C_Failure_ptrs[0]);
// Save the value of the input ports:
Input_Node_A_value = *Input_Node_A_ptrs[0];
Input_Node_B_value = *Input_Node_B_ptrs[0];
Input_Node_C_value = *Input_Node_C_ptrs[0];
B_Failure_value = *B_Failure_ptrs[0];
C_Failure_value = *C_Failure_ptrs[0];
// Convert the value of the inputs to characters:
itoa(Input_Node_A_value, in_nodeA_char_ptr, 10);
itoa(Input_Node_B_value, in_nodeB_char_ptr, 10);
itoa(Input_Node_C_value, in_nodeC_char_ptr, 10);
itoa(B_Failure_value, in_failB_char_ptr, 10);
itoa(C_Failure_value, in_failC_char_ptr, 10);
//display conversion results
ssPrintf("INPUTS TRAS CAMBIO\n");
ssPrintf("NodeA: %c ", *in_nodeA_char_ptr);
ssPrintf("NodeB: %c ", *in_nodeB_char_ptr);
ssPrintf("NodeC: %c ", *in_nodeC_char_ptr);
ssPrintf("FailB: %c ", *in_failB_char_ptr);
ssPrintf("FailC: %c\n", *in_failC_char_ptr);
// Just write a 0 in the outputs.
*Voted_Output = 0;
*ExactVotingLocalInputFailure = 0;
*VotingNoInputsFailure = 0;
}
I don´t know what is wrong with the code, but Matlab executes it correctly once (well, not so correctly, it does some strange things when converting the values), and then it breaks with the following diagnostic:
Warning: Using a default value of 0.2 for maximum step
size. The simulation step size will be equal to or less
than this value. You can disable this diagnostic by
setting 'Automatic solver parameter selection' diagnostic
to 'none' in the Diagnostics page of the configuration
parameters dialog.
INPUTS ORIGINALES
NodeA: 1 NodeB: 1 NodeC: 1 FailB: 0 FailC: 0
INPUTS TRAS CAMBIO
NodeA: 1 NodeB: NodeC: 1 FailB: 0 FailC:
Checked all 6 cache memory tables. Found no corrupted tables.
Saving minidump to file C:\DOCUME~1\IAYEST~1\CONFIG~1\Temp\matlab_crash_dump.2836.0.dmp
------------------------------------------------------------------------
Segmentation violation detected at Fri May 13 09:06:47 2011
------------------------------------------------------------------------
Configuration:
MATLAB Version: 7.5.0.342 (R2007b)
MATLAB License: 282444
Operating System: Microsoft Windows XP
Window System: Version 5.1 (Build 2600: Service Pack 3)
Processor ID: x86 Family 15 Model 6 Stepping 4, GenuineIntel
Virtual Machine: Java 1.6.0 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode
Default Charset: windows-1252
Register State:
EAX = 0000001b EBX = 18f7dcdb
ECX = cc32a696 EDX = 7a8531c4
ESI = 18f7dcd3 EDI = 12faf050
EBP = 00ceaec4 ESP = 00ceaea8
EIP = 7abc2f65 FLG = 00010246
Stack Trace:
[0] libut.dll:_mw_free32(0x18f7dcdb, 0x00ceaf00, 0x6faab51c, 0x18f7dcdb) + 117 bytes
[1] libut.dll:_utFree(0x18f7dcdb, 0x18f7dcdb, 0x015e5620, 0) + 80 bytes
[2] libmwsimulink.dll:class slErrMsgQueue * __cdecl CMexSFcnOutputFcn(struct slSFcnInfo *)(0x78132c78, 5, 0x00ceaf58, 0x78132e24) + 1644 bytes
[3] 0x00ceaf58
Please follow these steps to report this problem to The MathWorks so we
have the best chance of correcting it:
The next time MATLAB is launched under typical usage, a dialog box will
open to help you send the error log to The MathWorks. Alternatively, you
can send an e-mail to segv@mathworks.com with the following file attached:
C:\DOCUME~1\IAYEST~1\CONFIG~1\Temp\matlab_crash_dump.2836
If the problem is reproducible, please submit a Service Request via:
http://www.mathworks.com/support/contact_us/ts/help_request_1.html
A technical support engineer might contact you with further information.
Thank you for your help. Save your workspace and restart MATLAB.
Saving minidump to file C:\DOCUME~1\IAYEST~1\CONFIG~1\Temp\matlab_crash_dump.2836.1.dmp
Windows exception while processing the previous exception
Segmentation violation occurred within signal handler.
Unable to complete stack trace (stack was probably corrupted)
Please follow these steps to report this problem to The MathWorks so we
have the best chance of correcting it:
The next time MATLAB is launched under typical usage, a dialog box will
open to help you send the error log to The MathWorks. Alternatively, you
can send an e-mail to segv@mathworks.com with the following file attached:
C:\DOCUME~1\IAYEST~1\CONFIG~1\Temp\matlab_crash_dump.2836
If the problem is reproducible, please submit a Service Request via:
http://www.mathworks.com/support/contact_us/ts/help_request_1.html
A technical support engineer might contact you with further information.
Thank you for your help.
Saving minidump to file C:\DOCUME~1\IAYEST~1\CONFIG~1\Temp\matlab_crash_dump.2836.2.dmp
Windows exception while processing the previous exception
Segmentation violation occurred within signal handler.
Unable to complete stack trace (stack was probably corrupted)
Please follow these steps to report this problem to The MathWorks so we
have the best chance of correcting it:
I don´t know what am I doing wrong, can anybody help me?
Thank you in advance.

Answers (2)

Jan
Jan on 13 May 2011
This must fail:
char in_nodeA_char;
char *in_nodeA_char_ptr = &in_nodeA_char;
itoa(Input_Node_A_value, in_nodeA_char_ptr, 10);
"in_nodeA_char" is a single char value, which is referred by "in_nodeA_char_ptr". But by itoa, you write more than 1 character to the string in general. Therefore itoa is deprecated e.g. since MSVC2005. itoa_s would reveal such bugs. Either use this, or expand at least "in_nodeA_char" to a string with the needed number of characters.
  1 Comment
Iban
Iban on 13 May 2011
Hi Jan,
I don´t really need the 'in_nodeA_char_ptr' pointer. I just want to convert the integer 'Input_Node_A_value' into a char and store it in 'in_nodeA_char'. Both int and char are single values, actually they only can be '0' or '1'.
I tried with "in_nodeA_char = (char)Input_Node_A_value;" but it doesn´t work, because it transforms the integer value "1" in something like "1 ''". How can I make that conversion?
As you see I don´t know very much about C...

Sign in to comment.


Iban
Iban on 13 May 2011
Hello,
I have tried with in_nodeA_char = Input_Node_A_value + 0x30; and the problem has gone.
Thank you for answering.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!