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 9144553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:17:52+00:00 2026-06-17T10:17:52+00:00

#pragma pack(push, 1) typedef struct { /*…*/ unsigned int dataoffset; //No of bytes before

  • 0
#pragma pack(push, 1)
typedef struct 
{
      /*...*/
  unsigned int      dataoffset;         //No of bytes before actual pixel data
}HEADER;

typedef struct 
{
   /*...*/
   unsigned int     width;
   unsigned int     height;
   unsigned short   bits_per_pixel;         //code is written for 24 bits only and No other format is supported..
    /*...*/
}INFO_HEADER;

typedef struct 
{
   unsigned char    b;
   unsigned char    g;
   unsigned char    r;        
}COLORMAP;

#pragma pack(pop)


int main()
{
      // Var decl.
  INFO_HEADER       *pHeader = NULL;
  FILE              *pImage;
  COLORMAP          **ppColors;
  /*...*/

/* File opened in read, binary mode, memory allocated for pHeader*/     

fread (pHeader, sizeof(INFO_HEADER), 1, pImage);  

/*Next block is actually problematic.. Posting 'as is' from my code*/
      ppColors = (COLORMAP**)malloc((pHeader -> height ) * sizeof(COLORMAP));
  for(i = 0 ; i < pHeader -> height ; i++)
     ppColors[i] = (COLORMAP*)malloc(pHeader -> width * sizeof(COLORMAP));
fseek(pImage, pHeader -> fileheader.dataoffset, SEEK_SET);
    for (i = 0 ; i < pHeader -> width ; i++) 
  {
      for (j = 0 ; j < pHeader -> height ; j++) 
      {
          fread(&b, sizeof(unsigned char), 1, pImage);
          fread(&g, sizeof(unsigned char), 1, pImage);
          fread(&r, sizeof(unsigned char), 1, pImage);

          ppColors[i][j].b = b;
          ppColors[i][j].g = g;
          ppColors[i][j].r = r; 

          printf("width = %d height = %d %d:\t", i, j,  cnt);
          printf("%d ", (int)ppColors[i][j].b);
          printf("%d ", (int)ppColors[i][j].g);
          printf("%d\n", (int)ppColors[i][j].r);
          cnt++;  
      }
  }

  /*And at last free()ing..*/
  for(i = 0 ; i < pHeader -> height ; i++)        free(ppColors[i]);
   free(ppColors);
  cleanup();
  return(0)
}

Possible Duplicate: http://stackoverflow.com/questions/1568042/optimal-way-to-free-a-malloced-2d-array-in-c

Though above link could not solve my problems.

  1. I run out of memory. I have malloc()ed for height, then for each of the height, width is again malloc()ed. I am trying to work on width X height domain only. It seems that problem is with height. If you change

ppColors = (COLORMAP**)malloc((pHeader -> height ) * sizeof(COLORMAP)); to
ppColors = (COLORMAP**)malloc((pHeader -> height + 6 ) * sizeof(COLORMAP));

Then this problem disappears.

  1. But then at the time of free()ing I get double free / corruption at core dump.

I am damn sure I am going wrong somewhere. I do not expect someone to correct my code and I just run it. Just hints would do.

  • 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-17T10:17:53+00:00Added an answer on June 17, 2026 at 10:17 am

    I can see a couple of problems:

    • ppColors is an array of pointers. Each element in the array is a COLORMAP* so you need to calculate the size to be malloced using numElements * sizeof(COLORMAP*). COLORMAP is just 3 chars so its likely that sizeof(COLORMAP*) > sizeof(COLORMAP). Your current allocation will be too small so you’ll end up writing beyond the end of the array; this has undefined effects but is likely to crash.
    • use of width & height are reversed between allocations and loops so you’ll end up writing to unallocated memory at some point in your loops.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is C code. #pragma pack(push,1) typedef struct _DATA_PKT_D1 { char length[2]; char command[2];
#include <stdio.h> #pragma pack(push) #pragma (1) typedef struct contact { char firstname [40]; char
I am doing some C# interop work. I have the following struct: #pragma pack(push,1)
I am using templates for my struct like: #pragma pack(push, 1) template <typename T>
I've got the following structs in C++: (Using pragma pack 1) typedef struct _wfs_cdm_physicalcu
// File: InitFirst.h #pragma once template <int val> struct InitFirst { static float s_dividedByThree;
I have a structure within #pragma pack(push,1) and #pragma pack(pop) . I would like
#pragma pack(L1_CACHE_LINE) struct A { //... }; #pragma pack() A a; and struct A
#pragma pack(push) #pragma pack(1) I downloaded a tutorial and it has these lines in
What does the following statement actually do and what are it's effects? #pragma pack(push,8)

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.