#include <stdio.h>
void erase(string key){
int index=hash_function(key);
Node<T>*temp=table[index];
while(temp!=NULL){
if(temp->key==key){
table[index]=temp->next;
delete temp;
return;
else{
temp=temp->next;
}
}
}
can someone please help i am getting error while running the function
Related
I am trying to learn how to use priority_queue vs queue. I have this code for a priority_queue that's supposed to get input from users. The input is a chore and the priority number of the chore. They can enter as many as they want and it's supposed to output them in order. My problem is that it's not outputting them in order. I'm not sure if it's in my main or in my overloaded < operator function. Here is my Chore.h code:
#include<iostream>
#include<fstream>
using namespace std;
//#ifndef CHORE_H;
//#define CHORE_H;
class Chore{
public:
Chore(){priorityNum=0;choreName="";}
std::string getName (Chore c1)const{return choreName;}
int getPriorityNum(Chore c1)const{return priorityNum;}
bool operator <(const Chore c1)const;
std::ostream& output(std::ostream& cout,const Chore c)const;
std::istream& input(std::istream& cin);
private:
std::string choreName;
int priorityNum;
};
//#endif
Here's my Chore.cc code:
#include"Chore.h"
using namespace std;
bool Chore::operator <(const Chore c1)const{
Chore c2;
int c1Num=c1.getPriorityNum(c1);
int c2Num=c2.getPriorityNum(c2);
return c1Num<c2Num;
/*if(c1Num<c2Num)
return true;
else if(c1Num==c2Num)
return true;
else
return false;*/
}
std::ostream& Chore::output(std::ostream& cout,const Chore c)const{
cout<<endl<<"Chore: "<<getName(c)<<endl;
cout<<"chore Priority: "<<getPriorityNum(c);
}
std::istream& Chore::input(std::istream& cin){
cout<<endl<< "Enter chore name:";
cin >>choreName;
cout<<endl<<"Enter priority number:";
cin >>priorityNum;
}
my main is below:
#include<queue>
#include<iostream>
#include"Chore.h"
using namespace std;
int main(){
std::priority_queue<Chore>myChores;
Chore tmp;
bool enterAnother=true;
//input loop
while(enterAnother){
char c;//checks if they want to continue
tmp.input(cin);
myChores.push(tmp);
cout<<endl<<"Want to enter another chore?(y for yes, n for no)";
cin >>c;
if(c=='y'|| c=='Y')
enterAnother=true;
else
enterAnother=false;
}
//output loop
while(!myChores.empty()){
tmp = myChores.top();
myChores.top().output(cout,tmp);
myChores.pop();
}
}
Any help would be greatly appreciated.
All I do is construct a BNode object. The debugger says that the constructor is causing a segmentation fault. Does anyone know what the problem is here?
All I do is construct a BNode object. The debugger says that the constructor is causing a segmentation fault. Does anyone know what the problem is here?
#ifndef BTree_H
#define BTree_H
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
#include <sstream>
#include <cmath>
using namespace std;
template <typename T>
class BNode
{
public:
BNode();
BNode(int M);
~BNode();
int keyCount;
BNode *pointers;
T *keys;
};
template<typename T>
BNode<T>::BNode()
{
}
template<typename T>
BNode<T>::BNode(int M)
{
pointers = new BNode<T>[M];
keys = new T[M - 1];
}
template<typename T>
BNode<T>::~BNode()
{
delete[] pointers;
delete[] keys;
}
#endif
int main()
{
BNode<int> obj(5);
return 0;
}
You are deleting an array of pointers and keys, whereas you never defined these to be arrays.
Both of these are pointers.
You need to be freeing memory from the pointers, not arrays.
Try this:-
delete myPointer;
myPointer = NULL;
NOTE: If you're using C++, read about smart pointers. They'll come in handy!
A smart pointer clears the memory if the pointer gets out of scope. I wanted to adapt this to a file descriptor, like a socket. There you need a user defined deleter, because close() is the function to free the file descriptor (fd) resources.
I found this useful page, unfortunately, most approaches did not work for me. Below is a working solution I found up to now, which is a little nasty. Because uniqu_ptr expects a pointer I created int *fd to store the fd value, therefore, I had to close(*fd) and delete fd in my custom deleter.
(1) Is there a better way?
Options A and B, which are based on the hints provided by the mentioned web page, are much nicer but causing odd compiler errors.
(2) Does anyone know how to correctly use these alternatives?
I'm using Qt Creator 3.0.1 with CONFIG += c++11 option and gcc version 4.8.2
#include "ccommhandler.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <memory>
#include <qdebug.h>
//for Option A and B
struct CloseHandleDeleter {
typedef int pointer;
void operator()(int handle) const
{
}
};
//custom deleter, working
class MyComplexDeleter
{
public:
MyComplexDeleter() {}
void operator()(int* ptr) const
{
qDebug() << "Deleting ";
close(*ptr);
delete ptr;
}
};
CCommHandler::CCommHandler()
{
//Option A doesn't work
//std::unique_ptr<int, CloseHandleDeleter> file( socket(AF_INET, SOCK_STREAM, 0) );
//Option B doesn't work
//std::unique_ptr<int, int()(int)> filePtr( socket(AF_INET, SOCK_STREAM, 0) , close);
MyComplexDeleter deleter;
int *fd = new int;
*fd = socket(AF_INET, SOCK_STREAM, 0);
std::unique_ptr<int, MyComplexDeleter> p( fd , deleter);
}
Edit:
The posted answer by Nevin is right, it solves my initial problem.
The comment of learnvst caused to rethink my problem, and I have to say I may made it much more complex than needed, because the following simple class should also solve my problem of auto-free the memory of a resource or as in my case, to close the file descriptor:
class SocketHandler
{
int _fd;
public:
SocketHandler(int FD):_fd(FD){}
~SocketHandler() { if(_fd!=-1) close(_fd); }
operator int() const { return _fd; }
};
Because fd isn't a pointer, I wouldn't try to pigeonhole it into unique_ptr. Instead, create a custom class whose interface is based on unique_ptr, as in (caution: totally untested):
class unique_fd
{
public:
constexpr unique_fd() noexcept = default;
explicit unique_fd(int fd) noexcept : fd_(fd) {}
unique_fd(unique_fd&& u) noexcept : fd_(u.fd_) { u.fd_ = -1; }
~unique_fd() { if (-1 != fd_) ::close(fd_); }
unique_fd& operator=(unique_fd&& u) noexcept { reset(u.release()); return *this; }
int get() const noexcept { return fd_; }
operator int() const noexcept { return fd_; }
int release() noexcept { int fd = fd_; fd_ = -1; return fd; }
void reset(int fd = -1) noexcept { unique_fd(fd).swap(*this); }
void swap(unique_fd& u) noexcept { std::swap(fd_, u.fd_); }
unique_fd(const unique_fd&) = delete;
unique_fd& operator=(const unique_fd&) = delete;
// in the global namespace to override ::close(int)
friend int close(unique_fd& u) noexcept { int closed = ::close(u.fd_); u.fd_ = -1; return closed; }
private:
int fd_ = -1;
};
This is my CArray.h:
struct CComplexNumber
{
int rpart, ipart;
};
class CArray
{
protected:
CComplexNumber* a;
int size;
public:
CArray* next;
void updateElement(int rp, int ip);
};
And my CArray.cpp:
#include <iostream>
using namespace std;
#include "CArray.h"
void CArray::updateElement(int rp, int ip)
{
a->rpart = rp;
a->ipart = ip;
}
And this is a line in main.cpp
CArray* first = new CArray();
CArray* cur = first;
cur->updateElement(1,2); //=> Here is the line that causes the bug
When I debugged, the cmd crassed. I have to exit and debug each line. When I reached the line above, the compiler stop and appear:
Unhandled exception at 0x00b1155b in Section03.exe: 0xC0000005:
Access violation writing location 0x00000000.
Please fix my code and explain why I can't replace the rpart with rp?
I'd like to call this code from my program using LLVM:
#include <string>
#include <iostream>
extern "C" void hello() {
std::cout << "hello" << std::endl;
}
class Hello {
public:
Hello() {
std::cout <<"Hello::Hello()" << std::endl;
};
int hello() {
std::cout<< "Hello::hello()" << std::endl;
return 99;
};
};
I compiled this code to llvm byte code using clang++ -emit-llvm -c -o hello.bc hello.cpp and then I want to call it from this program:
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/GenericValue.h>
#include <llvm/ExecutionEngine/JIT.h>
#include <llvm/LLVMContext.h>
#include <llvm/Module.h>
#include <llvm/Target/TargetSelect.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/IRReader.h>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
using namespace llvm;
void callFunction(string file, string function) {
InitializeNativeTarget();
LLVMContext context;
string error;
MemoryBuffer* buff = MemoryBuffer::getFile(file);
assert(buff);
Module* m = getLazyBitcodeModule(buff, context, &error);
ExecutionEngine* engine = ExecutionEngine::create(m);
Function* func = m->getFunction(function);
vector<GenericValue> args(0);
engine->runFunction(func, args);
func = m->getFunction("Hello::Hello");
engine->runFunction(func, args);
}
int main() {
callFunction("hello.bc", "hello");
}
(compiled using g++ -g main.cpp 'llvm-config --cppflags --ldflags --libs core jit native bitreader')
I can call the hello() function without any problems.
My question is: how can I create a new instance of the Hello class using LLVM?
I'm getting a segmentation fault when I call Hello::Hello()
Thanks for any hints!!
Manuel
Running clang++ -emit-llvm on the given source won't emit Hello::Hello, and m->getFunction("Hello::Hello") wouldn't find it even if it were emitted. I would guess it's crashing because func is null.
Trying to directly call functions which aren't extern "C" from the LLVM JIT is generally not recommended... I'd suggest writing a wrapper like the following, and compiling it with clang (or using the clang API, depending on what you're doing):
extern "C" Hello* Hello_construct() {
return new Hello;
}