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

  • Home
  • SEARCH
  • 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 6012793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:27:19+00:00 2026-05-23T02:27:19+00:00

This is a long one. I have a function that loads some image data

  • 0

This is a long one. I have a function that loads some image data from disk. I’ve tried this with three different methods, one of which doesn’t work, and I’m wondering what the smartest method is.

Method 1:

I used to have it set up so that the loaded data was an argument to the function, which seemed to require allocating the space for it outside the function then passing a pointer to the allocated space. This requires knowing the size of the image ahead of time, so a helper function had to be made to get the image width and height first. The following is an example.

Pros: More explicit in the calling code that memory is being allocated and therefore should be deleted.

Cons: Need to know image size ahead of time.

// Data allocated outside the image, allocated space passed to function. This works.
// Notice that width & height are passed to the function. 
size=(*width)*(*height);
image  = new unsigned char[size];

void read_pgm(unsigned char *image, char *file_name, int width, int height){

    // Code to read sizeof(char)*width*height bytes of data from the file into image

}

Method 2:

I thought it would be nice to have the function allocate its own data so I don’t need to pass it a size. If I tried to allocate the space for it in the function, it seemed to be lost after the function ended. In the following case, the function read_pgm runs fine, but if I then try to write that data to another file, my code crashes.

Pros: Don’t need to know image size ahead of time, no need to allocate data in calling code.

Cons: Doesn’t work. Also, if it did, would calling it repeatedly in a loop result in a memory leak if I wasn’t clearing image outside the function?

// Data allocated inside the image for a pointer passed to the function. This doesn't work.
void read_pgm(unsigned char *image, char *file_name, int *width, int *height){

    size=(*width)*(*height);
    image  = new unsigned char[size];

    // Code to read the data from the file into image

}

Method 3:

Here, the data is again allocated in the function but is handed back as a returned item. This works, i.e. I can then write the data out to another image fine. I don’t understand why this works and Method 2 does not.

Pros: Same as Method 2.

Cons: Same as Method 2 except this currently works.

// Data allocated in the function, and returned. This works.
unsigned char* read_pgm(char *file_name, int *width, int *height){

    // Allocate data for the image
    size=(*width)*(*height);
    image  = new unsigned char[size];

    // Code to read the data from the file into image

    return image; // Return pointer to the data
}

So, my questions are:

  1. In a case like this, is it smarter to set the function up to allocate space itself so the calling code doesn’t need to supply the image size? Or is it smarter to allocate outside the function as a reminder that delete needs to be called on image at some point. Or am I wrong in thinking that this needs to be done? It seems like calling Method 2 or Method 3 in a loop would make a memory leak.

  2. Why doesn’t Method 2 work?

Thanks.

  • 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-23T02:27:20+00:00Added an answer on May 23, 2026 at 2:27 am

    Answer to question 2

    In order to make it work you must pass a reference to the pointer

    unsigned char * & image
    

    Otherwise you allocate memory and the COPY of the passed pointer points to it. The original pointer object doesn’t change.

    Answer to question 1

    Ever heard of smart pointers?

    Smart pointers can be used to manipulate memory all by themselves. If you don’t want to use smart pointers for some inexplicable reason, such as pseudooptimization, then I think you described the pros and cons of all methods yourself – there’s always tradeoff. Up to you to decide

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

Sidebar

Related Questions

I have this long and complex source code that uses a RNG with a
I have a function that returns an 8 digit long binary string for given
I have a function that can only accept strings. (it creates the image with
This is a long one, The Premise Was called in to help out a
I know this one is going to be a long-shot, but I thought I'd
So I have a hand-coded one-pager site with a very long length and dozens
I have a function that should be passed a string with two numbers (as
I have a javascript function that manipulates the DOM when it is called (adds
I have some Javascript code that needs to be dynamically created as a String
I'm hoping someone on this site can offer some help. I have a page

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.