assign memory dinamically


I’m trying to create a class where I need to assign memory dinamically. However the program stops suddenly when it reaches malloc.

I’m almost sure that malloc is corrupting random data. Can somebody tell me how to do it correctly?

class bi_packet{
  bi_packet(int nod_id_len_in, int crrcmd_len_in, int counter_len_in, int num_routines_in);

  int nod_id_len;
  int crrcmd_len;
  int counter_len;
  int packet_len;
  int num_routines;
  uint8_t* packet;
  uint8_t* nod_id;
  uint8_t* crrcmd;
  uint8_t* crrcmd_id;
  uint8_t* cmd_id;
  uint8_t* counter;

bi_packet::bi_packet(int nod_id_len_in, int crrcmd_len_in, int counter_len_in, int num_routines_in){

  nod_id_len   =  nod_id_len_in;
  crrcmd_len   =  crrcmd_len_in;
  counter_len  =  counter_len_in;
  packet_len   =  nod_id_len + crrcmd_len + 1 + counter_len;
  num_routines =  num_routines_in;

  packet  = (uint8_t *) malloc (  (packet_len) *  sizeof(uint8_t));  // *** HERE STOPS RUNNING  ***

  nod_id    =  packet;
  crrcmd    =  packet + nod_id_len;
  crrcmd_id =  packet + nod_id_len;
  cmd_id    =  packet + nod_id_len + crrcmd_len;
  counter   =  packet + nod_id_len + crrcmd_len + 1;




How much space is that function trying to allocate?

its trying to allocate 4+5+1+2 = 12 uint8_t.

I declare two objects of this class in my program. The first one is a "global variable" and the second one is declared during runtime. Until now I've haven't any troubles with the global one. The program stops only when the second object is declared in runtime. (until now)

How can I know where is malloc trying to allocate the object and if that space is free?

I'm a little bit confused. any help is really welcome.


Showing your -complete- code base (or at least a simplified version that displays the same issue), instantiating and using the object/class, etc - may also help...