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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:46:36+00:00 2026-05-27T11:46:36+00:00

Not very experienced with C++, I’ve got the following: void read_image(vector<unsigned char>* buffer) {

  • 0

Not very experienced with C++, I’ve got the following:

void read_image(vector<unsigned char>* buffer) {
    buffer = new vector<unsigned char>(2048);
}   

int main(int argc, char ** argv) {
    vector< unsigned char > *buffer;

    read_image(buffer);

    delete buffer; // gives invalid pointer at run-time

    return 0;
}

This is only the relevant part of the code, basically I want to initialize the pointer buffer in read_image(). At run-time I’m getting this when trying to free up the heap:

*** glibc detected *** ./main: free(): invalid pointer: 0x00007fff0648556c ***

What’s wrong with my approach?

  • 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-27T11:46:37+00:00Added an answer on May 27, 2026 at 11:46 am

    You are not sending buffer by reference (or as a pointer to vector<unsigned char>*) when callingread_image`, therefor the function is unable to propagate the update to outside of the function (ie. the caller).


    This modification of your function will result in what you wish:

    void read_image(vector<unsigned char>*& buffer) {
        buffer = new vector<unsigned char>(2048);
    }   
    

    We are now passing a reference as a parameter to read_image, and the original variable will be updated with the value returned from new vector<...> (...).


    If you are a pointer fanatic this is also valid:

    void read_image (vector<unsigned char>** buffer) {
        *buffer = new vector<unsigned char>(2048);
    }  
    
    ...
    
    read_image (&buffer);
    

    In the above we are giving read_image the address of the pointer itself, and inside we can dereference this pointer to pointer to set the value pointed to by the original pointer.

    Excuse the wording, I’m quite tired to be honest.


    FAQ regarding the use of references.

    • parashift.com/c++-faq-lite/ – [8] References
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to AD LDS and experienced but not qualified with SSAS, so
I'm not very experienced in this area - so I've got a few questions.
( Updated a little ) I'm not very experienced with internationalization using PHP, it
I'm a long time developer but not very experienced with DNS. Here's my problem:
Building a Django app on a VPS. I am not very experienced with setting
Forgive me if this is a stupid question. I am not very experienced with
I am not a very experienced C++ programmer, i get a warning when i
Question related to PHP memory-handling from someone not yet very experienced in PHP: If
I am not very experienced with PHP. I have a site I'm maintaining that
I am not very experienced with either programming or scripting database queries, and I

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.