Follow us on Twitter!
The important thing is not to stop questioning. - Albert Einstein
Thursday, April 24, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 23
Guests Online: 22
Members Online: 1

Registered Members: 82893
Newest Member: mor-amit
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Can someone explain the difference here? (Dynamic Memory)


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 31-05-10 02:02
I realize this is a very noobish question but can someone explain to me the difference between these two snippets of code?

The way I understand it, CODE 2 should not compile. However, under Dev C++, it compiles and runs fine....which makes me wonder why would you use CODE 1.

***************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];




Edited by on 31-05-10 03:45
Author

RE: Can someone explain the difference here? (Dynamic Memory)


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 31-05-10 06:36
from my noobish point of view both pieces of code are fine. In c++ there is a dynamic memory allocation so np with that. In general you should use code 1. Code 2 is ok till you use value types when you use objects then you need code 1 and explicit constructor calls.
In fact I may be wrong, please correct or delete my post if so.

Edited by on 31-05-10 06:36
Author

RE: Can someone explain the difference here? (Dynamic Memory)


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 31-05-10 07:25
Ok. That clears a bit up then. Thanks


Author

RE: Can someone explain the difference here? (Dynamic Memory)


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 31-05-10 10:46
Code 2 should according to old standards indeed not compile. However, certain compilers do offer that type of behaviour, though it shouldn't be allowed. If I recall correctly though, newer standards allow for it or they are at least planning on changing to allow code 2 as well. But, it'll basically result in the same thing as code 1, or at least should.