![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Feb 2005
Posts: 89
Rep Power: 4
![]() |
malloc and new
i was wondering how you all feel about using malloc in C++ coding.
let's say i ask the user how many people there are in his neighborhood, and input int numberofNeighbors, for use with some neighbor class. then i can malloc an array of pointers, and each time i make a new Neighbor, i can put the addy into the malloc-ed array, and re-get it without fiddling with a linked list. anyone have a better way of doing this. that is to say a way that doesn't waste tons of memory, or force me to go through object references to other objects. this way lets me allocate exactly the right amount of space, no more, no less. basically, is there a way to make a totally arbitrary array of pointers using "new"? |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 4
![]() |
>>is there a way to make a totally arbitrary array of pointers using "new"?
T *p = new T*[N]; // Simulate an array of N pointers to T |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
i guess that's why C++ supports C code.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Feb 2005
Posts: 89
Rep Power: 4
![]() |
thanks, eggbert.
can N be a variable, and can that array be created at runtime? |
|
|
|
|
|
#5 |
|
Newbie
|
malloc is really an abstraction of the "new object" meta-layer in modern C/C++ .
|
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 4
![]() |
>can N be a variable, and can that array be created at runtime?
Yes, and yes. |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Feb 2005
Posts: 89
Rep Power: 4
![]() |
thanks again. this does exactly what i need it to do, and MUCH easier than malloc-ing. went through this section in my dietel book and i'm much happier. and that delete[] is sweet, too.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|