Original Post
Comments for the article Pointers: Beginning to Intermediate
quote:No, they're not. Faulty understanding and use of new and delete are the number one and two causes of memory leaks. Here are simple rules of thumb that virtually guarantee headache-free dynamic memory management in C++:
From the article:
New and Delete are the number one cause of memory leaks.
char * AllocateChars( int size )
{
return new char [size];
}
std::auto_ptr< char > AllocateChars( int size )
{
return std::auto_ptr< char >( new char [size] );
}
quote:Such as? I'm sorry if I sound harsh, but I find this statement insufficiently qualified. An example would have been nice so that the statement can be corroborated or more clearly understood.
The STL library provides solutions to common problems where new and delete are apart of the initial solution.
class A
{
void a( void ) { int i = 1 + 2; }
typedef void (*A:: Pmf)( void ); // EDIT: P-smiley
pmf pointer_to_Aa;
};
A a;
a.pointer_to_Aa = a.a;
a.(*pointer_to_Aa)();
quote:
Before you can use a pointer, you have to initialize it. You must set it to point to a valid memory location, otherwise it points to nowhere and that can crash your computer.
enum{
FLAG1, // = 0
FLAG2,
FLAG3,
…
FLAG12, // = 12
};
quote:
Original post by Illumini
Oluseyi, I do agree with you that the headaches associated with new/delete are avoidable. But a firm grasp on the rules you provide works only in theory. In reality it takes along time programming with new/delete to avoid the myriad of problems you can cause yourself. For the beginer/intermiediate programmer (who this article is aimed at) I feel it gives the accurate portrail of how frustrating new/delete can be at times.
That said I feel new/delete can be used without much of a headache but this only comes with expierence, in my opinion anyway.
quote:
Original post by Oluseyireturn std::auto_ptr< char >( new char [size] );
int array[5] = {1,2,3,4,5}; //initialize array of int values
int* array2[5]; //create an array of pointers to int values
int* pointer = array; //store the pointer to the first element of the array of int values
for(int i = 0; i < 5; i++)
{
array2[i] = ++pointer; // increase the pointer to point to the next address in the array
}
array2[4] = array; //append the address of first ordinal to end of array of addresses
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more