0

I've tried many different variants of both of the ways, and cannot get it to sum the array. It's passed into the function as a pointer and need to calc the mean and return.

unsigned char calcMean(unsigned char *buffer, int height, int width)
{    
   unsigned char mean, sum=0;
   counter i, k;
   int size;

   size = width*height;

   mean = 0;
   for (i = 0; i < size; i++) {
       sum += buffer[i];
   }

   /*

   for(k=0;k<(width*height);k++)
   {
     mean = mean + *Buffer;
     frameBuffer++;
        printf("%d\n", mean);
   }        
   */   

    return sum;

 }
8
  • 6
    sum may be out of range of unsigned char type. Consider declaring is as a variable of larger data type like int. Commented Oct 16, 2013 at 10:12
  • What output do you get? What values are in buffer[i] when you run it under a debugger? Commented Oct 16, 2013 at 10:12
  • How is buffer filled? What happens when you run this code? Why are you trying to sum chars instead of integers? Have you tried a debugger? Commented Oct 16, 2013 at 10:12
  • @sgar91 it has to be that type, the output for mean is zero, same for sum Commented Oct 16, 2013 at 10:14
  • sorry guys for not clearly defining the question, i'm not sure how to sum the elements of an array that this pointer is pointing to Commented Oct 16, 2013 at 10:15

2 Answers 2

2

mean can be a char, since you would divide by size to calculate it. But the sum itself can be as high as size * 255 (8bits of unsigned char). width * height can overflow too.

To be at the safe side (on 32 or 64bit machines), consider this:

16bit * 16bit integer would require a 32bit integer (for the dimension). An unsigned char array with a size up to 4294967295 would then require 64bits for the sum.

So, if possible, use explicit integer sizes (C99):

uint8_t calcMean(uint8_t *buffer, uint16_t height, uint16_t width)
{    
   uint64_t sum=0;
   size_t i;
   size_t size;//size_t is large enough to store a pointer,
               //so it would have 32 or 64bits on corresponding platforms
               //(see uintptr_t, etc.)

   size = ((size_t)width)*((size_t)height);

   for (i = 0; i < size; i++) {
       sum += buffer[i];
   }

   return sum / size;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Or just use size_t everywhere.
You're right, I should've mentioned size_t. When built in 64bit mode, size_t would exceed those requirements, but that wouldn't make much of a difference. However, one could still use uint16_t for width and height, just for the sake of self explaining code.
0

To guard yourself from buffer overflow for the buffer[] array, assuming you have array defined and initialized with values, pass size of array along with pointer to array's first element rather than the method you have currently to calcMean().

Call to calcMean() shall be:

uint8_t array_name[3]={1,2,3}; /* Array size and values assumed */
...
calcMean(array_name, sizeof(array_name))

calcMean() definition shall be :

uint8_t calcMean(uint8_t *buffer, size_t buffer_size)
{
    uint64_t sum=0;
    size_t i;

    for(i = 0; i < buffer_size; i++)
    {
        sum += buffer[i];
    }
    ..../*Do whatever you want if at all needed */

    return (sum / size);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.