pop_front a pointer

I'm getting malloc: pointer being freed was not allocated error.

I suspect that it's one of my function that uses pop_front().

This is the basic run down of what I was planning to do with my code. I have a list that acts as a Queue, that holds pointers. I dequeue from my list into a vector, so I place the list's pointer at the front and push_back into the vector. I dequeue from the list since it's suppose to be in the vector.

This is the mock code that I have with the functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
struct A{

int x;
};

class myListClass{
public:

  shared_ptr<A> getFront(){
    return myQueue.front();
  }
  // I think this is giving the malloc error.
  void deleteFront(){
    myQueue.pop_front();
  }
  
  void addIntoQueue(shared_ptr<A> item){
    myQueue.push_back(item);
  }
private:
  list < shared_ptr<A> > myQueue;
};

class myVectorClass{

public:
  void addIntoVector(shared_ptr<A> item){
    myVector.push_back(item);
    object.deleteFront();
  }

private:
  vector < shared_ptr<A> myVector;
  myListClass object;
};

int main(){

  shared_ptr<A> obj (new A);
  myListClass list;
  myVectorClass v;
  // Adds object pointer to my list
  list.addIntoQueue(obj);
  // Adds object pointer into my vector and removes from list
  v.addToVector(list.getFront());

   

}
Why does the myVectorClass have a myListClass object? (And why does myVectorClass::addIntoVector attempt to remove an element from an empty list?)

While I'm asking silly questions: Why didn't you supply a compilable code snippet that illustrates the problem rather than a piece of code that cannot be compiled? (The method on line 45 does not exist in myVectorClass and you're missing a > on line 33.)
Last edited on
PCB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifndef PCB_H
#define PCB_H
#include <memory>
#include <list>

static int totalPID = 0;
class PCB{

public:
	
	// When a PCB object is created, the PID is increased by 1
	// Input 'A' should create a PCB object
	PCB()
	{ 

		totalPID++; 
	}
	
	void createNewPID(){

		pid = ++totalPID;
	}
	void setPID(){

		pid = totalPID;
	}

	// Returns the PID of the PCB
	int getPID(){

		return pid;
	}

private:
	int pid;

};

#endif 


DeviceQueue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef READY_QUEUE_H
#define READY_QUEUE_H
#include "PCB.h"
#include <list>
#include <memory>

class ReadyQueue{

public:

	ReadyQueue(){}

	~ReadyQueue(){}
	// Push a device into the queue
	void pushIntoQueue(std::shared_ptr<PCB> anItem){

		rQueue.push_back(anItem);
	}

	// Gets whatever is at the front of the queue
	std::shared_ptr<PCB> getFront(){

		return rQueue.front();
	}

	// Removes the first element in the queue
	void readyQueueDequeue(){

		rQueue.pop_front();
	}

	void resetPCB(std::shared_ptr<PCB> terminatePCB){

		totalPID++;
		terminatePCB->setPID();
		unusedMemory.push_back(terminatePCB);
	}

	void pushToUnusedMemory(std::shared_ptr<PCB> terminatedPCB){

		unusedMemory.push_back(terminatedPCB);
	}


	void printRQ(){

		for(std::list< std::shared_ptr<PCB> >::iterator it = rQueue.begin(); it != rQueue.end(); it++ ){

			std::cout << (*it)->getPID() << " ";
		}
		std::cout << '\n';
	}

private:

	std::list< std::shared_ptr<PCB> > rQueue;
	std::list< std::shared_ptr<PCB> > unusedMemory;
};

#endif 


CPU
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#ifndef CPU_H
#define CPU_H
#include <vector>
#include "ReadyQueue.h"

class CPU{

public:
	
	CPU(){}

	~CPU(){}

	bool isEmpty(){

		return cpuProcess.empty();
	}

	void insertIntoCPU(std::shared_ptr<PCB> aPCB){

		cpuProcess.push_back(aPCB);
		waitingQueue.readyQueueDequeue();
	}

	std::shared_ptr<PCB> getProcess(){

		return cpuProcess.front();
	}

	void interrupt(std::shared_ptr<PCB> aPCB){

		waitingQueue.pushToUnusedMemory(aPCB);
	}

	std::shared_ptr<PCB> getFrontOfQueue(){

		return waitingQueue.getFront();
	}

	std::shared_ptr<PCB> terminate(std::shared_ptr<PCB>aPCB){

		aPCB->createNewPID();
		return aPCB;
	}

	void removeFromCPU(){

		cpuProcess.erase(cpuProcess.begin());
	}
private:
	std::vector< std::shared_ptr<PCB> > cpuProcess;
	ReadyQueue waitingQueue;

};

#endif 


My main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include "ReadyQueue.h"
#include "PCB.h"
#include "Device.h"
#include "CPU.h"

using namespace std;

int main(){
	ReadyQueue r;
	CPU c;
	shared_ptr<PCB> a (new PCB);
	a->setPID();
	r.pushIntoQueue(a);
	r.printRQ();
	cout << c.isEmpty() << endl;
	cout << r.getFront() <<endl;
	c.insertIntoCPU(r.getFront());
	cout<< c.isEmpty() << endl;
	r.printRQ();
	return 0;
}
Last edited on
malloc: pointer being freed was not allocated error

wrong use of free: http://coliru.stacked-crooked.com/a/3b0f97886c9d2f2c
edit: why? because trying to free pointer not allocated memory with alloc
better use of free: http://coliru.stacked-crooked.com/a/291046c211f65648
Last edited on
So you're saying I have to add free(rQueue.getFront()) inorder to free the pointer?
As in the original code when you call insertIntoCPU the waitingQueue is empty, but you still want to remove an element from it for some reason.

Don't remove (or access) elements from empty containers. Doing so results in undefined behavior.
Topic archived. No new replies allowed.