Sign in to follow this  
AntiGuy

Help with dynamic memory!!

Recommended Posts

AntiGuy    267
I already know how to allocate the memory but the problem is when I switch my old data over to the new array everything that pointing to the old array doesn't switch over. Anyhelp?

Share this post


Link to post
Share on other sites
Pipo DeClown    804
Next time, you should put the code in your message so we can take a look at your problem. But this time, I'll guess that..

..you're probably using the = operator to 'copy' the old data to the new. When you do this with a pointer, the address will be copied, not the value. You should either use a special copying method like 'memcpy' or 'strcpy' (strings). The following should work too (only with single variables):

(*old) = (*new);

Share this post


Link to post
Share on other sites
demonkoryu    980
You need to copy the memory.

In C:


#include <memory.h>

const int elements = 10;
const int size_in_bytes = sizeof (char) * elements;
char* src = (char*) malloc( size_in_bytes );
char* dst = (char*) malloc( size_in_bytes );

memcpy( src, dst, size_in_bytes );


// don't forget to free the memory
free( src );
free( dst );




In C++:

#include <algorithm>

const int elements = 10;
char* src = new char[elements];
char* dst = new char[elements];

std::copy( src, src + elements, dst );

// don't forget to delete[] the memory
delete[] src;
delete[] dst;




If there are errors, I attribute them to my current uptime of 36 hours. :P
Of course, this is a very terse explanation (it is more an example). Feel free to ask for any details you didn't fully comprehend.

Share this post


Link to post
Share on other sites
AntiGuy    267
I'm not sure if I'm doing something wrong or I gave a wrong explaination.

Heres a bit of code:


items++;

Item newItm;
memset(&newItm,0,sizeof(Item));

Item *temp = new Item[items+1];
for(int i = 0; i < items-1; i++)
{temp[i] = Itm[i];}
temp[items-1] = newItm;

delete[] Itm;
Itm = temp;
temp = NULL;
}


Share this post


Link to post
Share on other sites
AntiGuy    267
I've done a bit of reseach and I think what I'm trying to do is resize an array but I hear you can't mix malloc, realloc, and free with new and delete.

Share this post


Link to post
Share on other sites
SiCrane    11839
Use a std::vector<>. If you maintain pointers and references to the vector rather than the data the vector holds then there shouldn't be any problems when the vector resizes. Besides which you don't need to manually write out the code for resizing yourself.

Share this post


Link to post
Share on other sites
Enigma    1410
Quote:
Original post by AntiGuy
memset(&newItm,0,sizeof(Item));

SiCrane's already given you the likely best solution but I just thought I'd point out that the above is very dangerous. You can only safely memset POD types (
Quote:
C++ FAQ Lite
[26.7] What is a "POD type"?

A type that consists of nothing but Plain Old Data.

A POD type is a C++ type that has an equivalent in C, and that uses the same rules as C uses for initialization, copying, layout, and addressing...
- see the link for the full definition).

Even if Item is currently a POD type you must also guarantee that it will always remain a POD type, or else change the memset line. Much safer to simply write a default constructor for Item which clears the members if it makes sense to do so, or to provide no default constructor and require that Items are initialised upon construction.

Enigma

Share this post


Link to post
Share on other sites
AntiGuy    267
Alright I think I've got it! It's a bit slow but if it's the best solution... Thanks bunches!

EDIT: Heh simple error! I was resizing the vector instead of reserving elements. Thanks Again!!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this