[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Pointers and Arrays

Posted on 2006-04-17
7
Medium Priority
?
197 Views
Last Modified: 2010-04-15
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
    }

0
Comment
Question by:nutterx
  • 3
  • 2
  • 2
7 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 16475298
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
0
 

Author Comment

by:nutterx
ID: 16475324
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;
    }



0
 

Author Comment

by:nutterx
ID: 16475330
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
ID: 16475373
>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.
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16475381
>> oops i do initialise "test i;" too:
Ok you intialize test i;
But did you allocated space for unsigned char *data;
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16475386
Is your data allocated ? Something like this

test *i;
i = (test *)malloc(sizeof(test));
i->data = malloc(...);
0
 

Author Comment

by:nutterx
ID: 16475415
thanks sunny, i can't believe i missed that!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question