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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:34:12+00:00 2026-05-28T20:34:12+00:00

I have a small memory access problem in my program and I do not

  • 0

I have a small memory access problem in my program and I do not find the error, maybe someone could help me.

I have created a new type to store rgb color values.
That type looks like:

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

In my main program I create with calloc a 2D dynamically array, to store the red color information’s.

pixel **pixelvalue = (pixel **) calloc(imginformation.width, sizeof(pixel));
for (i = 0; i < imginformation.width; i++) {
    pixelvalue[i] = (pixel *) calloc(imginformation.height, sizeof(pixel));
}

After that I call my function, which read the color values and who should safe them to the array. The function gets as parameter the array.

ReadFile(file, imginformation (Stuff like height and so one), pixelvalue (The calloc array));

In that function I try to write the values with

pixelvalue[i][j].r = (unsigned char)fgetc(in);

Here I get the memory access error, what did I wrong?

Edit

Hi, first of all sorry about the missing language, I was a little bit tired yesterday :).

To understanding, I created an array of pixel and the elements are pointing to another array of pixel? Something like [Point to another 1D array pixel]?

With pixel **pixelvalue = calloc(imginformation.width, sizeof(pixel *)); I create imginformation.width numbers of pointers from type pixel and each pointer show to pixel, right?

It would be awesome if you could explain it a little bit more, if I’m wrong. I would really like to understand it.

@carl-norum What do you mean with:

“you shouldn’t be casting the return values of calloc(). Doing so can
hide bugs with #include that could come back to bite you down the
road”.

? I use the alloc space as parameter for a function, not as return value.

Thanks for your help!

Greetz

  • 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-05-28T20:34:13+00:00Added an answer on May 28, 2026 at 8:34 pm

    You’re not really making a 2D array, you’re making an array of pointers that point to arrays of pixels. That means your first calloc call should allocate enough space for pointers, not for pixels:

    pixel **pixelvalue = calloc(imginformation.width, sizeof(pixel *));
    

    You didn’t tag your question with a language, but assuming it’s C (based on your typedef, which wouldn’t be necessary in C++), you shouldn’t be casting the return values of calloc(). Doing so can hide bugs with #include that could come back to bite you down the road.

    Edit:

    You asked a couple of follow-up questions. The first has been answered pretty well by several other answers, I think, but I’ll try to summarize. The way you’re doing the allocation, you are first going to allocate an array of pointers – each of those pointers is going to point to one row of your array. The rows themselves then need to be allocated – space for each pixel object goes there, and the pointers to the rows are stored in the first array of pointers.

    Your second question, bout the return value from calloc() is pretty straightforward. If you cast the return value, you can hide implicit declaration bugs from yourself. Since the return type of calloc is void *, if you do something like:

    my_ptr = calloc(1, 2);
    

    Everything works nicely. Now imagine that you didn’t include stdlib.h, and therefore had no prototype of calloc() in your translation unit. That would lead the compiler to assume the signature of calloc() to be int calloc(int, int), which isn’t true. The same line of code above would throw you a warning about assuming a default signature for that function. Using a typecast like you have in your code will mask that warning and you might never know you were missing that #include line.

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

Sidebar

Related Questions

have small problem, and would very much appreciate help :) I should convert byte
I have been working on a program, simulating a small database where I could
I have small problem with my .net 2.0 winforms application. I want to embed
I have small test app: Socket socket = new Socket(jeck.ru, 80); PrintWriter pw =
I’ve been working on a few small scale Access projects that have turned large
I have a small memory leak in my code regarding Strings in CLI/C++. I've
I have a small confusion in the dynamic memory allocation concept. If we declare
Maybe I start will a small introduction for my problem. I'm writing a python
I am currently learning to program on iPhone. I currently have a small test
I am having some strange problem. I have written a small module in VC++

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.