2

I'm doing a video processing project, and I got struck in assigning the block address for sending to the dct function.

The following line is not taking the correct assignment address as the right hand variable pointing to.

temp = (unsigned short *)((unsigned short *)(p_vqi->luma + j) + l);

so temp does not contain the correct address pointed by the p_vqi->luma variable, where j and i will be incremented 16 times on each step for a maximum of 144 and 176 respectively.

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Arun
  • 53
  • 1

3 Answers3

2

The thing that often gets people with pointer math is that it doesn't add one byte at a time, it adds one sizeof(thing pointed to) at a time, so you're going to skip over j lumas, however big that is, then i unsigned shorts, however big that is on your architecture. Usually, it's easier and more portable when working with fixed formats to work straight in bytes, like:

uint8_t* temp = (uint8_t*)p_vqi->luma;
temp += j*16 + i;
Karl Bielefeldt
  • 47,314
  • 10
  • 60
  • 94
2

Be aware that adding a number to a pointer increments the pointer by that number of elements, not bytes. In other words, you're first adding j * sizeof(the type of the luma entries) to the pointer, and then i * sizeof(unsigned short) which for most implementations are two bytes.

If however you want to add j + i bytes you should rather do something like this.

temp = (unsigned short *)((intptr_t)p_vqi->luma + j + i);

That should give you a pointer to an unsigned short that is advanced i + j bytes from the original. The intptr_t type is C99, if you need to be compatible with older compilers use unsigned long instead.

harald
  • 5,976
  • 1
  • 24
  • 41
  • Thankq Mr. Harald myself did this but its giving the waring as warning C4047: '=' : 'unsigned short **' differs in levels of indirection from 'unsigned short *' – Arun May 11 '11 at 12:43
  • myself tried different command its assinging fine but still dont know its correct or wrong as me still in the midle of the code. – Arun May 11 '11 at 12:45
1

Addition (& substraction) of pointers have a different behavior that the standard arithmetic operations. These operations will be factorised by the size of the data they represent.

Lets take an example, considering that char is one byte, short is two bytes and an int is 4 bytes length:

char * p1;
shot * p2;
int * p3;

p1 += 1; // p1 will be incremented by 1
p2 += 1; // p2 will be incremented by 2
p3 += 1; // p3 will be incremented by 4
greydet
  • 5,509
  • 3
  • 31
  • 51