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 comment
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 comment
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 comment
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 comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...