小编M.S*_*SEL的帖子

C++ how to add an element to a pointer array by exceeding size

assume that we created dynamically allocated memory such as:

int SIZE = 10;
int *p = new int[SIZE];
for(int i = 0; i < SIZE; ++i)
  p[i] = i;
Run Code Online (Sandbox Code Playgroud)

it will assing 0 to 9 to our pointer array.
Then i wanted to add 10,11,12 to the array
can i do :

p[10] = 10;
p[11] = 11;
p[12] = 12;
Run Code Online (Sandbox Code Playgroud)

or should i do:

delete[] p;
size = 13;
p = new int[SIZE];
for(int i = 0; i < …
Run Code Online (Sandbox Code Playgroud)

c++ arrays pointers memory-management vector

7
推荐指数
1
解决办法
132
查看次数

标签 统计

arrays ×1

c++ ×1

memory-management ×1

pointers ×1

vector ×1