setting array values with a char pointer

Started by
7 comments, last by Lonefox1 18 years, 7 months ago
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 = *temp;
         *temp++;
     }


any help appreciated si
Advertisement
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 = *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);
Jooleem. Get addicted.
they all seem to be giving me the same access violation :S
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]
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
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.
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

This topic is closed to new replies.

Advertisement