Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
#include <iostream>
#include <sstream>
#include "blocknode.h"
using namespace std;
class MemoryManager
{
public:
MemoryManager(unsigned int memsize);
unsigned char * malloc(unsigned int request);
void free(unsigned char * blockptr);
blocknode *getFirstPtr();
friend ostream & operator<<(ostream & out,const MemoryManager &M);
private:
unsigned int memsize;
unsigned char *baseptr;
blocknode * firstBlock;
void mergeForward(blocknode *p);
void splitBlock(blocknode *p,unsigned int chunksize);
};
Here is the BLOCKNODE.h file
#include <iostream>
using namespace std;
struct blocknode
{
unsigned int bsize;
bool free;
unsigned char *bptr;
blocknode *next;
blocknode *prev;
blocknode(unsigned int sz,unsigned char *b,bool f=true,blocknode
*p=0,blocknode *n=0):
bsize(sz),free(f),bptr(b),prev(p),next(n) {}
};
CPP FILE
#include <cassert>
#include <iostream>
#include <sstream>
#include <string>
#include "MemoryManager.h"
using namespace std;
ostream & operator<<(ostream & out,const MemoryManager &M)
{
blocknode *tmp = M.firstBlock;
assert(tmp);
while(tmp)
{
out << "[" << tmp->bsize << ",";
if (tmp->free)
out << "free] ";
else
out << "allocated] ";
if (tmp->next)
out << " -> ";
tmp = tmp->next;
}
return out;
}
MemoryManager::MemoryManager(unsigned int memtotal): memsize(memtotal)
{
baseptr = new unsigned char[memsize];
firstBlock = new blocknode(memsize,baseptr);
}
blocknode *MemoryManager::getFirstPtr()
{
return firstBlock;
}
unsigned char * MemoryManager::malloc(unsigned int request)
// Finds the first block in the list whose size is >= request
// If the block's size is strictly greater than request
// the block is split, with the newly create block being free.
// It then changes the original block's free status to false
{
blocknode * tmp = this->firstBlock;
assert(tmp);
while (tmp){
if (tmp->bsize >= request){
if (tmp->bsize > request){
splitBlock(tmp, request);
return tmp->bptr;
}
tmp->free = false;
return tmp->bptr;
}
tmp = tmp->next;
}
}
void MemoryManager::splitBlock(blocknode *p, unsigned int chunksize)
// Utility function. Inserts a block after that represented by p
// changing p's blocksize to chunksize; the new successor node
// will have blocksize the original blocksize of p minus chunksize and
// will represent a free block.
// Preconditions: p represents a free block with block size > chunksize
// and the modified target of p will still be free.
{
if (p->free == false || p->bsize <= chunksize) {
cout << "Error splitting memory....exiting with error code 1" << endl;
exit(1);
}
blocknode * heap = new blocknode(p->bsize,p->bptr + chunksize,true,0,0);
heap->bsize = p->bsize - chunksize;
heap->prev = p;
p->bsize = chunksize;
p->next = heap;
}
void MemoryManager::mergeForward(blocknode *p)
// merges two consecutive free blocks
// using a pointer to the first blocknode;
// following blocknode is deleted
{
blocknode * tmp = p->next;
p->bsize += p->next->bsize;
p->next = tmp->next;
tmp->next->prev = p;
delete tmp;
}
void MemoryManager::free(unsigned char *blockptr)
// makes the block represented by the blocknode free
// and merges with successor, if it is free; also
// merges with the predecessor, it it is free
{
blocknode * tmp = this->firstBlock->next;
assert(tmp);
while (tmp) {
if (tmp->bptr == blockptr) {
tmp->free = true;
if (tmp->free == true && tmp->next->free == true) {
mergeForward(tmp);
}
if (tmp->free == true && tmp->prev->free == true) {
mergeForward(tmp->prev);
}
}
}
}
The goal of this program is to pretty much simulate the C heap manager which deals with malloc() and free(). I am having trouble with the last four functions of the memory manager cpp file. (refer to the comments) The code compiles however my program crashes during runtime, it says that there is an unhanded exception at memory location XxXXXXXXX does anyone know what is causing this? Line 110 ("if(tmp->next->free == true)") is where the program breaks
When MemoryManager::free() calls mergeForward() (the first call to mergeForward()) as a result of what happens in mergeForward(), it looks like the tmp pointer used by free() will no longer be valid, because mergeForward() deleted it.
The derefence of tmp, immediately afterwards, will result in undefined behavior.
This is in addition to the other bug in free() that I noted in the comments.
Related
I have a code where I'm trying to delete the first id in a structure. It used to work, but now it just returns "ЭЭЭЭЭЭЭЭЭЭЭЭЭЭЭЭЭЭЭЭ" (repeated russian letter). I tried reinstalling VS, didn't help. I know it works because I tried it on an online compiler.
These two warnings also point to the delete function, which I'm guessing is the problem.
Warning C4156 deletion of an array expression without using the array form of 'delete'; array form substituted
Warning C4154 deletion of an array expression; conversion to pointer supplied
Here is the code itslef:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <locale.h>
#include <iostream>
#pragma warning (disable: 4703) //disables warning of uninitialized variable j
using namespace std;
#define MAXDL 9
struct el_sp
{
char id[MAXDL];
struct el_sp* sled;
};
void vkl(struct el_sp** p, char t_id[]) //enters the entered ID's from keyboard into the struct
{
struct el_sp* pt,
* k, * j;
pt = (struct el_sp*)malloc(sizeof(struct el_sp));
strcpy_s(pt->id, t_id);
if (*p == NULL || strcmp(pt->id, (*p)->id) < 0)
{
pt->sled = *p; *p = pt;
}
else
{
k = *p;
while (k != NULL && strcmp(pt->id, k->id) >= 0)
{
j = k; k = k->sled;
}
j->sled = pt; pt->sled = k;
}
}
void pech_sp(struct el_sp* p) //prints the struct
{
struct el_sp* i;
char* o;
printf("\Result:\n");
for (i = p; i != NULL; i = i->sled)
puts(i->id);
}
int main() {
setlocale(LC_ALL, "RUS");
struct el_sp* p;
unsigned n;
unsigned i;
char t_id[MAXDL];
printf("\nEnter the amount of identificators\n n=");
scanf_s("%u", &n);
getchar();
p = NULL;
printf("Enter the identificators (press enter after each one)\n");
for (i = 1; i <= n; i++)
{
gets_s(t_id);
vkl(&p, t_id);
}
delete p->id;
pech_sp(p);
return 0;
}
P.S. Delete does the same thing whatever i try, in any code
P.S.S. Sorry for bad formatting, it's the way our prof needs it
Don't call delete p->id. It serves no purpose except to crash your program: neither it nor the struct it is in was allocated by new.
Also, never use delete to try to free malloced memory, or free to free newed memory.
Did you want to delete the entire first node? Then do something like
el_sp *oldp = p;
p = p->next;
free(oldp);
Deleting a node in the middle is actually a bit easier. Use "chasing pointers" that point to the previous element (prev) and the current element (cur). If cur is the node you want to delete, simply do
prev->next->next = cur->next;
delete cur;
(Assuming you allocate nodes with new, which you should!)
I understand that sometimes professors want things their way, but it's just as important to know what would be the most straightforward way to achieve your goals.
That's what I came up with. Note that there's no manual memory management, the sorting is automated, and you can easily replace the container type. In most cases the vector will be perfectly adequate - the list should only be used if the benchmarks show that you get better performance. In most cases - you won't (contrary to what professors may tell you: trust reality over teachings).
#include <algorithm>
#include <clocale>
#include <iostream>
#include <list>
#include <vector>
struct El_Sp
{
std::string id;
};
#if 1
using El_Spy = std::vector<El_Sp>;
#else
using El_Spy = std::list<El_Sp>;
#endif
template <> struct std::less<El_Sp>
{
bool operator()(const El_Sp &l, const El_Sp &r) const
{ return l.id < r.id; }
};
std::istream &operator>>(std::istream &in, El_Sp &el_sp)
{
return in >> el_sp.id;
}
std::ostream &operator<<(std::ostream &out, const El_Sp &el_sp)
{
return out << el_sp.id;
}
std::ostream &operator<<(std::ostream &out, const El_Spy &el_spy)
{
for (auto &el : el_spy)
out << el << '\n';
return out;
}
template <typename Container, typename T, typename Pred = std::less<typename Container::value_type>>
auto insert_sorted(Container &cont, T &&item, Pred pred = {})
{
return cont.insert(
std::upper_bound(std::begin(cont), std::end(cont), std::as_const(item), pred),
std::forward<T>(item));
}
void enter_id(El_Spy &el_spy)
{
El_Sp el;
std::cin >> el;
insert_sorted(el_spy, el);
}
int main()
{
El_Spy el_spy;
std::setlocale(0, "");
size_t n = 0;
std::cout << "Введите количество идентификаторов n=";
std::cin >> n;
std::cout << "Введите идентификаторы. Нажмите Enter после каждого.\n";
while (n--)
enter_id(el_spy);
std::cout << "Идентификаторы:\n" << el_spy;
}
Example session:
Введите количество идентификаторов n=3
Введите идентификаторы. Нажмите Enter после каждого.
Алла
Дарья
Вера
Идентификаторы:
Алла
Вера
Дарья
When using std::vector, the insertion sort isn't really necessary - you could use std::sort after all identifiers have been entered:
int main()
{
std::setlocale(0, "");
size_t n = 0;
std::cout << "Введите количество идентификаторов n=";
std::cin >> n;
El_Spy el_spy(n);
std::cout << "Введите идентификаторы. Нажмите Enter после каждого.\n";
for (auto &el_sp : el_spy)
std::cin >> el_sp;
std::sort(std::begin(el_spy), std::end(el_spy), std::less<El_Sp>());
std::cout << "Идентификаторы:\n" << el_spy;
}
struct el_sp
{
char id[MAXDL];
struct el_sp* sled;
};
id is a statically allocated array. It cannot be deleted.
delete p->id;
The above is wrong. Deleting a pointer whose memory is not allocated by new leads to undefined behaviour.
You should have
delete p;
And after deletion, you should not use p. So pech_sp should be called before rather than after.
pech_sp(p);
delete p;
Im trying to write a class that stores an id and a value in an container class.
Im using an nested class as my data structure.
When im compiling the code sometimes it prints perfectly, sometimes it prints nothing and sometimes it prints half of the data then stops.
When i debug the code the same weird behavior occours, when it fails during debug it throws an error "Map.exe has triggered a breakpoint.", the Error occours in the print method when im using cout.
cmap.h
#pragma once
class CMap
{
public:
CMap();
~CMap();
CMap& Add(int id, int value);
void print() const;
private:
class container
{
public:
~container();
int container_id = 0;
int container_value = 0;
};
container* p_komp_;
int dim_ = -1;
void resize();
};
cmap.cpp
#include "cmap.h"
#include <iostream>
using namespace std;
CMap::CMap()
{
p_komp_ = new container[0];
}
CMap::~CMap()
{
p_komp_ = nullptr;
cout << "destroy cmap";
}
CMap& CMap::Add(int id, int value)
{
resize();
p_komp_[dim_].container_id = id;
p_komp_[dim_].container_value = value;
return *this;
}
void CMap::resize()
{
container* temp_array = new container[++dim_];
if (dim_ == 0)
{
temp_array[0].container_id = p_komp_[0].container_id;
temp_array[0].container_value = p_komp_[0].container_value;
}
for (unsigned i = 0; i < dim_; i++)
{
temp_array[i].container_id = p_komp_[i].container_id;
temp_array[i].container_value = p_komp_[i].container_value;
}
p_komp_ = temp_array;
}
void CMap::print() const
{
for (unsigned i = 0; i <= dim_; i++)
{
cout << p_komp_[i].container_id;
cout << p_komp_[i].container_value;
}
}
CMap::container::~container()
{
cout << "destruct container";
}
Map.cpp
#include "cmap.h"
#include <iostream>
using namespace std;
void main(void)
{
CMap m2;
m2.Add(1, 7);
m2.Add(3, 5);
m2.print();
}
These two things are a possible reason for your problem:
int dim_ = -1;
and
container* temp_array = new container[++dim_];
When you allocate, you increase dim_ from -1 to 0. That is you create a zero-sized "array", where every indexing into it will be out of bounds and lead to undefined behavior.
You also have memory leaks since you never delete[] what you new[]. I didn't look for more problems, but there probably a more.
And an "array" (created at compile-time or through new[]) will have indexes from 0 to size - 1 (inclusive). You seem to think that the "size" you provide is the top index. It's not, it's the number of elements.
It seems to me that you might need to take a few steps back, get a couple of good books to read, and almost start over.
If I have a list with N elements that represent a binary number, and each node (I named it info) is a boolean (true = 1, false = 0) what would the best way to get the decimal value using a recursive function be?
I tried using google but I only came with the formula for decimal;
1 * 2N + 0 * 2N-1 + 0 * 2N-2 + ... + 1 * 21 + 1 * 20
Right now I have a header and some basic structure for the method, according to what I understand of the problem. Also the problem says the first digit is the most significative but I don´t quite get if that is relevant.
int Lista:: ConvierteBinarioDecimal ( void ) const;
static int suma= 0;
ConvierteBinarioDecimal();
}
return suma;
}
And the Nodo (node) class.
class Nodo{
public:
bool Nodo::getInfo(void); //Retorns the content of info
Nodo* Nodo::getNext(void); //Retorns the cotent of next
private:
bool info;
Nodo *next;
};
class Lista{
Nodo *primero;
int longitud;
};
I started learning C++ last week and so far it has been way more tough than Java >_< so any help would be a godsend.
This is a recursive example:
I have to do some little changes, adding const to getInfo (there is no problem here) and adding const to getNext (you could need also and non-const version), the problem was that function ConvierteBinarioDecimal is declared as const which is good (don't have to change anything for calculating decimal representation, but this force, that the two method need to be const) you could add non-const version if needed.
The static int ConvierteBinarioDecimal(Nodo const* node) { is the recursive implementation of the conversion to decimal from binary. If it need to be recursive this is your sample code, if you could use an iterative version would be better for performance.
#include <algorithm>
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
class Nodo {
public:
bool getInfo(void) const { return info; } // Retorns the content of info
Nodo* getNext(void) const { return next; } // Retorns the cotent of next
Nodo(bool i, Nodo* n) : info(i), next(n) {}
private:
bool info;
Nodo* next;
};
class Lista {
Nodo* primero;
int longitud;
public:
int ConvierteBinarioDecimal(void) const;
Lista(Nodo* p, int l) : primero(p), longitud(l) {}
};
static int ConvierteBinarioDecimal(Nodo const* node, int decimal = 0) {
if (node == NULL) // prefer C++11 nullptr
return decimal;
decimal *= 2;
if (node->getInfo())
decimal++;
return ConvierteBinarioDecimal(node->getNext(), decimal);
}
int Lista::ConvierteBinarioDecimal(void) const {
return ::ConvierteBinarioDecimal(primero);
}
int main(int argc, char* argv[]) {
Lista list(new Nodo(true, new Nodo(false, new Nodo(true, new Nodo(true, NULL)))), 4);
std::cout << list.ConvierteBinarioDecimal() << std::endl;
return 0;
}
ConvierteBinarioDecimal()
{
int indice = longitud;
Nodo *corriente = primero; // current, begin at first node
while (corriente != null)
{
if (corriente->getInfo())
suma += 1 << --indice; // use bitshifting to add this binary digit
// alternatively, this could be "suma |= 1 << --indice;"
corriente = corriente->getNext(); // go to next node
}
}
I hope the Spanish helps as well! :)
One of the fastest ways to do this is to use bit shift operations. So, for example:
long decimal = 0;
Nodo * current = primero;
unsigned int bitcounter = 0;
while(current)
{
if(current->getInfo)
{
long temp = 1;
temp << bit counter;
decimal |= temp;
}
bitcounter++;
current = current->next;
}
Basically, what I've done here is to look at each bit in a while look. Note how I traverse the linked list. I stop when current->next is null by checking current in the following iteration. I also keep track of which bit I'm processing. For each bit, if the bit is 1, I place that into a temporary variable and shift it over to the right position using the << operator. Then I OR this temporary variable with the running total using the |= operator (OR and assignment).
Now, to do this recursively:
long ConvertBinaryToDecimal()
{
static long decimal = 0;
static Nodo * current = primero;
// Base case
if(!current)
{
current = primero;
long result = decimal;
decimal = 0;
return result;
}
else {
decimal = decimal<<1;
if(primero->getInfo())
decimal |= 1;
current = current->next;
ConvertBinaryToDecimal();
}
I want to make an implementation of stack, I found a working model on the internet, unfortunately it is based on the idea that I know the size of the stack I want to implement right away. What I want to do is be able to add segments to my stack as they are needed, because potential maximum amount of the slots required goes into 10s of thousands and from my understanding making the size set in stone (when all of it is not needed most of the time) is a huge waste of memory and loss of the execution speed of the program. I also do not want to use any complex prewritten functions in my implementation (the functions provided by STL or different libraries such as vector etc.) as I want to understand all of them more by trying to make them myself/with brief help.
struct variabl {
char *given_name;
double value;
};
variabl* variables[50000];
int c = 0;
int end_of_stack = 0;
class Stack
{
private:
int top, length;
char *z;
int index_struc = 0;
public:
Stack(int = 0);
~Stack();
char pop();
void push();
};
Stack::Stack(int size) /*
This is where the problem begins, I want to be able to allocate the size
dynamically.
*/
{
top = -1;
length = size;
z = new char[length];
}
void Stack::push()
{
++top;
z[top] = variables[index_struc]->value;
index_struc++;
}
char Stack::pop()
{
end_of_stack = 0;
if (z == 0 || top == -1)
{
end_of_stack = 1;
return NULL;
}
char top_stack = z[top];
top--;
length--;
return top_stack;
}
Stack::~Stack()
{
delete[] z;
}
I had somewhat of a idea, and tried doing
Stack stackk
//whenever I want to put another thing into stack
stackk.push = new char;
but then I didnt completely understand how will it work for my purpose, I don't think it will be fully accessible with the pop method etc because it will be a set of separate arrays/variables right? I want the implementation to remain reasonably simple so I can understand it.
Change your push function to take a parameter, rather than needing to reference variables.
To handle pushes, start with an initial length of your array z (and change z to a better variable name). When you are pushing a new value, check if the new value will mean that the size of your array is too small (by comparing length and top). If it will exceed the current size, allocate a bigger array and copy the values from z to the new array, free up z, and make z point to the new array.
Here you have a simple implementation without the need of reallocating arrays. It uses the auxiliary class Node, that holds a value, and a pointer to another Node (that is set to NULL to indicate the end of the stack).
main() tests the stack by reading commands of the form
p c: push c to the stack
g: print top of stack and pop
#include <cstdlib>
#include <iostream>
using namespace std;
class Node {
private:
char c;
Node *next;
public:
Node(char cc, Node *nnext){
c = cc;
next = nnext;
}
char getChar(){
return c;
}
Node *getNext(){
return next;
}
~Node(){}
};
class Stack {
private:
Node *start;
public:
Stack(){
start = NULL;
}
void push(char c){
start = new Node(c, start);
}
char pop(){
if(start == NULL){
//Handle error
cerr << "pop on empty stack" << endl;
exit(1);
}
else {
char r = (*start).getChar();
Node* newstart = (*start).getNext();
delete start;
start = newstart;
return r;
}
}
bool empty(){
return start == NULL;
}
};
int main(){
char c, k;
Stack st;
while(cin>>c){
switch(c){
case 'p':
cin >> k;
st.push(k);
break;
case 'g':
cout << st.pop()<<endl;
break;
}
}
return 0;
}
This program I am writing will use a special implementation of a stack as a linked structure. An encoded message input my the user will be parsed and decoded using the stack. What I have written compiles find and runs without crashing. The program asks the user for the string to be decoded. However, the encoded message is not decoded with result printed on the screen. I can't figure out why my program isn't decoding and printing the user's input. Any help is greatly appreciated. Thanks.
My header file :
#ifndef DECODER_H
#define DECODER_H
#include <iostream>
#include <stdlib.h>
using namespace std;
// ---------------------------
// Structure which will serve
// as the link on the stack.
// ---------------------------
struct StackNode {
char ch;
StackNode* next;
};
// -------------------------------
// Class which will contains the
// functions for appropriate use
// of the stack.
// -------------------------------
class Decoder
{
private:
StackNode* top;
public:
Decoder();
~Decoder();
int EmptyStack();
int FullStack();
void Push(char ch);
char Pop();
void Decode(char *encMsg, char *decMsg);
};
#ifndef FALSE
#define FALSE (0)
#endif
#ifndef TRUE
#define TRUE (!FALSE)
#endif
#endif // End of stack header.
My .cpp file:
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "Decoder.h"
// ------------------------------
// Function: Decoder()
//
// Purpose: Class constructor.
// ------------------------------
Decoder::Decoder()
{
top = NULL;
}
// ------------------------------
// Function: Decoder()
//
// Purpose: Class destructor.
// ------------------------------
Decoder::~Decoder()
{
// TODO
// Destroy anything remaining in the stack
}
// -----------------------------------
// FullStack()
//
// Return TRUE if the stack is full.
// -----------------------------------
int Decoder::FullStack()
{
return TRUE;
}
// -----------------------------------
// EmptyStack()
//
// Return TRUE if the stack is empty
// -----------------------------------
int Decoder::EmptyStack()
{
return (top == NULL);
}
// ------------------------------------------------
// Function: void Push(char ch)
//
// Purpose: Dynamically creates a structure of type
// StackNode (see Decoder.h), stores the character
// in the structure and pushes the structure onto
// the stack.
// ------------------------------------------------
void Decoder::Push(char ch)
{
// Make a new node whose reference is
// the existing list
StackNode* newNode = new (StackNode);
newNode->ch = ch;
// newNode->next = NULL;
if (top == NULL)
top = newNode; // top points to new node
else
{
newNode->next = top;
top = newNode;
}
}
// --------------------------------------------------
// Function: char Pop()
//
// Purpose: Remove (pop) the top node from the stack,
// copy the character, from this node, delete and
// return the character.
// --------------------------------------------------
char Decoder::Pop()
{
StackNode* temp;
char ch;
if (!EmptyStack())
{
ch = top->ch;
temp = top;
top = top->next;
delete(temp);
return ch;
}
else {
cout << "Warning: Overuse of Pop()" << endl;
return '\0';
}
}
// ----------------------------------------------------
// Function: void Decode(char* encMsg, char* decMsg)
//
// Purpose: Parse and decode the message stored in the
// character array encMsg using the stack functions
// and return the decoded message in the char array
// decMsg.
// ----------------------------------------------------
void Decoder::Decode(char* encMsg, char* decMsg)
{
int StackCount = 0;
char num[2] = " ";
for (int i = 0; i < strlen(encMsg); i++)
{
// check whether 1 is an even number of input
if ((encMsg[i] == '1') && (encMsg[i-1] != '2')) // every other index will be a command number
{
Push(encMsg[i+1]);
StackCount++;
}
if (encMsg[i] == '2' && ((encMsg[i+1] >= '0') && (encMsg[i+1 ] <= '9'))) // every other index will be a command number
{
num[0] = encMsg[i+1];
// pop as many as the argument states to pop
for (int j = 0; j < atoi(num); j++)
{
Pop();
StackCount--;
}
}
}
//cout << StackCount << endl;
// Place the remaining characters from the stack into decMsg
int i;
for (i = 0; i < StackCount; i++)
{
decMsg[i] = Pop();
}
decMsg[i] = '\0';
return;
}
My Main .cpp:
#include <iostream>
#include <string>
#include "Decoder.h"
using namespace std;
int main (void)
{
char quit[] = "QUIT";
char en[2048];
char dec[512];
Decoder d;
do {
cout << "\nEnter a message to be decoded" << endl;
cin.getline(en, 1024);
d.Decode(en, dec);
cout << dec << endl;
} while (strcmp(en,quit) != 0);
return 0;
}
This line of code
if ((encMsg[i] == '1') && (encMsg[i-1] != '2'))
Maybe a problem there when i is zero.
It is guaranteed to try endMsg[-1] every time since i=0 is followed immediately by encMsg[i-1] which is always checked since && is present.
for (int i = 0; i < strlen(encMsg); i++)
{
// check whether 1 is an even number of input
if ((encMsg[i] == '1') && (encMsg[i-1] != '2')) // every other index will be a command number
{