Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Tac-Tics

Purpose of new?

This topic is 5954 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I currently know more about Java than C++ now. I''m confused with how the keywork "new" works in C++. In Java, you always use "new" to create a new instance of an object, but in C++ I understand that 1) it creates a pointer of a new instance and 2) the instance is created out of "dynamic memory" so it''s size can be determined at run time.... My teacher explained to me that means even though I can''t say something like: cin >> x; int list[x]; I *can* say: cin >> x; int list[] = new int[x]; However, I swear I made a program once that was something like: const int MAX = 30; int list[MAX]; and it worked.... If my memory is indeed *not* crazy, my question is, how is "new" useful in C++ if you can get around it? "I''''m not evil... I just use the power of good in evil ways."

Share this post


Link to post
Share on other sites
Advertisement
you can do the second way because x is a const so the compiler knows at compile time how much memory it will need for the array

"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
you wont always know what the size of the array will be, so you do something like this:

int *list;

cin >> x;

list = new int[x];

now you have an array that has x elements. you can''t have put memory aside for an variable sized array, so you have to use a pointer and dynamically allocate it. in your example, the compiler knows MAX will always be 30, so there isn''t a problem.

Share this post


Link to post
Share on other sites
I thought that might be the case, but I was not sure the compiler had forsight enough to recognise a constant as a fixed variable (oxymoron anyway?)



"I''''m not evil... I just use the power of good in evil ways."

Share this post


Link to post
Share on other sites
Ok, I just wrote a test program to prove that to myself. Again much thanks. I learned an emense amount about pointers today in a very short amount of time =-) Arigatou Gozaimasu

"I''''m not evil... I just use the power of good in evil ways."

Share this post


Link to post
Share on other sites
Actually, most optimizing compilers will treat a CONST as a string replacement after doing type checking. They just drop the type and value straight into the symbol table and don''t even allocate any memory for them.

Landsknecht

Share this post


Link to post
Share on other sites
new is required for polymorphism.

class CBASE{};
class CDERIVED: CBASE{};

CBASE *pBase = new CDERIVED;


new and delete are also DYNAMIC sorta things, meaning you can do it on the fly.

  
int* pi = new int[startingAmount];
while(..)
{
..
//somewhere funny

delete []pi;
pi = new int[newLength];

}

Share this post


Link to post
Share on other sites
  
const int SetConstLength(int i)
{
return i;
}

void main(void)
{
//allowed

const int arrayLength = SetConstLength(10);
int *myArray = new int[arrayLength];
//not allowed

int myArray[arrayLength]; //compiler wants to set at design time and it cant

//allowed

const in arrayLength = 20;
int myArray[arrayLength];
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!