amitk3553 Posted November 8, 2013 Report Share Posted November 8, 2013 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-expressionhci_test.cpp:75: error: ‘new’ cannot appear in a constant-expressionhci_test.cpp:75: error: ISO C++ forbids declaration of ‘host_hci_pkt_arr’ with no typehci_test.cpp:75: error: ISO C++ forbids initialization of member ‘host_hci_pkt_arr’hci_test.cpp:75: error: making ‘host_hci_pkt_arr’ statichci_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. Quote Link to comment Share on other sites More sharing options...
apfitch Posted November 8, 2013 Report Share Posted November 8, 2013 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). amitk3553 1 Quote Link to comment Share on other sites More sharing options...
dakupoto Posted November 10, 2013 Report Share Posted November 10, 2013 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. karandeep963 and amitk3553 2 Quote Link to comment Share on other sites More sharing options...
sumit_tuwien Posted November 10, 2013 Report Share Posted November 10, 2013 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 karandeep963 and amitk3553 2 Quote Link to comment Share on other sites More sharing options...
amitk3553 Posted November 12, 2013 Author Report Share Posted November 12, 2013 Thanks sumit, Please let me know how to enable c++11 option in compiler. Regards cam Quote Link to comment Share on other sites More sharing options...
sumit_tuwien Posted November 12, 2013 Report Share Posted November 12, 2013 in gcc -std=c++11 option will enable it amitk3553 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.