English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

What does this line do?

int* addr = new int[num];

It is making an array with a pointer? Or something? I've been studying for a final and doing my last programs in a class all day and the code is starting to blend together...

2007-07-25 10:26:27 · 2 answers · asked by Zamaza 3 in Computers & Internet Programming & Design

2 answers

addr is a pointer to an array of num ints

This lets your program dynamically allocate the memory for the array, instead of having a fixed amount of memory by defining int a[1000], for example. You can't say int a[num]; it's not valid.

You use this when you'll be determining the size of the array programmically. It's then also common to see delete a[ ] later on to free the memory.

Check out this site:
http://www.fredosaurus.com/notes-cpp/newdelete/50dynamalloc.html

2007-07-25 10:35:35 · answer #1 · answered by conehead 6 · 2 0

It creates a new array of num integers, and sets addr to the address if the first element of the array.

2007-07-25 10:33:20 · answer #2 · answered by McFate 7 · 0 0

fedest.com, questions and answers