Click here to Skip to main content
16,010,351 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questiontemplate bug in vc++6? Pin
nm_11427-Mar-05 7:21
nm_11427-Mar-05 7:21 
AnswerRe: template bug in vc++6? Pin
Joaquín M López Muñoz27-Mar-05 9:36
Joaquín M López Muñoz27-Mar-05 9:36 
GeneralRe: template bug in vc++6? Pin
nm_11427-Mar-05 13:30
nm_11427-Mar-05 13:30 
GeneralArray of Pointers Pin
LighthouseJ27-Mar-05 6:05
LighthouseJ27-Mar-05 6:05 
GeneralRe: Array of Pointers Pin
Gary R. Wheeler27-Mar-05 6:33
Gary R. Wheeler27-Mar-05 6:33 
GeneralRe: Array of Pointers Pin
LighthouseJ27-Mar-05 6:56
LighthouseJ27-Mar-05 6:56 
GeneralRe: Array of Pointers Pin
LighthouseJ27-Mar-05 11:40
LighthouseJ27-Mar-05 11:40 
GeneralRe: Array of Pointers Pin
Axter26-Apr-05 18:07
professionalAxter26-Apr-05 18:07 
The code below is a similar method, but a little more efficient, since it only calls new twice, and it only calls delete twice.
It calls new once to create the array of pointers, and then it calls new a second time to create a single block array of the target type.
The the for loop assigns different sections of the block of array to each pointer in the pointer array.
<br />
template < typename T ><br />
T **Allocate2DArray( int nRows, int nCols)<br />
{<br />
    T **ppi;<br />
    T *pool;<br />
    T *curPtr;<br />
    //(step 1) allocate memory for array of elements of column<br />
<br />
    ppi = new T*[nRows];<br />
<br />
    //(step 2) allocate memory for array of elements of each row<br />
    pool = new T [nRows * nCols];<br />
<br />
    // Now point the pointers in the right place<br />
    curPtr = pool;<br />
    for( int i = 0; i < nRows; i++)<br />
    {<br />
        *(ppi + i) = curPtr;<br />
         curPtr += nCols;<br />
    }<br />
    return ppi;<br />
}<br />
<br />
template < typename T ><br />
void Free2DArray(T** Array)<br />
{<br />
    delete [] *Array;<br />
    delete [] Array;<br />
}<br />
<br />
int main()<br />
{<br />
    double **d = Allocate2DArray<double>(10000, 10000);<br />
    d[0][0] = 10.0;<br />
    d[1][1] = 20.0;<br />
    d[9999][9999] = 2345.09;<br />
    Free2DArray(d);<br />
}<br />

Top ten member of C++ Expert Exchange.
http://www.experts-exchange.com/Cplusplus
GeneralFollow-Up Pin
LighthouseJ27-Mar-05 6:36
LighthouseJ27-Mar-05 6:36 
GeneralRe: Follow-Up Pin
Gary R. Wheeler27-Mar-05 7:01
Gary R. Wheeler27-Mar-05 7:01 
GeneralRe: Array of Pointers Pin
Michael Dunn27-Mar-05 10:41
sitebuilderMichael Dunn27-Mar-05 10:41 
GeneralAES algorithm implementation in VC++ Pin
Member 154898327-Mar-05 4:16
Member 154898327-Mar-05 4:16 
GeneralRe: AES algorithm implementation in VC++ Pin
Michael Dunn27-Mar-05 10:43
sitebuilderMichael Dunn27-Mar-05 10:43 
GeneralRe: AES algorithm implementation in VC++ Pin
Anonymous28-Mar-05 1:27
Anonymous28-Mar-05 1:27 
GeneralLaunching application at startup Pin
Imtiaz Murtaza27-Mar-05 3:33
Imtiaz Murtaza27-Mar-05 3:33 
GeneralRe: Launching application at startup Pin
Gary R. Wheeler27-Mar-05 3:42
Gary R. Wheeler27-Mar-05 3:42 
GeneralRe: Launching application at startup Pin
David Crow28-Mar-05 2:56
David Crow28-Mar-05 2:56 
General.net events from unmanaged code Pin
ischen_s127-Mar-05 2:34
ischen_s127-Mar-05 2:34 
GeneralDLL hook in an .exe Pin
DigitalPenetration27-Mar-05 2:10
DigitalPenetration27-Mar-05 2:10 
GeneralRe: DLL hook in an .exe Pin
ThatsAlok27-Mar-05 18:07
ThatsAlok27-Mar-05 18:07 
GeneralRe: DLL hook in an .exe Pin
DigitalPenetration28-Mar-05 7:47
DigitalPenetration28-Mar-05 7:47 
GeneralExport templates from DLL Pin
mistus27-Mar-05 1:57
mistus27-Mar-05 1:57 
GeneralRe: Export templates from DLL Pin
includeh1027-Mar-05 7:15
includeh1027-Mar-05 7:15 
GeneralRe: Export templates from DLL Pin
MMansonFan2529-Mar-05 23:05
MMansonFan2529-Mar-05 23:05 
QuestionHow to start/stop service Pin
Iliya Yordanov26-Mar-05 23:08
Iliya Yordanov26-Mar-05 23:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.