Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7774675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:26:18+00:00 2026-06-01T17:26:18+00:00

I am trying to free a three dimensional pointer array representing a bmp image

  • 0

I am trying to free a three dimensional pointer array representing a bmp image and whle it compiles ok I get SIGTRAP signals in gdb when debugging. My error message is

warning: HEAP[bmpsample.exe]:
warning: Heap block at 0061FFB8 modified at 0061FFCC past requested size of c.
Program received signal SIGTRAP, Trace/ breakpoint trap. 0x7787704e5 in ntdll!TpWaitForAlpcCompletion()
from ntdll.dll

The error occurs when I free the array after loading in the values from the bmp file. My code is as follows.

Allocation:

int ***alloc3D(int xlen, int ylen, int zlen) {
int i, j, ***array;
if ((array = malloc(xlen*sizeof(int**)))==NULL) {
    perror("Error in first assignment of 3D malloc\n");
}
// Allocate pointers for each row
for (i = 0; i < xlen; i++) {
    if ((array[i] = malloc(ylen*sizeof(int*)))==NULL){
        perror("Error in second assignment of 3D malloc\n");
    }
    // Allocate pointer for each column in the row
    for (j=0; j < ylen; j++) {
        if((array[i][j] = malloc(zlen*sizeof(int)))==NULL) {
            perror("Error in third assignment of 3D malloc\n");
        }
    }
}

Filling the array

int ***readBitmap(FILE *inFile, BmpImageInfo info, int*** array) {
    // Pixels consist of unsigned char values red, green and blue
Rgb *pixel = malloc( sizeof(Rgb) );
int read, j, i;
for( j=0; j<info.height; j++ ) {
    read = 0;
    for( i=0; i<info.width; i++ ) {
        if( fread(&pixel, 1, sizeof(Rgb), inFile) != sizeof(Rgb) ) {
                printf( "Error reading pixel!\n" );
        }
        array[j][i][1] = (int)(pixel->red);
        array[j][i][2] = (int)(pixel->green);
        array[j][i][3] = (int)(pixel->blue);
        read += sizeof(Rgb);
    }

    if ( read % 4 != 0 ) {
        read = 4 - (read%4);
        printf( "Padding: %d bytes\n", read );
        fread( pixel, read, 1, inFile );
    }
}
free(pixel);

return array;

}

Deallocation

void dealloc3D(int*** arr3D,int l,int m)
{
    int i,j;

    for(i=0;i<l;i++)
    {
        for(j=0;j<m;j++)
        {
                free(arr3D[i][j]);
        }
        free(arr3D[i]);
    }
    free(arr3D);
}

I suscpect that the problem lies in casting the RGB values from unsigned char to int, but I see no other way of doing it. If I simply assign integer values to the alloced array there is no problem with freeing them.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:26:19+00:00Added an answer on June 1, 2026 at 5:26 pm

    You have a problem with the first fread statement

    fread(&pixel, 1, sizeof(Rgb), inFile)
    

    which is reading into the pointer pixel, not into what pixel is pointing to. After that, any use of pixel is likely to corrupt the heap (or something else).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to free memory that's reallocated but I get an error... float *
I'm trying to get the Registration-Free Activation of COM Components: A Walkthrough sample from
Sometimes in execution I get this error message in VS2010 when trying to free
I am trying to free up some unused resources in my app. I have
I'm trying to free g_strdup but I'm not sure what I'm doing wrong. Using
I am trying to nail down free space on a remote server by querying
I've been trying to find a free database creator for mac os, and i'm
I'm trying to detect Use after free() bugs, otherwise known as Dangling pointers. I
I'm trying to match any bullet list in a free text document. Bullet lists
I am trying to compare two lists of data which has some free text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.