Jump to content

array inside class


amitk3553

Recommended Posts

Can I declare and allocate memory to an array in class as given below:

 
class hci_test
{
public:
unsigned char* host_hci_pkt_arr;

host_hci_pkt_arr = new(nothrow) unsigned char [2];
host_hci_pkt_arr[0] = 0x01;
host_hci_pkt_arr[1] = 0x02;     
};

While I am compiling above following errors are generated:

std::nothrow’ cannot appear in a constant-expression
hci_test.cpp:75: error: ‘new’ cannot appear in a constant-expression
hci_test.cpp:75: error: ISO C++ forbids declaration of ‘host_hci_pkt_arr’ with no type
hci_test.cpp:75: error: ISO C++ forbids initialization of member ‘host_hci_pkt_arr’
hci_test.cpp:75: error: making ‘host_hci_pkt_arr’ static
hci_test.cpp:75: error: ISO C++ forbids in-class initialization of non-const static member ‘host_hci_pkt_arr’
hci_test.cpp:75: error: declaration of ‘int hci_test::host_hci_pkt_arr’
hci_test.cpp:69: error: conflicts with previous declaration ‘unsigned char* hci_test::host_hci_pkt_arr’


Please tell me the solution.

Link to post
Share on other sites

That's just C++ - you need to allocate the memory in the constructor, and de-allocate it in the destructor,

 

regards

Alan

 

P.S. Another way I find is useful to help remember things like this is that sequential code in C/C++ really belongs in a function. So writing this:

host_hci_pkt_arr = new(nothrow) unsigned char [2];
host_hci_pkt_arr[0] = 0x01;
host_hci_pkt_arr[1] = 0x02;   

in a class doesn't make sense, it belongs in a function (in this particular case the class constructor).

Link to post
Share on other sites

Can I declare and allocate memory to an array in class as given below:

 
class hci_test

{

public:

unsigned char* host_hci_pkt_arr;

host_hci_pkt_arr = new(nothrow) unsigned char [2];

host_hci_pkt_arr[0] = 0x01;

host_hci_pkt_arr[1] = 0x02;     

};

While I am compiling above following errors are generated:

std::nothrow’ cannot appear in a constant-expression

hci_test.cpp:75: error: ‘new’ cannot appear in a constant-expression

hci_test.cpp:75: error: ISO C++ forbids declaration of ‘host_hci_pkt_arr’ with no type

hci_test.cpp:75: error: ISO C++ forbids initialization of member ‘host_hci_pkt_arr’

hci_test.cpp:75: error: making ‘host_hci_pkt_arr’ static

hci_test.cpp:75: error: ISO C++ forbids in-class initialization of non-const static member ‘host_hci_pkt_arr’

hci_test.cpp:75: error: declaration of ‘int hci_test::host_hci_pkt_arr’

hci_test.cpp:69: error: conflicts with previous declaration ‘unsigned char* hci_test::host_hci_pkt_arr’

Please tell me the solution.

Hello,

Please note that SystemC is just a C++ library. So, there is no reason why an array

cannot be declared, initialized and used inside a class. But there are some problems:

1. unsigned char* host_hci_pkt_arr == this is just a pointer to a single unsigned char

NOT an array of pointers to unsigned chars. Why not :

unsigned char* host_hci_pkt_arr[<SOME_SIZE>];

This is declaring array of pointers to an array of SOME_SIZE, explicitly. You could then

fill the elements in as : host_hci_pkt_arr[0] = "0x01";

2. The moment you use the "new" operator, you have to use the corresponding "delete"

to clear memory in the destructor. On the other hand, by explicitly declaring the array

as above, the compiler and runtime system takes care of memory allocation/deallocation.

It appears that you are trying to model packets, and realistically a packet consists of a

large number of bytes. If there are a large number of packets moving around, that are

created dynamically, and then not deleted when the container object is deleted, one is

looking at annoying memory leak issues.

HTH.

Link to post
Share on other sites

Adding an option for you. You might want to use it like as follows:

 

 

 
class hci_test {
public:
   unsigned char* host_hci_pkt_arr = new unsigned char [2];
   
    hci_test(){
       host_hci_pkt_arr[0] = 0x01;
       host_hci_pkt_arr[1] = 0x02;     
    }


    ~hci_test(){
       delete [] host_hci_pkt_arr ;
    }


};

Enable c++11 option in your compiler if you want to have it!

 

Regards, Sumit

Link to post
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...