Dynamic Memory Question

Looking at this example code, can someone tell me what the difference is if we eliminated the pointer *p and after the line "cin>>i" simply replaced "p=new..." with "int p[i]"? Both produce the same output? Thanks for the help!

// rememb-o-matic
#include <iostream>
#include <new>
using namespace std;

int main ()
{
int i,n;
int * p;
cout << "How many numbers would you like to type? ";
cin >> i;
p= new (nothrow) int[i];
if (p == 0)
cout << "Error: memory could not be allocated";
else
{
for (n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n=0; n<i; n++)
cout << p[n] << ", ";
delete[] p;
}
return 0;
}
Some code requires a constant between the brackets. Some other code can take a variable as well. Disregard this.

-Albatross
Last edited on
Thank you for the reply. However, I'm still a bit confused. You say int p[] requires a constant? So what would be the difference between these two snippets of code:
Thanks again for the help!

**************CODE 1***************
#include <iostream>
#include <new>
using namespace std;

int main ()
{
int i,n;
int * p;
cout << "How many numbers would you like to type? ";
cin >> i;
p= new (nothrow) int[i];

***************CODE 2***************
#include <iostream>
#include <new>
using namespace std;

int main ()
{
int i,n;
cout << "How many numbers would you like to type? ";
cin >> i;
int p[i];
Okay... actually... disregard what I said earlier. I should probably sleep a bit before I try to answer any more questions.

-Albatross
The number of elements in a named array must be known at compile time. Therefore, it must be a constant value (either a const int or an integer literal).
But both of those code snippets work and produce the same output. I'm just wondering why choose one over the other I guess?

Thanks for the help.
This will not compile:

1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
	int i;
	std::cin >> i;
	int a[i];

	return 0;
}

Error messages (from VC++ Express):

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'a' : unknown size


If you're interested in arrays, read this: http://www.cplusplus.com/doc/tutorial/arrays/
It compiles for me? I am using Dev C++ and that code compiles and runs with no errors. I even checked to be sure the array was created.

That's odd. And not standard-compliant, as far as I'm aware.
Ok, well that still kind of helps. So normally, and to be safe, you should use dynamic memory allocation when you're trying to define something like an array after the program has begun execution?

Also, I am following the tutorial. The CODE 1 snippet is from the last program entitled "rememb-o-matic", in the tutorial on Dynamic Memory. However, I replaced the CODE 1 snippet with the CODE 2 snippet and the program still ran and produced the same results.

I'd still like to know why Dev is letting it compile and run.

Thanks for the help though. That actually cleared up a lot of my confusion.
I'd still like to know why Dev is letting it compile and run.

Because g++ (which is the compiler that comes with it) allows this as an extension.
It won't work with some other compilers and is not part of the C++ standard. However, it is part of the C standard, which is probably why g++ supports it too.
Topic archived. No new replies allowed.