rdoubleui
rdoubleui

Reputation: 3598

Invalid pointer - why?

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?

Upvotes: 5

Views: 2146

Answers (3)

Filip Ros&#233;en
Filip Ros&#233;en

Reputation: 63882

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.

Upvotes: 11

Ameliorator
Ameliorator

Reputation: 437

I guess you want to create buffer which can hold more than one image otherwise I dont know why you need a vector here.

if my guess about your requirement is correct you should do something like this -

void read_image(vector<unsigned char*>& buffer,int size) { 
    char* p = new char[size];
    buffer.push_back(p);
}
void process_image_buffer(char* image_buffer) {
    // Do something with image_buffer
}
int main(int argc, char ** argv) {
    vector<unsigned char*> buffer;
    for all images
       // calculate the unknown size at runtime here e.g.by taking input from user
       read_image(buffer,size);

    // Do Image Processing
    // process each image buffer in a for loop
    for all images (i < buffer.size())
       process_image_buffer(buffer[i]);

    // Free all at end
    for all images (i < buffer.size()) 
       delete[] buffer[i];
    return 0;
}

Upvotes: 2

DhruvPathak
DhruvPathak

Reputation: 43265

void read_image(vector<unsigned char>* & buffer) { // passed by reference
    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;
}

Upvotes: 2

Related Questions