dynamic memory allocation in functions
Jan 22, 2014 at 10:14am UTC
I had function call
hci_host_data_pkt_gen (0x03, &data_ll_hci_rx[2], &hci_host_data_pkt_arr[2])
But execution stops after printing of "Mem locations"
Please tell me the mistake?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
unsigned char * hci_host_data_pkt_arr ;
unsigned char * data_ll_hci_rx;
data_ll_hci_rx= new (nothrow) unsigned char [10];
unsigned char * hci_host_data_pkt_gen (int con_handle_lsb, unsigned char * source_location, unsigned char * dest_location)
{
hci_host_data_pkt_arr = new (nothrow) unsigned char [10];
hci_host_data_pkt_arr[0] = 0x02;
hci_host_data_pkt_arr[1] = con_handle_lsb;
cpy_ll_hci_pyld_hci_host_pyld (source_location, dest_location, 8);
//!info
return hci_host_data_pkt_arr;
}
void cpy_ll_hci_pyld_hci_host_pyld (unsigned char * source_location, unsigned char * dest_location, int no_mem_locations)
{
int i;
for (i=0;i<no_mem_locations;i++)
{
cout<<"@" <<sc_time_stamp()<<"\tMem locations------------" ;
dest_location[i] = source_location[i];
[b]/*---------------Problem is with above statement, execution stops after printing Mem Locations-------* / [/b]
cout<<"@"<<sc_time_stamp()<<"\tCHECK EVENTS for data packets--";
}
} */
Last edited on Jan 22, 2014 at 10:18am UTC
Jan 22, 2014 at 11:02am UTC
I would omit that nothrow
. If you can't alloc the required memory your program is doomed anyway so it's ok end the program.
The issue is: Does dest_location
and/or source_location
point to something valid (i.e. at least 8 unsigned char)?
Jan 22, 2014 at 11:22am UTC
I checked for dest_location and source_location
Actually the problem is with dest_location. Source_location is working fine.
I replaced dest_location with unsigned char* a (locally declared in cpy_ll_hci_pyld_hci_host_pyld). Then it works well...it seems somewhere problem is with hci_host_data_pkt_arr (dest_location )
But I am not able to figure out
Topic archived. No new replies allowed.