Purpose of new?

Started by
7 comments, last by Tac-Tics 22 years, 2 months ago
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."
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
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.
ncsu121978:
When did Mr. T say that?

Edited by - TapeWorm on February 3, 2002 12:03:32 AM
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."
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."
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
My sig used to be, "God was my co-pilot but we crashed in the mountains and I had to eat him..."
But folks whinned and I had to change it.
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];}  

  const int SetConstLength(int i){	return i;}void main(void){	//allowedconst int arrayLength = SetConstLength(10);int *myArray = new int[arrayLength];	//not allowedint myArray[arrayLength]; //compiler wants to set at design time and it cant	//allowedconst in arrayLength = 20;int myArray[arrayLength];}  

This topic is closed to new replies.

Advertisement