unsgined char array

Jun 28, 2010 at 7:26am
Hi guys,

I've initialized a couple of unsigne char array like this

eye_frame = new unsigned char [gl_img_h*gl_img_w];
scene_frame = new unsigned char [gl_img_h*gl_img_w];

then I would like to fill values in; i did

for (int i = 0; i<gl_img_h*gl_img_w; i++)
{
//unsigned char val1 = (gl_img_h*gl_img_w - i)%255;
//unsigned char val2 = 1+i;
////eye_frame[i] = val1;
//eye_frame[i] = val2;
//scene_frame[i] = val1;
*(eye_frame + i) = (unsigned char)i;
scene_frame[i] = 20;
}

But in all cases apart when I use a constant (last line), the array is not filled properly.

Where is the error?

thanks
Jun 28, 2010 at 7:29am
In what way is it not filled properly? How are you checking that?
Jun 28, 2010 at 7:46am
while debuggin I check the values.After new the array is filled with "ì" values, but after the first iteration the array becomes an empty array. Actually if I use this statement

unsigned char val1 = (gl_img_h*gl_img_w - i)%255;
scene_frame[i] = val1;

this will work if val1 is not equal to 0 for i=0. This does not make sense to me, I shoud be mistaken somewhere in the initialization.
Jun 28, 2010 at 9:44am
new function return a pointer to the memory allocated and the type of the pointer is the type of that memory.
so eye_frame and scene_frame should be unsigned char *.
ur code works fine and nothing wrong about it and if you wanna see the result of eye_frame and scene_frame in output, you have to type cast it to int as you know i guess charecter with ascii code lower than 32 are not printable....
i wrote the code for you and compile it and it works fine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <new>
#define max 10
using namespace std;
int main()
{
   unsigned char * eye_frame,* scene_frame;
    eye_frame=new unsigned char [max];
    scene_frame=new unsigned char [max];
    for (int i=0;i<max;i++){
        *(eye_frame+i)=(unsigned char)i;
        scene_frame[i]=20;
        }
    cout<<(int)scene_frame[2]<<"\t"<<(int)eye_frame[2];
    
    system("pause");
    return 0;
    
    }


and output :

20           2 press any key to continue...

Jun 28, 2010 at 10:38am
@tinauser

You do know that putting the number 1 into a char will NOT be the same as putting the character '1' into it right?

Each letter of the alphabet or number character has its own character code. The character code for the character '1' is 49.
Jun 28, 2010 at 1:30pm
@Galik. yep, I know it: one of the hyp. is that 0 is encoded as end of file, and therefore close the array.

@sourena. everything looks the same, the only difference is that I did not include <new> but I doubt that would be the reason of my problem.

Anyway, I don't need to initialize any byte with the 0 value (all bit = 0) so I just skip that value and everything works fine...
Topic archived. No new replies allowed.