-1

I'm trying to get monochrome image from .bmp image with using bitmap_image.hpp library. But in one place (Pic[i][j] = 0.3 * r + 0.59 * g + 0.11 * b;) i receive that error: Unhandled exception at 0x0019BD8F in PicCircle.exe: 0xC0000005: Access violation writing location 0x011CF000. . So, that's wrong with it?

code:

#define _SCL_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "bitmap_image.hpp"

#define C 0.01

double** ArrayCreate ( int M, int N )
{
    int i;
    double** ArrayRoot;

    ArrayRoot = (double **)malloc(sizeof(double*) * M);

    for (i = 0; i < M; i++)
        ArrayRoot[i] = (double *)malloc(sizeof(double) * N);

    return ArrayRoot;
}

void ArrayDestroy ( double** Array , int M)
{
    int i;

    for (i = 0; i < M; i++){
        Array[i] = (double *)realloc(Array[i], 0);
    };

    Array = (double **)realloc(Array, 0);
}

void main ( void )
{
    double** Pic;
    unsigned char r, g, b;
    int H, W, i, j;

    bitmap_image image("m1.bmp");

    H = image.height();
    W = image.width();

    Pic = ArrayCreate(H, W);


    for (i = 0; i < W; i++)
        for (j = 0; j < H; j++)
        {
            image.get_pixel(i, j, r, g, b);
            Pic[i][j] = 0.3 * r + 0.59 * g + 0.11 * b;
        }

    for (i = 0; i < W; i++)
        for (j = 0; j < H; j++)
        {
            if (abs(sqrt(pow(Pic[i + 1][j] - Pic[i][j], 2) + pow(Pic[i][j + 1]  - Pic[i][j], 2))) >= C)
                Pic[i][j] = 1;
            else
                Pic[i][j] = 0;
        }


    ArrayDestroy(Pic, H);
}
  • Minor: Suggest `free(Array[i]); Array[i] = NULL;` rather than `Array[i] = (double *)realloc(Array[i], 0);`. Passing a 0 to `realloc(ptr, 0)` does not guarantee freeing all memory. – chux - Reinstate Monica Jun 10 '14 at 17:35
  • `image.get_pixel(i, j, r, g, b);` -- can we see what `get_pixel` does? You do not appear to pass these variables by reference. – Jongware Jun 17 '14 at 13:36

2 Answers2

2

In your first loop you access the Pic array as Pic[width][height], but in the second loop you access it as Pic[height][width]. One of those two is incorrect, probably the first one.

Fixing your for loop should correct the issue.

Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
  • Yes, don't observed it. Now it's fixed. But still, problem appears earlier than second for, so problem is still there. – user3726846 Jun 10 '14 at 16:33
2

This:

ArrayRoot = (double **)malloc(sizeof(int*) * M);

looks super-broken; it assumes sizeof (int *) to be the same as (sizeof double *) which is probably true, but still a very broken thing to write.

The follow-up is worse:

ArrayRoot[i] = (double *)malloc(sizeof(int) * N);

since sizeof (int) is very probably smaller than sizeof (double) this is going to lead to horror.

The way to avoid this category of error is to never write the type name in the malloc() argument: dereference the pointer being assigned to, instead. The latter would then become:

ArrayRoot[i] = malloc(N * sizeof *ArrayRoot[i]);
                                  ^^^^^^^^^^^^^
                                  this part is to
                                  the left of the =

This also drops the cast of course.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
  • Ops, thanks, copied that part for int and didn't change all types. Fixed it, but error is still appears it that place, but now it is "Unhandled exception at 0x00D4BD8F in PicCircle.exe: 0xC0000005: Access violation writing location 0xFDFDFDFD.". – user3726846 Jun 10 '14 at 16:37
  • +1, especially for `ArrayRoot[i] = malloc(N * sizeof *ArrayRoot[i])` – chux - Reinstate Monica Jun 10 '14 at 17:48