dynamic array/pointer


1
2
3
4
  unsigned int* hci_buffer;                      
  unsigned int hci_buff_size;                    
  hci_buff_size = HCI_DAT_PKT_LEN * HCI_NO_PKTS; //!used macros
  hci_buffer = new(nothrow) unsigned int[hci_buff_size];          


Errors:
ISO C++ forbids declaration of ‘hci_buff_size’ with no type
line no 3: error: ISO C++ forbids initialization of member ‘hci_buff_size’
line no 3: error: making ‘hci_buff_size’ static
line no 3: error: ISO C++ forbids in-class initialization of non-const static member ‘hci_buff_size’
line no 3: error: declaration of ‘int hci_top::hci_buff_size’
line no 3: error: conflicts with previous declaration ‘unsigned int hci_top::hci_buff_size’

Last edited on
What code comes before this code? Where is the code located? You have not provided enough information.
Its solved, But i have one more question:

unsigned int* hci_buffer;
hci_buffer = new(nothrow) unsigned int[hci_buff_size];

I want to initialize all locations of hci_buffer to zero. How it would be possible instead of initializing each location to zero??
Last edited on
Just add empty curly braces between the ending square bracket and the semi-colon:
http://ideone.com/4V4oJQ
means if I write like

hci_buffer = {};

then hci_buffer would be initialized to zero;
No.
then please tell me syntax to initialize hci_buffer
L B wrote:
Just add empty curly braces between the ending square bracket and the semi-colon:
http://ideone.com/4V4oJQ http://ideone.com/4V4oJQ http://ideone.com/4V4oJQ
Last edited on
empty pair of parentheses works too (and is pre-C++11, unlike the curlies)

unsigned int* hci_buffer = new unsigned int[hci_buff_size]();
hello EssGeEich,

Memory is destructed in the link in which you referred.

I don't want to destruct, just want to initialise with zero(like reset value).

Please read the code carefully.
Besides the fact LB posted the link (which you really didn't notice??), you should check out the 5th line.

There's even a comment - did you see it?
int *p = new int[10]{}; //notice the {}

delete[] p;

it means syntax in my case would b

unsigned int *hci_buffer = new int[10]{}; //Means need to add {} at the time of constructing ?

delete[] hci_buffer; //at time when need to initialize to zero?



1
2
unsigned int *hci_buffer = new int[10]{};
//Means need to add {} at the time of constructing ? 

^ Yes.
The {} initializes all the elements to 0.

The delete[] goes when you finished using hci_buffer.
Last edited on
Topic archived. No new replies allowed.