How do I cast a void pointer to a int[3]? - c++

I need to call a 3rd party library and pass in an int[3] as a void * like this [works]:
int pattern[3] = {2,4,10};
if ( OSTaskCreate( BlinkLED,
( void * ) pattern,
( void * ) &BlinkTaskStack[USER_TASK_STK_SIZE],
( void * ) BlinkTaskStack,
MAIN_PRIO - 1 ) != OS_NO_ERR )
{
iprintf( "*** Error creating blink task\r\n" );
}
But now I need to parse a string to get the pattern array and I can't seem to get it right.
First I pass the string into the parser and get back the array:
int (&ParseBlinkOnCommand(char rxbuffer[3]))[3]
{
// Code parses rxbuffer and creates the 3 ints needed
int pattern[3] = {repeats, onTicks, offTicks};
return pattern;
}
Then I try to pass it to the OSTaskCreate just like I did before:
int pattern2[3] = ParseBlinkOnCommand(rxbuffer);
if ( OSTaskCreate( BlinkLED,
( void * ) pattern2,
( void * ) &BlinkTaskStack[USER_TASK_STK_SIZE],
( void * ) BlinkTaskStack,
MAIN_PRIO - 1 ) != OS_NO_ERR )
{
iprintf( "*** Error creating remote blink task\r\n" );
}
but I get the error 'array must be initialized with a brace-enclosed initializer'.
What is the right way to do this?

First, ParseBlinkOnCommand returns reference to local object and so return dangling reference.
Second C-array are not copyable, so int pattern2[3] = ParseBlinkOnCommand(rxbuffer); should be int (&pattern2)[3] = ParseBlinkOnCommand(rxbuffer);.
but why not using std::vector or std::array (or custom structure) ?
std::vector<int> ParseBlinkOnCommand(const char (&rxbuffer)[3])
{
// Code parses rxbuffer and creates the 3 ints needed
return {repeats, onTicks, offTicks};
}
And then
auto pattern2 = ParseBlinkOnCommand(rxbuffer);
if ( OSTaskCreate( BlinkLED,
pattern2.data(),
&BlinkTaskStack[USER_TASK_STK_SIZE],
BlinkTaskStack,
MAIN_PRIO - 1 ) != OS_NO_ERR )
{
iprintf( "*** Error creating remote blink task\r\n" );
}

Related

How to pass Array of Objects using PHPCPP, loop through each object and return associative array

I have recently started learning PHPCPP - A C++ library for developing PHP extensions and trying to understand:
how to pass an Array of objects from php to C++ through PHPCPP
library as examples give only info about arrays and objects separately,
then how to loop through each object in C++
and how to return an associative array back to PHP
Can someone point me to the right direction?
I have come up with this example however need some help:
class Curve{
public :
double shape;
double peak;
double tpeak;
double start;
double lag;
double index;
};
Php::Value example1(Php::Parameters &params) {
vector<Curve> c = params[0];
//create an associative array and return to php
std::map< std::string, std::map<std::string, std::map<std::string, std::map<std::string, std::double>>> > data;
// loop through array of objects here or do something with an array
...
data[c.shape][c.peak][c.tpeak][c.start] = 1/12 * c.index;
return data;
}
extern "C" {
PHPCPP_EXPORT void *get_module() {
static Php::Extension myExtension("my_extension", "1.0");
myExtension.add<example1>("example1", {
Php::ByVal("curves", "Array", false);
});
eturn myExtension;
}
}
In contrary to your declaration
Php::ByVal("curves", "Array", false);
I am using
Php::ByVal("curves", "Array", true);
I did not test this piece of code, therefore it may be necessary to add some slight corrections :-)
Good luck!
public:
Php::Value GetAssociativeArray( Php::Parameters &params ) {
if ( params.size( ) < 1 ) {
error_function( "need at least 1 parameter" );
}
if ( ! params[ 0 ].isObject( ) ) {
error_function( "needs an object as first parameter" );
}
//
// what you have to do before you can use this example:
// define the data type data_t, which should be stored in php_array_result
// supply the PHP class the objects in php_array_objects belong to - can be coded in PHP or cpp
// supply the PHP class the objects in php_array_result belong to - can be coded in PHP or cpp
//
// the PHP object associated to the current cpp class
Php::Value self( this );
// the objects received
Php::Value php_array_objects = params[ 0 ];
// the PHP array to return
Php::Array php_array_result;
// the c++ - class
Curve * obj_curve;
// a single PHP object from the PHP array we received
Php::Value php_obj_in
// a single object from the PHP array we are delivering
Php::Object php_obj_out;
// the key of the associative PHP Array
std::string array_key;
// the data to collect in the associative PHP array
data_t data;
// some other data
int64_t data_returned;
for ( int i = 0; i < php_array_objects.size( ) ; i++ ) {
// retrieve the next object
php_obj_in = php_array_objects[ i ];
// cast PHP object to c++-class
obj_curve = ( Curve * ) php_obj_in.implementation( );
// do something with obj_curve
data = obj_curve->do_something( );
// calculate the key
key = "key_" + std::to_string( i );
// to create an object pass in the class name ( Curve ) to the constructor with optional constructor parameters (1,2,3)
php_obj_out = Php::Object( "Curve", 1, 2, 3 );
// set a class member named "class_member" of php_obj_out
php_obj_out[ "class_member" ] = data;
// call the method "class_method" of php_obj_out
data_returned = php_obj_out.call( "class_method", "parameter" );
// add the new created object to the PHP array
php_array_result[ key ] = php_obj_out;
}
return php_array_result;
} // GetAssociativeArray( )

A function to display contents of 1 or 2 dimensional array of any type

I needed to be able to display the contents of my various arrays (for debugging purposes at this point), and decided to write a function to help me with that. This is what I came up with.
The goal is to be able to display any type of incoming array (int, double, etc).
Because I never had any official programming training, I am wondering if what I have is too "inelegant" and could be improved by doing something obvious to a good computer science person, but not so to a layperson.
int
DisplayArrayInDebugWindow(
void** incoming_array,
char* array_type_str,
int array_last_index_dim_size,
int array_terminator,
HWND handle_to_display_window,
wchar_t* optional_array_name )
{
wchar_t message_bufferw[1000];
message_bufferw[0] = L'\0';
wchar_t temp_buffer[400];
if ( array_last_index_dim_size == 0 ) { array_last_index_dim_size = 1; }
// ----------------------------------------------------------------------------
// Processing for "int" type array
// ----------------------------------------------------------------------------
if ( 0 == (strcmp( array_type_str, "int" )) )
{
int j = 0;
swprintf( temp_buffer, L"%s\r\n", optional_array_name );
wcscat( message_bufferw, temp_buffer );
for ( int i = 0; ((int)(*((int*)( (int)incoming_array + i * (int)sizeof(int) * array_last_index_dim_size + j * (int)sizeof(int))))) != array_terminator; i++ )
{
swprintf( temp_buffer, L"%02i:\t", i );
wcscat( message_bufferw, temp_buffer );
for ( j; j < last_array_dim_size; j++ )
{
swprintf( temp_buffer, L"%i\t", ((int)(*((int*)( (int)incoming_array + i * (int)sizeof(int) * array_last_index_dim_size + j * (int)sizeof(int) )))) ); //
wcscat( message_bufferw, temp_buffer );
}
wcscat( message_bufferw, L"\r\n" );
// --------------------------------------------------------------------
// reset j to 0 each time
// --------------------------------------------------------------------
j = 0;
}
swprintf( temp_buffer, L"\nEnd of Array\n" );
wcscat( message_bufferw, temp_buffer );
SetWindowText( handle_to_display_window, message_bufferw );
}
return 0;
}
NB: When I pass in "incoming array", I type cast it as (void**) obviously.
When the data type changes but the algorithm doesn't, it's time to consider using templates.
template<class Element_Type>
print_array(Element_Type const * p_begin,
Element_Type const * p_end)
{
while (p_begin != p_end)
{
cout << *p_begin;
++p_begin;
}
}
The conversion from single dimension to multiple dimension is left as an exercise to the OP and readers.
Edit 1: Another alternative
At some point, the output function will need information about how to print the information you gave it.
One option is for you to write your own printf function that has format specifiers for the data you send it.
While another option is to pass a pointer to a function that prints the data.
The fundamental issue is that the output function needs to know how to print the data.
For C++, I suggest overriding operator<< in the class / structure. Since the class/structure knows the data, it can easily know how to print the data.

Need to convert cstring to byte* and need to pass it as an i/p argument in c++ functions

In SendCompressedString(),
*passing the compressed data string as an i/p argument.
*Tried printing the whole compressed data in a console window.Can able to print it in the console window....
*Now i need to convert that whole data to Byte * and sizeof that data as an i/p argument to SendBytes()
bool NetOutputBuffer_c::SendCompressedString ( std::string sStr )
{
std::cout.write(sStr.c_str(), sStr.size());
SendInt ( sStr.size() );
return SendBytes ( sStr.c_str(), sStr.size() );
}
In SendBytes(),
Tried to print the pBuf data but can able to print only 5% of data...
what shall i need to do inorder to print the whole data?
bool NetOutputBuffer_c::SendBytes ( const void * pBuf, int iLen )
{
BYTE * pMy = (BYTE*)pBuf;
printf("pMy is %s\n",pMy);
while ( iLen>0 && !m_bError )
{
int iLeft = m_iBufferSize - ( m_pBufferPtr-m_pBuffer );
printf("iLeft is %d\n",iLeft);
if ( iLen<=iLeft )
{
printf("iLen is %d\n",iLen);
memcpy ( m_pBufferPtr, pMy, iLen );
printf("m_pBuffer is %s\n",(char*)m_pBufferPtr);
m_pBufferPtr += iLen;
break;
}
ResizeIf ( iLen );
}
return !m_bError;
}
Requirement::
In SendCompressedString(),
*Need to convert the whole data into Byte * and send it as an i/p argument to SendBytes()
*In SendBytes(),need to print all the data...
*Can someone please help me to achieve this?
Why are you doing the memcpy in a loop? That looks wrong to me. Here is the memcpy reference:
http://www.cplusplus.com/reference/cstring/memcpy/
So you just need to specify a source and destination pointer (address) and what size to copy. So your code should be:
bool NetOutputBuffer_c::SendBytes ( const void * pBuf, int iLen )
{
BYTE * pMy = (BYTE*)pBuf;
memcpy ( m_pBufferPtr, pMy, iLen );
}

Memory Corruption Due to Copy Constructor/Deleting Array?

I have a struct called SFrame which contains many elements, notably 2 elements which are of type unsigned char*. I create a member variable of this struct in my class but I newly initialize it on each iteration in a function in my class (except for when a certain boolean is true). I do this in the following manner:
if (false == m_bRemainderNeedsProcessing)
{
// ... calls before and after the initialization are unimportant and not shown
m_sFrame = SFrame();
}
Then I pass m_sFrame to a function to assign some of its elements and then I need to assign an unsigned char array to my pszMessage variable in my struct.
m_sFrame.iMessageSize = m_sFrame.iPayloadLength;
m_sFrame.iOriginalMessageSize = m_sFrame.iPayloadLength;
m_sFrame.pszMessage = new unsigned char[m_sFrame.iPayloadLength + Constants::RSSL_DECODE_PADDING];
m_sFrame.pszOriginalMessage = new unsigned char[m_sFrame.iPayloadLength + Constants::RSSL_DECODE_PADDING];
These SFrame instances are stored in a vector of SFrames i.e.
std::vector<SFrame>;
I want to be able to reuse m_sFrame for each iteration but I have to ensure that if I am going to clear the contents of the SFrame, that when I store it inside the vector, the SFrame is copied into the vector without losing it's assigned values. For this I create a copy constructor for SFrame:
I have attached an image of part of the SFrame's copy constructor.
At the end of my function, I clear the memory in the pszMessage (and the pszOriginalMessage which is almost the same) by doing the following:
ClearMemory(m_sFrame.pszMessage);
Where the ClearMemory function does the following:
void CPCAPParser::ClearMemory(unsigned char *pszBuffer)
{
if(pszBuffer != NULL)
{
delete [] pszBuffer;
}
}
Thee problem is, this function seems to be deleting more than it should do.... because after many iterations, I get an unhandled exception: Access Violation...
I've attached a few images that might help convey what the problem is. Really need help with this :(, if anyone needs me to add more details let me know.
Thanks
http://imageshack.com/f/pduGDLGZp (Constants::RSSL_DECODE_PADDING has length 7 so there are 13 bytes in total which have been set - made evident at the start of the memory block).
http://imageshack.com/f/exRaaEmip - Where I am calling ClearMemory (the memory address is obviously still the same).
I would post more images but I don't have enough rep...
SFrame:
struct SFrame
{
int* ipTemp_int_ptr;
int* ipTemp_int_ptr_actual;
int* piTimestampPos;
int* piOffset;
int iIP_Header_Length;
int iTCP_Header_Length;
int iTCP_Source_Port;
int iTCP_Dest_Port;
long long uiSequenceNumber;
long long uiInitialSequenceNumber;
long long uiAckNumber;
int iIp_total_length;
int iActual_frame_length;
int iOriginal_frame_length;
int iCaptured_frame_length;
int iTotalPayloadLength;
int iTotalMsgLoad;
int iPayloadLength;
int iBytesComplete;
int iFragmentID;
int iRemainder;
int iMessageSize;
int iOriginalMessageSize;
long long iNextExpectedSequenceNum;
std::string strSourceAddress;
std::string strDestAddress;
std::string strTimestamp;
unsigned char* pszMessage;
unsigned char* pszOriginalMessage;
unsigned int uiClientID;
int iStartOfRemainder;
int iAccumulatedMsgLength;
SFrame() : ipTemp_int_ptr ( NULL ),
ipTemp_int_ptr_actual ( NULL ),
piTimestampPos ( NULL ),
piOffset ( NULL ),
pszMessage ( NULL ),
pszOriginalMessage ( NULL ),
iIP_Header_Length( 0 ),
iTCP_Header_Length ( 0 ),
iTCP_Source_Port ( 0 ),
iTCP_Dest_Port ( 0 ),
iIp_total_length ( 0 ),
iActual_frame_length ( 0 ),
iOriginal_frame_length ( 0 ),
iCaptured_frame_length ( 0 ),
uiSequenceNumber( 0 ),
uiInitialSequenceNumber ( 0 ),
uiAckNumber( 0 ),
iPayloadLength ( 0 ),
iNextExpectedSequenceNum ( 0 ),
uiClientID ( 0 ),
iMessageSize ( 0 ),
iOriginalMessageSize ( 0 ),
iFragmentID( 0 ),
iTotalPayloadLength( 0 ),
iBytesComplete( 0 ),
iAccumulatedMsgLength ( 0 ),
iRemainder ( 0 ),
iStartOfRemainder( 0 ),
iTotalMsgLoad ( 0 )
{
}
SFrame(const SFrame &c_rSrc)
{
*this = c_rSrc;
}
SFrame &SFrame::operator=(const SFrame &c_rSrc)
{
iIP_Header_Length = c_rSrc.iIP_Header_Length;
iTCP_Header_Length = c_rSrc.iTCP_Header_Length;
iTCP_Source_Port = c_rSrc.iTCP_Source_Port;
iTCP_Dest_Port = c_rSrc.iTCP_Dest_Port;
iIp_total_length = c_rSrc.iIp_total_length;
iActual_frame_length = c_rSrc.iActual_frame_length;
iOriginal_frame_length = c_rSrc.iOriginal_frame_length;
iCaptured_frame_length = c_rSrc.iCaptured_frame_length;
iPayloadLength = c_rSrc.iPayloadLength;
uiSequenceNumber = c_rSrc.uiSequenceNumber;
uiInitialSequenceNumber = c_rSrc.uiInitialSequenceNumber;
uiAckNumber = c_rSrc.uiAckNumber;
iNextExpectedSequenceNum = c_rSrc.iNextExpectedSequenceNum;
uiClientID = c_rSrc.uiClientID;
iFragmentID = c_rSrc.iFragmentID;
iMessageSize = c_rSrc.iMessageSize;
iOriginalMessageSize = c_rSrc.iOriginalMessageSize;
iTotalPayloadLength = c_rSrc.iTotalPayloadLength;
iBytesComplete = c_rSrc.iBytesComplete;
iAccumulatedMsgLength = c_rSrc.iAccumulatedMsgLength;
iRemainder = c_rSrc.iRemainder;
iStartOfRemainder = c_rSrc.iStartOfRemainder;
iTotalMsgLoad = c_rSrc.iTotalMsgLoad;
strSourceAddress = c_rSrc.strSourceAddress;
strDestAddress = c_rSrc.strDestAddress;
strTimestamp = c_rSrc.strTimestamp;
pszMessage = (c_rSrc.pszMessage == NULL) ? NULL : new unsigned char[c_rSrc.iMessageSize];
pszOriginalMessage = (c_rSrc.pszOriginalMessage == NULL) ? NULL : new unsigned char[c_rSrc.iOriginalMessageSize];
if(pszMessage != NULL)
{
memcpy(pszMessage, c_rSrc.pszMessage, c_rSrc.iMessageSize);
}
if(pszOriginalMessage != NULL)
{
memcpy(pszOriginalMessage, c_rSrc.pszOriginalMessage, c_rSrc.iOriginalMessageSize);
}
return *this;
}
~SFrame()
{
delete [] pszMessage;
delete [] pszOriginalMessage;
}
};
Your problem is that your SFrame struct is not safely asignable, yet you are placing instances of this in a std::vector that will make copies.
Either:
Add a working user-defined copy constructor and assignment operator to your SFrame struct or
Replace the pointer members with std::vector.
You have many members in your struct now. If you miss just one in your copy constructor, or your handling of allocated memory is faulty, you will have a broken copy. Since a vector<SFrame> will create copies, having broken copies is a no-go with vector<SFrame>.
So instead of this, here is the fix using option 2:
#include <vector>
struct SFrame
{
std::vector<int> ipTemp_int_ptr;
std::vector<int> ipTemp_int_ptr_actual;
std::vector<int> piTimestampPos;
std::vector<int> piOffset;
int iIP_Header_Length;
int iTCP_Header_Length;
int iTCP_Source_Port;
int iTCP_Dest_Port;
long long uiSequenceNumber;
long long uiInitialSequenceNumber;
long long uiAckNumber;
int iIp_total_length;
int iActual_frame_length;
int iOriginal_frame_length;
int iCaptured_frame_length;
int iTotalPayloadLength;
int iTotalMsgLoad;
int iPayloadLength;
int iBytesComplete;
int iFragmentID;
int iRemainder;
int iMessageSize;
int iOriginalMessageSize;
long long iNextExpectedSequenceNum;
std::string strSourceAddress;
std::string strDestAddress;
std::string strTimestamp;
std::vector<unsigned char> pszMessage;
std::vector<unsigned char> pszOriginalMessage;
unsigned int uiClientID;
int iStartOfRemainder;
int iAccumulatedMsgLength;
SFrame() :
iIP_Header_Length( 0 ),
iTCP_Header_Length ( 0 ),
iTCP_Source_Port ( 0 ),
iTCP_Dest_Port ( 0 ),
iIp_total_length ( 0 ),
iActual_frame_length ( 0 ),
iOriginal_frame_length ( 0 ),
iCaptured_frame_length ( 0 ),
uiSequenceNumber( 0 ),
uiInitialSequenceNumber ( 0 ),
uiAckNumber( 0 ),
iPayloadLength ( 0 ),
iNextExpectedSequenceNum ( 0 ),
uiClientID ( 0 ),
iMessageSize ( 0 ),
iOriginalMessageSize ( 0 ),
iFragmentID( 0 ),
iTotalPayloadLength( 0 ),
iBytesComplete( 0 ),
iAccumulatedMsgLength ( 0 ),
iRemainder ( 0 ),
iStartOfRemainder( 0 ),
iTotalMsgLoad ( 0 )
{
}
};
Note that the copy constructor and assignment operator (and destructor) are now gone, thus making the code a lot easier to handle, as well as not having a chance of missing any of the members during the copy. Instead we let the compiler generate the copy, and the compiler will always get every member copied.
Now, your code base that uses the struct has to be recompiled, and you will inevitably get compiler errors. However those errors are usually very easy to fix. Most will probably require you to
Remove the lines with delete [] somepointer; where somePointer is now a vector and
If passing a pointer to the beginning of the buffer, you pass &vector[0] or vector.data(), since a vector is basically a wrapper for new[]/delete[].
Going back to your original code, one issue with your assignment operator is that you failed to delete the previous memory allocated, thus you have a memory leak. Also, you didn't check for self-assignment, given the way you wrote the copy operations. However, this may not have been the only error, as we didn't see how you're using these SFrame instances.
Therefore it may be better to change to vector, fix the compiler errors, rebuild and test your app.

Vector with custom objects: crash when I want to use one received with .at method

I have a class named CConfig, I'm creating new object:
std::vector< CConfig > docs;
CConfig newfile( "somefile.xml", "root" );
printf("%s", newfile.GetTagValue( "servername" )); // this works
docs.push_back( newfile );
When I'm getting this object with .at method
CConfig file = docs.at(0);
printf("%s", file.GetTagValue( "servername" )); // this crashes
Where's the problem?
(im sorry if formatting is wrong but currently I don't use javascript because my bandwidth is ended and max speed is 1kb/s so I'll try to fix it later)
CConfig.h:
class CConfig
{
TiXmlDocument m_doc;
TiXmlElement* m_pRoot;
bool m_bIsLoaded;
public:
CConfig ( void ) {};
CConfig ( const char * pszFileName, const char * pszRootName );
~CConfig ( void ) {};
const char* GetTagValue ( const char * pszTagName );
const char* GetTagAttribute ( const char * pszTagName, const char * pszAttributeName );
TiXmlElement* GetRootElement ( void ) { return m_pRoot; };
bool IsAvailable ( void ) { return m_bIsLoaded; };
};
CConfig.cpp
#include "CConfig.h"
CConfig::CConfig( const char * pszFileName, const char * pszRootName )
{
m_bIsLoaded = m_doc.LoadFile( pszFileName );
if( m_bIsLoaded )
m_pRoot = m_doc.FirstChildElement( pszRootName );
}
const char * CConfig::GetTagValue( const char * pszTagName )
{
if( m_bIsLoaded && m_pRoot )
{
TiXmlElement * element = m_pRoot->FirstChildElement( pszTagName );
if( element )
return element->GetText();
}
}
const char * CConfig::GetTagAttribute( const char * pszTagName, const char * pszAttributeName )
{
if( m_bIsLoaded && m_pRoot )
{
TiXmlElement * element = m_pRoot->FirstChildElement( pszTagName );
if( element )
return element->Attribute( pszAttributeName );
}
}
I'm using tinyxml
Your issue is with pointers to old memory. When you add an item to an array, it is copied. Later you leave that scope and the original is destroyed, but ask yourself where the pointer in your copy is pointing? Still to the first (now deleted) object's memory. Uh-oh.
The simplest fix (while avoiding large copy operations) is to make m_doc into a shared pointer (available in the standard in C++11, or via Boost in C++03). That will then handle everything for you rule-of-3 wise. And because the underlying memory won't move, m_pRoot will remain valid until the last copy has been deleted.
If copy-space is not an issue, then fix your Rule of Three violation by properly adding a copy constructor:
CConfig(const CConfig& obj)
: m_doc(obj.m_doc)
, m_bLoaded(obj.m_bLoaded)
, m_pRoot()
{
if (m_bLoaded)
m_pRoot = m_doc.GetRootElement();
}
An assignment operator is also likely in order, but if you don't need it, hide it by declaring it (but not implementing it) as private or use the C++11 delete attribute feature.
Interestingly enough, you don't even need the m_bLoaded member. A non-NULL root pointer can indicate your loaded-state, but that is a separate issue. This at least be enough to get you up and running.