• C

Pointers and Arrays

I am having a little trouble with the following code, it works for c++ but i  get violation errors when compiled in C ...i am using visc++7.1


code:

struct test
{
unsigned char *data;
}

test *i;
unsigned char temp;
int h;

    for( h = 0; h < i->m_size * 3; h += 3 )
    {
            temp = i->data[h]; // allways gives access violation error's here
    }

nutterxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
Hi nutterx,

test * i is uninitialized. It is a dangling pointer and could be pointing to anything.
Also there is no member m_size in struct test!!! Is this the exact code ?

Cheers!
Sunnycoder
nutterxAuthor Commented:
sorry i cut alittle too much out ;-)

struct test
{
int m_size;
unsigned char *data;
}

i am actually trying to convert a BMP from BGR to RGB

      i->data = (unsigned char *)malloc(Infoheader.biSizeImage);
        i->m_size = Infoheader.biSizeImage
      fread(i->data,1,Infoheader.biSizeImage,fptr);

i can actually display the image ok using BGR but i want it to be RGB so i am trying to convert it
here is all the code for the convert
 
        unsigned char temp;
      unsigned char *rgb = (unsigned char*)i->m_data[0];
    for( h = 0; h < i->m_size * 3; h += 3 )
    {
            temp = rgb[h]; //access violation here!!
            rgb[h] = rgb[h + 2];
                rgb[i + 2] = temp;
    }



nutterxAuthor Commented:
oops i do initialise "test i;" too:

i = (test *)malloc(sizeof(test));

like i said the code works all ok just not when i try to convert from gbr to rgb,so im very sure everything is initialised ok.
Cloud as a Security Delivery Platform for MSSPs

Every Managed Security Service Provider (MSSP) needs a platform to deliver effective and efficient security-as-a-service to their customers. Scale, elasticity and profitability are a few of the many features that a Cloud platform offers. View our on-demand webinar to learn more!

sunnycoderCommented:
>unsigned char *rgb = (unsigned char*)i->m_data[0];
i has no member m_data .. I am guessing that you want it to point to i->data
The assignment should have been rgb = i->data
i->data[0] would be a unsigned char and not pointer to it

>for( h = 0; h < i->m_size * 3; h += 3 )

you have m_size of data and you are accessing upto m_size*3. You clearly are trying to read beyond the memory you allocated.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rajeev_devinCommented:
>> oops i do initialise "test i;" too:
Ok you intialize test i;
But did you allocated space for unsigned char *data;
rajeev_devinCommented:
Is your data allocated ? Something like this

test *i;
i = (test *)malloc(sizeof(test));
i->data = malloc(...);
nutterxAuthor Commented:
thanks sunny, i can't believe i missed that!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.