Sign in to follow this  
MadMax1992

new unsigned char[25] = 40 elements?

Recommended Posts

I have a simple class that creates an array of unsigned chars. The class will then fill this array with pseudo-random numbers. This array of chars is not ment to be used as a c-style string (or any other kind of string). This is the code:
m_pArray = new unsigned char[m_nNumElements];

for (unsigned int i = 0; i < m_nNumElements; i++) {
m_pArray[i] = unsigned char(MyMath::GetRand(0, 255));
}


however, while I was just making sure everything works, I noticed in my 'watch-window', that the m_pArray array has 40 elements. I assume this has something to do with null-termination of the 'string'. Will this produce any dodgy results if I only access characters withing the [0, 25] range? Or do I need to null-terminate it anyway? Thanks in advance, Max Henger EDIT: edited to correct variable names [Edited by - MadMax1992 on November 7, 2008 7:52:18 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by MadMax1992
I assume this has something to do with null-termination of the 'string'.

Right.

Quote:
Original post by MadMax1992
Will this produce any dodgy results if I only access characters withing the [0, 25] range?

No.

Share this post


Link to post
Share on other sites
Watch-window? Is that in your debugger?

If it is, what line are you seeing that at? It should show 25 elements after the 1st line you listed, but not before.

Also, you said "m_pGenes array has 40 elements." but your code is referring to "m_pArray = new unsigned char[m_nNumElements];"

Was it a typo and you mean m_pArray in both cases? If not, that's here your error is.

Also, you declare m_pArray but in your loop you refer to pArray. That needs to be addressed.

Share this post


Link to post
Share on other sites
the fact that the variable names aren't the same is because I wanted to created a more generic look on this problem instead of the way I implemented it. My variable names are correct ;-)

Thank you both for your simple but relevant answers!

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