Sign in to follow this  

troubles with pointers and allocated memory

This topic is 3635 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

I am running building some code trying to make some heightmaps. When I come around to the normals I have an array of vectors(just a struct with 3 floats) to hold each normal at the point on the grid. so i have a data member array called normals that is initilized like so normals = new Vector3D[length*width]; in my compute normals function i have a temp norms array declared like so Vector3D *norms = new Vector3D[length*width]; thus the problem, when i later go to draw the terrain normals is empty. I discovered in my computnormals function norms would be created with the same memory address as normals. so at the end of my function when i delete norms this deleted or perhaps just deleted the values in normals. Can this please be explained why this is so and how to avoid/ correct. [Edited by - ashstampede on January 4, 2008 6:38:59 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by ashstampede
using
Vector3D *norms = new Vector3D[sizeof(normals)]; didnt have the results I would have hoped.

Can this please be explained why this is so and how to avoid/ correct.
normals is a pointer, so sizeof(normals) will return 4 (assuming a 32-bit system).

Share this post


Link to post
Share on other sites
yeah it will, I realized that "didnt have the results I would have hoped." it just gave me a different memory address for the norms array so I got to the conclusion that the size declaration of [length * width] was giving me the same memory block for two arrays that i only want the same size for.

Share this post


Link to post
Share on other sites
Memory addresses mean nothing. It's possible that you'll never see the same memory address being used for two allocations, and it's equally possible you'll be given the same address back after every deallocation.

Just about the only thing you're guaranteed (I think, I'm not sure if it's in the C++ specification) is that two calls to new won't return the same address unless the address is freed.

Share this post


Link to post
Share on other sites

This topic is 3635 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.

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