Join us at IRC!
I'd prefer to die standing, than to live on my knees - Che Guevara
Wednesday, May 23, 2012
Navigation
Members Online
Total Online: 42
Web Spiders: 21
Guests Online: 39
Members Online: 3

Registered Members: 70170
Newest Member: bahmx
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Can someone explain the difference here? (Dynamic Memory)

ETep
Member



Posts: 6
Location:
Joined: 26.05.10
Rank:
Wiseman
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];


756E6F726967696E616C




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

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

fleandr
Member

Posts: 17
Location:
Joined: 22.04.10
Rank:
HBH Guru
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 fleandr on 31-05-10 06:36
Author

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

ETep
Member



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


756E6F726967696E616C


Author

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

COM
Banned



Posts: 800
Location:
Joined: 31.08.07
Rank:
God
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.


K'aem'nhi kh'rn, K'aem'nhi kh'r, K'aem'nhi kh'rmnu.
I'a Y'gs-Othoth!
Guest
Username

Password

Remember Me


Bookmark This Page
Affiliates
Adverts

 

 

Links
By using, viewing or obtaining any information contained on this site, you agree to the disclaimer.

© HellBound Hackers 2008- 2009. Since 3rd December 2004.