0

At the very start of the edges function i try to declare an array to hold the image with black borders (now the first comment), but as i've discovered using printfs, it causes a segfault. I searched online and found that the problem is probably a lack of memory on the stack, so i tried dynamically creating the array on the heap, but now it segfaults inconsistently, sometimes on the first three lines, others further ahead. Please point out if there is a problem with my code or if there really is a lack of memory, and in that case, what can i do about it?

// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
    //RGBTRIPLE holder[height + 2][width + 2];
    // Allocate new image array from heap because stack is full.
    RGBTRIPLE **holder;
    int holderh = height + 2;
    int holderw = width + 2;

    // Allocate for each row.
    holder = (RGBTRIPLE **) malloc(holderh * sizeof(RGBTRIPLE *));
    if (holder == NULL)
    {
        printf("No memory for sec array in edges.\n");
        exit(1);
    }

    // Allocate for each column.
    for (int i = 0; i < holderh; i++)
    {
        holder[i] = (RGBTRIPLE *) malloc(holderw * sizeof(RGBTRIPLE));
        if (holder[i] == NULL)
        {
            printf("No memory for sec array in edges.\n");
            exit(1);
        }
    }

    // index - index - multiplicative -> For calculating Gx and Gy.
    // Indexes refer to which element of 2d array to operate and mult is the mult weight applied to it.
    int index_length = 6;
    int xindex[][3] = {{-1, -1, -1}, {0, -1, -2}, {1, -1, -1}, {-1, 1, 1}, {0, 1, 2}, {1, 1 ,1}};
    int yindex[][3] = {{-1, -1, -1}, {-1, 0, -2}, {-1, 1, -1}, {1, -1, 1}, {1, 0, 2}, {1, 1, 1}};

    // Copies image to new array with black borders.
    for (int i = 0, h = height + 2, h1 = height + 1; i < h; i++)
    {
        for (int j = 0, w = width + 2, w1 = width + 1; j < w; j++)
        {
            if (i == 0 || j == 0 || i == h1 || j == w1)
            {
                holder[i][j].rgbtRed = 0;
                holder[i][j].rgbtGreen = 0;
                holder[i][j].rgbtBlue = 0;
            }
            else
            {
                holder[i][j].rgbtRed = image[i - 1][j - 1].rgbtRed;
                holder[i][j].rgbtGreen = image[i - 1][j - 1].rgbtGreen;
                holder[i][j].rgbtBlue = image[i - 1][j - 1].rgbtBlue;
            }
        }
    }

    // Finds edges and copies them into image.
    for (int i = 1; i <= height; i++)
    {
        for (int j = 1; j <= height; j++)
        {
            int Gx[3] = {0, 0, 0};
            int Gy[3] = {0, 0, 0};

            // Finds edges on x axis for all color channels.
            for (int k = 0; k < index_length; k++)
            {
                Gx[0] += image[xindex[k][0]][xindex[k][1]].rgbtRed * xindex[k][2];
                Gx[1] += image[xindex[k][0]][xindex[k][1]].rgbtGreen * xindex[k][2];
                Gx[2] += image[xindex[k][0]][xindex[k][1]].rgbtBlue * xindex[k][2];
            }

            // Finds edges on y axis on all color channels.
            for (int k = 0; k < index_length; k++)
            {
                Gy[0] += image[yindex[k][0]][yindex[k][1]].rgbtRed * yindex[k][2];
                Gy[1] += image[yindex[k][0]][yindex[k][1]].rgbtGreen * yindex[k][2];
                Gy[2] += image[yindex[k][0]][yindex[k][1]].rgbtBlue * yindex[k][2];
            }

            // Calculate geometric mean of Gx and Gy.
            int Gxy[3];
            for (int k = 0; k < 3; k++)
            {
                Gxy[k] = round(sqrt(Gx[k] * Gx[k] + Gy[k] * Gy[k]));
                if (Gxy[k] > 255)
                {
                    Gxy[k] = 255;
                }
            }

            // Return pixels to original array.
            image[i - 1][j - 1].rgbtRed = Gxy[0];
            image[i - 1][j - 1].rgbtGreen = Gxy[1];
            image[i - 1][j - 1].rgbtBlue = Gxy[2];
        }
    }

    for (int i = 0; i < holderh; i++)
    {
        free(holder[i]);
    }
    free(holder);

    return;
}
2
  • Couldn't replicate a seg fault. What are you using for your inputs? Also, while a production program may run out of memory due to massive memory requirements, nothing in these class assignments would approach that kind of memory usage, except for maybe a memory leak combined with an infinite loop. A seg fault is more likely trying to directly access a memory address that's outside the allowed memory space for the program or an invalid address such as a negative number.
    – Cliff B
    Feb 24, 2023 at 18:52
  • It is fine now, i came back to the exercise some time later and it works without change. I even returned to simply declaring the array and it works. I'm also pretty sure it was suffering a segfault in the array declaration, so i don't know what it could've been. I don't think it's possible it was a memory leak or invalid memory access since it erroed on the first line of my function. In any case, thank you for clarifying about the possible causes.
    – Decowry
    Mar 5, 2023 at 14:49

1 Answer 1

0

I just restarted my codespace and it works now without a change. I suppose it wasn't that big of a problem, though i still don't know what it was.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .