• Advertisement
Sign in to follow this  

Another C++ Pointer Question

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

Hi all,

I have the following setup:

float *m_fA;
float *m_fB; //Both NULLED prior to use obviously
...
...
if(m_fB == NULL)
{
m_fB = m_fA; //Problem line
}
else
{
for(int i = 0; i < 10; i++)
{
m_fB += m_fA;
}
}

So I wanted m_fA to be added to m_fB on every iteration. The trouble is m_fB is a pointer to m_fA so it is adding to itself. How can I make it so the contents of A are put into B, and then I can perform the code within the loop as expected. I hope this is clear.

cheers

Share this post


Link to post
Share on other sites
Advertisement
Well, if you simply want to copy the contents of one array into another array, loop through the array and and assign each value in m_fA into each element of m_fB.

for (unsigned int i = 0; i < size; ++i)
m_fB = m_fA;


Thats one way of going about it, but there is probably a standard function to perform this kind of operation in one line, see if you cant find it on the net or something.

Share this post


Link to post
Share on other sites
Quote:
Original post by CodeCriminal
Thats one way of going about it, but there is probably a standard function to perform this kind of operation in one line, see if you cant find it on the net or something.


There is :)

std::copy(m_fA, m_fA + numberOfElementsInfA, m_fB);


Share this post


Link to post
Share on other sites
Quote:
Original post by SiS-Shadowman
Quote:
Original post by CodeCriminal
Thats one way of going about it, but there is probably a standard function to perform this kind of operation in one line, see if you cant find it on the net or something.


There is :)
*** Source Snippet Removed ***


Ah yes, how did I miss that haha.
Also, I neglected to mention that if you are infact using pointers (i.e.no array has actually been statically typed) then you are going to need dynamically allocate B.

Think about it, a pointer 'points' to memory addresses, it doesnt have its own personal memory block when instantiated, there for you have to create this block of memory yourself, and to do so requires dynamic (as opposed to static) memory allocation.

eg.
m_fB = new float [size]; // replace size with a variable/constant that contains the actual size of the array you want to instantiate. 

// ...

// and when you finished with it..
delete [] m_fB;

Share this post


Link to post
Share on other sites
Thanks for all the help guys, I've managed to solve the problem. I was setting B to be a pointer to A in an attempt to allocate the right amount of memory for B. This of course meant that any change to A would also change B, which I didn't want. As soon as I allocated memory for B manually I didn't have any more problem. Rookie mistake I guess! Still can't get my head round pointers!

Thanks again

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement