3

My code seems to work(I haven't tried it with large datasets because of the above error).

Code:

#include <iostream>
#include <queue>
#include <stxxl/queue>

int main()
{
   //queue<int> q; //this works
   stxxl::queue<int> q; //does not work
   for (int i = 0; i<100; i++) {
       q.push(i);
   }
   std::cout << "done copying" << std::endl;
   while (q.empty() == false) {
       std::cout << q.front() << std::endl;
       q.pop();
   }
   std::cout << "done poping" << std::endl;
   return 0;
}

my simple .stxxl is simply: disk=./testfile,0,syscall

But my error is:

stackexchangeexample(3884) malloc: *** error for object 0x101c04000: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
The program has unexpectedly finished.

I'm not sure how to troubleshoot it, do I need to free memory in this case? I'm still learning c++ so sorry if this is really basic(this only happens when I use the stxxl queue).

Lostsoul
  • 25,013
  • 48
  • 144
  • 239
  • Have you been deleting previous versions of this question and reposting it? I'm sure I've seen this a few times today. – Blastfurnace May 13 '12 at 19:25
  • @Blastfurnace yes sorry. I have done it today, as someone posted a comment(last time, I was advised to to solve another warning I was getting, so I deleted the question while learning how to configure . stxxl). If I can solve it myself(or pointed to the right direction), I didn't want to waste people's time to read it. – Lostsoul May 13 '12 at 19:34
  • You can edit your question rather than deleting it, and reposting a modified version. – luke May 14 '12 at 18:21

1 Answers1

1

I've never used stxxl before but since it's a template you can take a look at the code here: http://algo2.iti.kit.edu/stxxl/trunk/queue_8h_source.html. And since you're a newbie I'll explain a few things. This goofy queue maintains a queue of pointers. Line 00054 shows typedef ValTp value_type, so now your int is a value_type. Line's 00072 & 00073 show that your front and back elements are of that value_type. Do you see how they will be maintained as pointers. Finally if you look at any constructor the pool_type* pool defined on line 00069 will be "new'd" up (which is the basis of your elements) and the init function is always called. And within init, pool->steal() is called, click on it if you want to learn more.

In short, you need to be pushing new'd up integers onto your queue. Bad interface, not your fault.

jiveturkey
  • 2,484
  • 1
  • 23
  • 41
  • Wow..thanks so much, I'll study the code more and understand more. Thanks for spending time to explain. I thought maybe I was not using malloc/free or something.. – Lostsoul May 14 '12 at 19:14
  • Thanks again jnbbender. I did try to read all their source code to understand but I'm confused at what I need to do to get it to work. What does it mean to push `new'd up` integers? – Lostsoul May 18 '12 at 02:00
  • when you say something is `new'd` up it just means you've used `new` to allocate memory for it, in other words `Integer myInt = new Integer` Here I've `new'd` up an `Integer`. Pushing refers to the fact that they've placed them all on their list, maybe pushing isn't what I should've said but all I meant was they're putting them on their list. So to get it to work: `int p[] = {12, 34, 56 }; q.push(p+i);` Or `int* p1 = new int(12); q.push(p1); int* p2 = new int(34); q.push(p2);` You need to push pointers. – jiveturkey May 23 '12 at 18:55