-2

I come across a code as follow:

int* list = new int[5];

I never saw such declaration. What does it do? I suppose list is a pointer to an int? Using VS debug environment, I can see list having one int value. So how are the other element accessed? Thanks.

iBug
  • 30,581
  • 7
  • 64
  • 105
CaTx
  • 1,121
  • 4
  • 17
  • 36
  • It simply dynamically allocates a buffer with 5 `int` and assign it's address to the pointer named `list`. This is not what a linked list is. Basically this is how you `malloc()` in C++. – Havenard Jan 24 '18 at 03:08
  • So how do I access each of these 5 elements? What is style of this declaration called? – CaTx Jan 24 '18 at 03:18
  • 4
    It is called a "mistake". Replace it with `std::vector list(5);`, and get rid of the (hopefully) matching `delete [] list;`. – Jerry Coffin Jan 24 '18 at 03:38
  • @CaTx You just access like an array, `list[0]` through `list[4]`. – Havenard Jan 24 '18 at 04:02
  • Thanks Havenard. @Jerry Coffin, the assignment forces me to do it that way. It comes from the professor. – CaTx Jan 24 '18 at 04:10
  • Possible duplicate of [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) –  Jan 24 '18 at 04:29

2 Answers2

1

The statement is called badly-mannered dynamic (memory) allocation, involving two operators, new (new[]) and delete (delete[]). The new operator allocates memory and the delete operator deallocates memory.

Since it's a chunk of memory assigned to a pointer, it behaves mostly the same as an array (int list[5]), so you should access it as usually as you do with a pointer. Like

int *list = new int[5];
list[0] = 10; list[3] = 25; // Whatever
for (int i = 0; i < 5; i++)
    list[i] = 1+1;

You can see a list of topics about dynamic allocation in C++ on Stack Overflow here.

iBug
  • 30,581
  • 7
  • 64
  • 105
  • This style comes from the assignment. Not sure if the professor deserves the comment. – CaTx Jan 24 '18 at 04:11
  • 1
    @CaTx the assignment is a symptom of a common problem: Students graduate thinking they have learned to write C++ code and have actually been taught C programming. So while it's good to see they are teaching `new` and not `malloc`, it's not much of an improvement. Now knowing how to manually control your memory at a low level is an important skill, but the way it is being taught hypes up it's importance in day-to-day programming and the smarter alternatives, along with important concepts that do not exist in C, are put off or outright ignored. – user4581301 Jan 24 '18 at 05:01
  • 1
    Side note: If you are going to be messing with dynamic allocation, do yourself a favour and familiarize yourself with the [Rules of Three and Five](http://en.cppreference.com/w/cpp/language/rule_of_three). Then take a look at the Rule of Zero and think about how much easier it makes your job. – user4581301 Jan 24 '18 at 05:06
0

This is not a linked list, but a construct known as dynamic memory. If you want to create an array normally, you would write something like

int myArray[3] = {
    1, 2, 3
};

Notice that when I write the size of myArray (3), I use a constant expression; If I were to write

int x = 3;
int myArray[x] = {
    1, 2, 3
};

It wouldn’t work, because x is a variable, not a constant expression. Note that even if x is a const, this still wouldn’t work. This is because before the program is run, memory needs to be allocated for the arrays.

When you say int * myData = new int[3], you can create memory on the fly, without constant expressions. In this case, it would be perfectly fine to do this

int x = 3;
int * myData = new int[x];

However, myData is a pointer, not an array, so the common sizeof(x) / sizeof(*x) trick doesn’t work here.

Make sure that after your dynamic memory is no longer of use, you write delete[] myData. This de-allocates the memory block and allows other memory to be stored in its place.

This memory is a block of memory just like an array. A linked list, however, is totally different, and would take a while to explain. Check out the Wikipedia on it.

paper man
  • 416
  • 4
  • 14