Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

jcabeleira

the new operator

This topic is 5559 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!