safe structures embedded systems

Posted by user405633 on Stack Overflow See other posts from Stack Overflow or by user405633
Published on 2011-03-10T20:46:14Z Indexed on 2011/03/11 8:10 UTC
Read the original article Hit count: 152

Filed under:
|
|
|

I have a packet from a server which is parsed in an embedded system. I need to parse it in a very efficient way, avoiding memory issues, like overlapping, corrupting my memory and others variables.

The packet has this structure "String A:String B:String C".

As example, here the packet received is compounded of three parts separated using a separator ":", all these parts must be accesibles from an structure.

Which is the most efficient and safe way to do this.

A.- Creating an structure with attributes (partA, PartB PartC) sized with a criteria based on avoid exceed this sized from the source of the packet, and attaching also an index with the length of each part in a way to avoid extracting garbage, this part length indicator could be less or equal to 300 (ie: part B).

typedef struct parsedPacket_struct {
  char partA[2];int len_partA;
  char partB[300];int len_partB;
  char partC[2];int len_partC;
}parsedPacket;

The problem here is that I am wasting memory, because each structure should copy the packet content to each the structure, is there a way to only save the base address of each part and still using the len_partX.

© Stack Overflow or respective owner

Related posts about c

    Related posts about struct