Click here to Skip to main content
15,894,720 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: Array Size Pin
CPallini28-Sep-08 23:35
mveCPallini28-Sep-08 23:35 
AnswerRe: Array Size Pin
TeVc++28-Sep-08 23:49
TeVc++28-Sep-08 23:49 
GeneralRe: Array Size Pin
CPallini29-Sep-08 0:15
mveCPallini29-Sep-08 0:15 
GeneralRe: Array Size Pin
TeVc++29-Sep-08 0:32
TeVc++29-Sep-08 0:32 
GeneralRe: Array Size Pin
CPallini29-Sep-08 0:47
mveCPallini29-Sep-08 0:47 
GeneralRe: Array Size Pin
TeVc++29-Sep-08 19:45
TeVc++29-Sep-08 19:45 
GeneralYou are welcome. Pin
CPallini29-Sep-08 21:49
mveCPallini29-Sep-08 21:49 
Questiondynamic allocation of arrays [modified] Pin
Sauce!28-Sep-08 19:14
Sauce!28-Sep-08 19:14 
In one of my projects I have an array of pointers to Tile's (a class of my own) within a Block class. Upon calling the constructor for the Block class I assign the pointer to each Tile in the array to the address of a Tile created on the heap using the new operator like so;

Block::Block()
{
while(i=0; i < 20; i++)
{
Array_of_pointers_to_Tiles[i] = new Tile;
}
}


I then call the delete operator in the destructor like so;

Block::~Block()
{
while(i=0; i < 20; i++)
{
delete Array_of_pointers_to_Tiles[i];
Array_of_pointers_to_Tiles[i] = NULL;
}
}


This code does not compile.

According to this resource[^] I am to allocate the array like so;

int* a = NULL;   // Pointer to int, initialize to nothing.
int n;           // Size needed for array
cin >> n;        // Read in the size
a = new int[n];  // Allocate n ints and save ptr in a.
for (int i=0; i<n; i++) {
    a[i] = 0;    // Initialize all elements to zero.
}
. . .  // Use a as a normal array
delete [] a;  // When done, free memory pointed to by a.
a = NULL;     // Clear a to prevent using invalid memory reference.


and to free the memory allocated to the array;

delete [] a;  // Free memory allocated for the a array.
a = NULL;     // Be sure the deallocated memory isn't used.


However this code doesn't compile either! Frown | :(

What is the correct way?!?

modified on Monday, September 29, 2008 1:44 AM

AnswerRe: dynamic allocation of arrays Pin
Saurabh.Garg28-Sep-08 19:23
Saurabh.Garg28-Sep-08 19:23 
GeneralRe: dynamic allocation of arrays Pin
Sauce!28-Sep-08 19:28
Sauce!28-Sep-08 19:28 
GeneralRe: dynamic allocation of arrays [modified] Pin
Sauce!29-Sep-08 21:01
Sauce!29-Sep-08 21:01 
GeneralRe: dynamic allocation of arrays Pin
Iain Clarke, Warrior Programmer30-Sep-08 0:20
Iain Clarke, Warrior Programmer30-Sep-08 0:20 
GeneralRe: dynamic allocation of arrays Pin
Sauce!30-Sep-08 0:55
Sauce!30-Sep-08 0:55 
GeneralRe: dynamic allocation of arrays Pin
Iain Clarke, Warrior Programmer30-Sep-08 1:25
Iain Clarke, Warrior Programmer30-Sep-08 1:25 
GeneralRe: dynamic allocation of arrays Pin
Sauce!30-Sep-08 1:34
Sauce!30-Sep-08 1:34 
GeneralRe: dynamic allocation of arrays Pin
Iain Clarke, Warrior Programmer30-Sep-08 1:48
Iain Clarke, Warrior Programmer30-Sep-08 1:48 
GeneralRe: dynamic allocation of arrays Pin
Sauce!30-Sep-08 2:16
Sauce!30-Sep-08 2:16 
GeneralRe: dynamic allocation of arrays Pin
Iain Clarke, Warrior Programmer30-Sep-08 3:21
Iain Clarke, Warrior Programmer30-Sep-08 3:21 
GeneralRe: dynamic allocation of arrays [modified] Pin
Sauce!30-Sep-08 5:01
Sauce!30-Sep-08 5:01 
GeneralRe: dynamic allocation of arrays Pin
Iain Clarke, Warrior Programmer30-Sep-08 5:31
Iain Clarke, Warrior Programmer30-Sep-08 5:31 
GeneralRe: dynamic allocation of arrays Pin
Sauce!30-Sep-08 5:43
Sauce!30-Sep-08 5:43 
GeneralRe: dynamic allocation of arrays Pin
Iain Clarke, Warrior Programmer30-Sep-08 6:02
Iain Clarke, Warrior Programmer30-Sep-08 6:02 
GeneralRe: dynamic allocation of arrays Pin
Sauce!2-Oct-08 5:05
Sauce!2-Oct-08 5:05 
GeneralRe: dynamic allocation of arrays [modified] Pin
Sauce!4-Oct-08 2:42
Sauce!4-Oct-08 2:42 
GeneralRe: dynamic allocation of arrays Pin
Sauce!4-Oct-08 5:19
Sauce!4-Oct-08 5:19 

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.