the new operator
when i allocate a vector to a pointer with the new operator and allocate again another vector with different dimensions to the same pointer does the memory from the first vector get totaly freed?
Huh?
Show us a code sample of what you''re talking about... there''s no such thing as a "vector to a pointer".
How appropriate. You fight like a cow.
Show us a code sample of what you''re talking about... there''s no such thing as a "vector to a pointer".
How appropriate. You fight like a cow.
quote:
Original post by jcabeleira
when i allocate a vector to a pointer with the new operator and allocate again another vector with different dimensions to the same pointer does the memory from the first vector get totaly freed?
No, you HAVE TO call delete
sorry if i wasn''t clear, my english is a little rusty but here is an example:
/*first allocation*/
char *pointer= new char[10];
/*second allocation*/
pointer= new char[20];
so, does the memory block from the first instruction get freed when i make the second allocation or do i really need to call the delete operator between the two instructions?
/*first allocation*/
char *pointer= new char[10];
/*second allocation*/
pointer= new char[20];
so, does the memory block from the first instruction get freed when i make the second allocation or do i really need to call the delete operator between the two instructions?
noVum is correct, if you don''t use delete your just having it point to something else, which probably means that you now have no way in accessing the data that it was pointing to..
Please visit Turt99 Productions
Please visit Turt99 Productions
Instead of using the new operator you could use the malloc(), realloc() and free() functions.
If you allocate memory and then you use realloc() you haven't to free the memory first.
Example1:
/*first allocation*/
char* pointer = (char*)malloc(10);
/*second allocation*/
pointer = (char*)realloc(pointer,20);
And only when you don't use pointer anymore you can free memory:
free(pointer);
Example2:
Another way is to use only realloc() function.
/*first allocation*/
char* pointer = NULL;
pointer = (char*)realloc(pointer,10);
/*second allocation*/
pointer = (char*)realloc(pointer,20);
//..and at the end
free(pointer);
________________________________________________________
www.wizardofoz.fx.to
[edited by - BioLich on September 24, 2003 6:25:27 PM]
If you allocate memory and then you use realloc() you haven't to free the memory first.
Example1:
/*first allocation*/
char* pointer = (char*)malloc(10);
/*second allocation*/
pointer = (char*)realloc(pointer,20);
And only when you don't use pointer anymore you can free memory:
free(pointer);
Example2:
Another way is to use only realloc() function.
/*first allocation*/
char* pointer = NULL;
pointer = (char*)realloc(pointer,10);
/*second allocation*/
pointer = (char*)realloc(pointer,20);
//..and at the end
free(pointer);
________________________________________________________
www.wizardofoz.fx.to
[edited by - BioLich on September 24, 2003 6:25:27 PM]
________________________________________________________www.wizardofoz.tk
Another option is to use the STL. Instead of a char[] you can use a list. The difference in performance is negligable and you can forget most of the memory handling.
No, the List is no alternative. It is implemented as a linked list which IS slower than an array. You mean std::vector which is an automatically resizing array with constant access times.
September 25, 2003 03:22 PM
A minor correction on what has been told above. It is correct that you must delete anything that you new. There is no garbage collector in standard C++ as there is in Java. However, when allocating an array, you must be sure to use the correct form of delete.
For instance:
// allocate an array of characters
char* array = new char[1024];
// delete the array of characters, make sure to use delete[]
// because you used new[] above.
delete[] array;
Similarly, if you allocate a single object using new, you must make sure that you use delete, not delete[].
class example {
public:
int value;
};
int main() {
// create an instance of ''example''
example *myExample = new example;
// delete an instance of ''example''
delete myExample;
// the WRONG way to delete ''myExample''
// delete[] myExample;
}
If you use delete[], many times your application will enter into an infinite loop (or just crash) as the runtime library attempts to find the end of the myExample array. Because myExample is not an array it will never find the end!
STL:
I agree with noVum, a list is a bad idea. However, a vector internally is just an array. Indexing into the vector is slightly more expensive than indexing into an array, though, as the internal mechanism for indexing into the vector involves pointer arithmetic. While opponents will argue that there is further overhead associated with calling the operator[] function, this is not necessarily true with modern compilers. Most C++ compilers will inline this operation thus removing the function call overhead.
For instance:
// allocate an array of characters
char* array = new char[1024];
// delete the array of characters, make sure to use delete[]
// because you used new[] above.
delete[] array;
Similarly, if you allocate a single object using new, you must make sure that you use delete, not delete[].
class example {
public:
int value;
};
int main() {
// create an instance of ''example''
example *myExample = new example;
// delete an instance of ''example''
delete myExample;
// the WRONG way to delete ''myExample''
// delete[] myExample;
}
If you use delete[], many times your application will enter into an infinite loop (or just crash) as the runtime library attempts to find the end of the myExample array. Because myExample is not an array it will never find the end!
STL:
I agree with noVum, a list is a bad idea. However, a vector internally is just an array. Indexing into the vector is slightly more expensive than indexing into an array, though, as the internal mechanism for indexing into the vector involves pointer arithmetic. While opponents will argue that there is further overhead associated with calling the operator[] function, this is not necessarily true with modern compilers. Most C++ compilers will inline this operation thus removing the function call overhead.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement