0

I am running the below minimal working example for storing some GNU multiprecision library (GMP) numbers mpz_class in a vector and then clearing the memory allocated as below:

#include <limits>
#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <gmp.h>
#include <gmpxx.h>

std::vector<mpz_class> GenVector(int n) {
   std::vector<mpz_class> result;
   mpz_class x("45642346"), y("45345345");
   for(int k=0; k<n; k++) {
   result.push_back(x + k*y);
   }
   return result;
}

int main() {
  int n(5);
  std::vector<mpz_class> states = GenVector(n);
  for(size_t k=0; k<states.size(); k++) std::cout<<states[k].get_mpz_t()<<std::endl;
  mpz_clear(states[0].get_mpz_t()); //OFFENDING LINE
  return 0;
}

Compiling and executing this gives

45642346
90987691
136333036
181678381
227023726
*** glibc detected *** ./Test: double free or corruption (fasttop): 0x000000000181d220 ***
======= Backtrace: =========

The answers are correct but there seems to be a memory corrpution; but if I change the index in the offending line from 0 to anything else like, say, 1, then there is no problem. Can somebody clarify what is going on? Thanks.

Note: Command for compilation and linking is g++ Program.cpp -o program.exe -lgmpxx -lgmp

MaviPranav
  • 335
  • 2
  • 14
  • I don't know anything about this library, but it seems to me that `mpz_class` should be handling the lifetime of the `mpz_t` and you should never call `mpz_clear` yourself. The need to call `mpz_clear` is in the C API where there is no such thing as a destructor, but `mpz_class` is part of the C++ API. – Dark Falcon Sep 19 '14 at 15:05
  • @DarkFalcon: thanks for the comment. In which case, how to free the space occupied by the `mpz_class` is unclear to me. Any clues, anyone? – MaviPranav Sep 19 '14 at 15:25
  • I realise that perhaps `states.clear()` is an alternative using the std library. – MaviPranav Sep 19 '14 at 15:33
  • @MaviPranav "*In which case, how to free the space occupied by the mpz_class is unclear to me.*" - You need to do some reading about the C++ language and the STL library: [`The storage of the vector is handled automatically, being expanded and contracted as needed.`](http://en.cppreference.com/w/cpp/container/vector). Also, since you seem to know `n` in advance, **always** call the `reserve` method on your vector prior to inserting data in it, lest you want to pay for painful intermediate resizing. Alternatively, you could use the appropriate`constructor, which allows you to specify the size. – Mihai Todor Sep 19 '14 at 17:00
  • That being said, once your `result` variable goes out of scope, because it is a local variable allocated statically, it will be disposed of automatically once it goes out of scope, together with its contents, because `mpz_class` objects are C++ constructs, which have the appropriate destructor. Take a look [here](http://stackoverflow.com/a/15651693/1174378) for a more detailed explanation. – Mihai Todor Sep 19 '14 at 17:08

0 Answers0