creating an array with "new" creates infinite array?

so I did just notice that the code below works just fine, why? I want to create an array with only 2 elements, not infinite

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main()
{
	int *a;
	a = new int[2];
	a[5000] = 2;
	cout << a[5000];
	cin.get();
	return 0;
}
Last edited on
As for why there is no bounds checking, there are a couple aspects to the answer:

An array is a leftover from C. C arrays are about as primitive as you can get. Just a sequence of elements with contiguous addresses. There is no bounds checking because it is simply exposing raw memory. Implementing a robust bounds-checking mechanism would have been almost impossible in C.
In C++, bounds-checking is possible on class types. But an array is still the plain old C-compatible one. it is not a class. Further, C++ is also built on another rule which makes bounds-checking non-ideal. The C++ guiding principle is "you don't pay for what you don't use". If your code is correct, you don't need bounds-checking, and you shouldn't be forced to pay for the overhead of runtime bounds-checking.
So C++ offers the std::vector class template, which allows both. operator[] is designed to be efficient. The language standard does not require that it performs bounds checking (although it does not forbid it either). A vector also has the at() member function which is guaranteed to perform bounds-checking. So in C++, you get the best of both worlds if you use a vector. You get array-like performance without bounds-checking, and you get the ability to use bounds-checked access when you want it.

http://stackoverflow.com/questions/1239938/c-accesses-an-array-out-of-bounds-gives-no-error-why
In your case, using any index greater than 1 leads to Undefined Behavior (UB).
This means that 2, 3, 4, 10, up to 5000 and over, are all bad indexes.

So in the end, it just appears to be working, but it can crash randomly.

You also forget to delete[] a; at the end, so you're leaking memory.
Last edited on
thanks, well, now just the only thing that's left is to learn vectors :D

edit: @Catfish666, I dont need to do delete[] a, since it gets deleted automatically after I close the app, or wont it?
Last edited on
I dont need to do delete[] a, since it gets deleted automatically after I close the app, or wont it?

Well yes, when the program finishes executing, the operating system will reclaim the memory it gave the program to use.

That said, C++ is not a garbage collected language (such as Java for example).
This means that you are responsible for deallocating all the memory you allocated, otherwise your program will leak memory.

Leaking memory means that, while the program runs, it may potentially keep allocating more memory than it needs, until the memory runs out.

Wikipedia has a nice example in the corresponding article:
http://en.wikipedia.org/wiki/Memory_leak
Last edited on
yes, I know about this, but since this is just a pure console testing I dont really need to do that, since OS will free that memory after I close the app
In C++, not freeing memory is a dangerous habit to get into (well, until you learn about smart pointers), and other C++ programmers may think you are ignorant of the fact that C++ isn't garbage collected.

So expect to get a lot of "hey you forgot to deallocate the memory" observations if you stick to your guns like this.
well, I'm learning, so I dont really care much, ofcourse, I'd free memory in an app where I wouldnt really want to use the pointer anymore, but it's totally useless for a simple console application that will run for 5 seconds since im just testing something
It's a good habit to always match new with delete. For example, if the code was inside another function rather than in main(), each time the function is called it will allocate a fresh block of memory. Do that without delete and you have a serious memory leak. One way to reduce the likelihood of that happening is to start out with good coding habits and stick to them as a matter of routine.
wait, but when I'll do a delete at the end of the function, then I wont be able to use the pointer in main, righT?
when I'll do a delete at the end of the function, then I wont be able to use the pointer in main

Correct.
so if I do "delete", it will COMPLETELY remove the pointer, or clears the thing it points to? e.g.

1
2
3
4
5
6
7
//global initialization 
int *a;
a = new int;
// do stuff
delete a;

char a;


will this be possible?
No, you cannot reuse the name of the pointer in the same scope.
And remember, a pointer is just a variable that holds a memory address.

http://www.cplusplus.com/reference/new/

The operator new allocates memory, then return the address of that memory (which you store in a pointer).

The operator delete deallocates the memory located at the address stored in the pointer.
oh, I got it now, thanks
Last edited on
Topic archived. No new replies allowed.