Sign in to follow this  
Lonefox1

setting array values with a char pointer

Recommended Posts

Hi guys im trying to assign some values from a char pointer into an array, my pointer knowledge aint too hot so im not sure what im doing wrong here. im getting an access violation for the first i value in the for loop.
//function for returning the pointer:
unsigned char *GetImage() { return m_pImageData; }

//trying to assign the data to an array (in main cpp)
unsigned char * temp;
unsigned char  DataArray[64];

     temp = m_tga->GetImage();
     for (int i = 0; i < 64; i++)
     {
         DataArray[i] = *temp;
         *temp++;
     }


any help appreciated si

Share this post


Link to post
Share on other sites
You have a typo:


//function for returning the pointer:
unsigned char *GetImage() { return m_pImageData; }

//trying to assign the data to an array (in main cpp)
unsigned char * temp;
unsigned char DataArray[64];

temp = m_tga->GetImage();
for (int i = 0; i < 64; i++)
{
DataArray[i] = *temp;
// *temp++; // <- typo here
temp++; // thats better
// or do this instead:
DataArray[i] = *(temp + i);
// or this
DataArray[i] = temp[i];
}



or an alternative:


#include <algorithm>

//function for returning the pointer:
unsigned char *GetImage() { return m_pImageData; }

//trying to assign the data to an array (in main cpp)
unsigned char * temp;
unsigned char DataArray[64];

temp = m_tga->GetImage();

std::copy( temp, temp+64, DataArray );

Share this post


Link to post
Share on other sites
Quote:
Original post by Konfusius
You have a typo:

*** Source Snippet Removed ***


You could do that or,



//function for returning the pointer:
unsigned char *GetImage() { return m_pImageData; }

//trying to assign the data to an array (in main cpp)
unsigned char * temp;
unsigned char DataArray[64];

memcpy( DataArray, temp, sizeof(DataArray) );





Mind you, then you have to make sure both the source and destination are of the proper size, for the amount of bytes your copying.


Also, you are saying your getting an access violation. Is the pointer being returned by GetImage() valid?


Rig


(EDIT:: Talk about beaten to the punch [grin])

Share this post


Link to post
Share on other sites
At what address does the AV occur? My bet is your m_pImageData isn't a valid pointer.

[edit]No offense, guys, but in C++ code, I clearly advocate the use of std::copy over memcpy.[wink][/edit]

Share this post


Link to post
Share on other sites
hmm just went through a bit of debugging i watched the m_pImageData var and got this:

this->m_pImageData = (unsigned char *) 0x6167742e <address 0x6167742e out of bounds>

any idea what could be causing this?

cheers
si

Share this post


Link to post
Share on other sites
You'll have to find out where you assigned to m_pImageData.
Also, other errors in your program can cause your stack become corrupted.
Looks like a funny little debugging session is ahead. If you can't sort it out, please post some more code.

Share this post


Link to post
Share on other sites
i think its to do with my TGA loader, im unsure if it supports greyscale images thus making the pointer useless :S seems to work fine if i load a normal tga in. gona have a mess about with it now thanks guys

si

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