Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 19th, 2005, 6:55 PM   #1
brkstf
Programmer
 
brkstf's Avatar
 
Join Date: Feb 2005
Posts: 89
Rep Power: 4 brkstf is on a distinguished road
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"?
brkstf is offline   Reply With Quote
Old Apr 19th, 2005, 7:56 PM   #2
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
>>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
Eggbert is offline   Reply With Quote
Old Apr 20th, 2005, 2:24 AM   #3
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
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.
bl00dninja is offline   Reply With Quote
Old Apr 20th, 2005, 1:28 PM   #4
brkstf
Programmer
 
brkstf's Avatar
 
Join Date: Feb 2005
Posts: 89
Rep Power: 4 brkstf is on a distinguished road
thanks, eggbert.

can N be a variable, and can that array be created at runtime?
brkstf is offline   Reply With Quote
Old Apr 20th, 2005, 1:42 PM   #5
Donald Ferrone
Newbie
 
Donald Ferrone's Avatar
 
Join Date: Mar 2005
Posts: 19
Rep Power: 0 Donald Ferrone is on a distinguished road
Send a message via MSN to Donald Ferrone
malloc is really an abstraction of the "new object" meta-layer in modern C/C++ .
Donald Ferrone is offline   Reply With Quote
Old Apr 20th, 2005, 2:31 PM   #6
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
>can N be a variable, and can that array be created at runtime?
Yes, and yes.
Eggbert is offline   Reply With Quote
Old Apr 21st, 2005, 11:03 AM   #7
brkstf
Programmer
 
brkstf's Avatar
 
Join Date: Feb 2005
Posts: 89
Rep Power: 4 brkstf is on a distinguished road
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.
brkstf is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:06 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC