How to properly do dynamic memory allocation (C++) -


i trying create dynamically allocated array , print it's contents in c++. although code producing incredibly strange output when print.

int main() {     int* arr;      arr = new int [1200];     memset(arr, 5, 1200);      (int = 0; < 1200; i++)         printf("%d ", arr[i]);      std::cout << '\n';     return 0; } 

mixing cout , printf because playing around code. proper includes in file.\

here output:

84215045 84215045 84215045 84215045 84215045 84215045 84215045 84215045 84215045 84215045 84215045 84215045 1285 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0  

i have no idea how produced.

edit:

thank answers. understand code , why output looks way look.great answers.

memset(arr, 5, 1200); 

if scribble whole bunch of 5'a on piece of paper , try read paper, you'll things "5555". 84215048 in hex 0x05050505.

don't manipulate arrays of data if raw chunks of memory unless know you're doing.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -