//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 = *temp;
*temp++;
}
setting array values with a char pointer
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.
any help appreciated
si
You have a typo:
or an alternative:
//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 = *temp; // *temp++; // <- typo here temp++; // thats better // or do this instead: DataArray = *(temp + i); // or this DataArray = temp; }
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 );
You don't have to loop. You can copy an entire memory block in one go using memcpy. Try this:
#include <string.h>...memcpy(DataArray, temp, 64);
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])
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]
[edit]No offense, guys, but in C++ code, I clearly advocate the use of std::copy over memcpy.[wink][/edit]
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
this->m_pImageData = (unsigned char *) 0x6167742e <address 0x6167742e out of bounds>
any idea what could be causing this?
cheers
si
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement