
Originally Posted by
Guest
Yes!
Yes, message gets created on line #4 and dies with the function call. You simply cannot refer to it that way. Copies or pointers (C++ has additional pointer types to play with) to heap-allocated data are the only way to return stuff.
Okay guys thank you for your suggestions, basically when I think about your suggestions, that is how I would handle it in C, giving pointer as parameter and returning a value.
In the method decode I allocate new message object, but I never delete it, my system works properly but I simply do not understand why is it working. Why no heap corruption has happened, since I never freed memory that I allocated in the decode method?
Code:
Message* MessageDecoder::decode(uint8_t* deserializedBuffer, uint8_t deserializedBufferLen)
{
Message* message;
.
.
.
message = new Message( dataType, dataLength, data);
.
.
.
return message;
}
Code:
int8_t MessageProcessor::processReceivedData(Message* message)
{
.
.
.
// Returning pointer to a Message object
message = this->messageDecoder->decode( receieveBuffer, receieveBufferLen );
return MESSAGE_PROCESSOR_OK;
.
.
.
}
Code:
int main(void)
{
Message message(0, 0, NULL);
int8_t retVal;
.
.
.
while (true)
{
retVal = messageProcessor.processReceivedData(&message);
if ( retVal == MESSAGE_PROCESSOR_OK )
{
pcSerialPort.writeString("JEDAN\n\r");
}
.
.
.
}
}