I am having some trouble figuring out why i get Unhandled exception at 0x003DBD00 in Project10.exe: 0xC0000005: Access violation reading location 0xCDCDCDE5. It seems to pop up at a string assignment operator similar to this: std::string x = std::move(std::string y); Please see code below for more information.
The call starts from here: *members += NVPair<std::string, std::string>(n,v);
FYI members is members = new List <NVPair <std::string, std::string>, DATA_MEMBERS_PER_OBJECT>(); where DATA_MEMBERS_PER_OBJECT is 4
List class declarations:
auto list = new List <T, OBJECTS_PER_JSON_FILE>(); //Not used in this process
auto members = new List <NVPair <std::string, std::string>, DATA_MEMBERS_PER_OBJECT>();
Operator call:
n = trim(trim(getName(line)),'"'); //Trim removes extra characters from line and returns Name
v = trim(trim(getValue(line)),'"'); //or Value as a std::string
*members += NVPair<std::string, std::string>(n,v);
List Class
//List.h
#include <iostream>
#include <string>
template <typename T, unsigned int n>
class List{
T *Array[n];
size_t elements;
T dummy;
public:
List(){ elements = 0u; }
size_t size() const { return elements; }
const T& operator[](unsigned int i) const{...}
void operator+=(const T& add){ //adds a copy to the element
*Array[elements] = add;
elements++;
}
void operator+=(T&& add){ //moves element
*Array[elements] = std::move(add);
elements++;
}
Name Value pair Class
//NVPair.h
#include <iostream>
#include <string>
template <typename T, typename B>
class NVPair{
T Name;
B Value;
public:
NVPair(){ Name = ""; Value = ""; }
NVPair(T n, B v){ Name = n; Value = v; }
T name() const { return Name; }
B value() const{ return Value; }
NVPair& operator=(const NVPair& add){
Name = add.Name;
Value = add.Value;
return *this; }
NVPair& operator=( NVPair&& add){
Name = std::move(add.Name);
Value = std::move(add.Value);
return *this; }
};
I have been trying to debug it and it fails and goes into xstring at Name = std::move(add.Name) inside the NVPair =function.
Any help is much appreciated!
Edit: Looks like I was being very vague (Sorry first time). So The main program reads information from a file that contains Name and value pairs. It then creates a List object that is an array of pointers to Name value pair objects. So T * Array[n] is an array of T pointers. The purpose is to store information from a file that looks like this:
{
"Name" : "Cat"
"type" : "animal"
}
and so on...
The array member inside your List class is never initialized, but you are referencing it in the += operator. Also, I presume you want an array of NVPairs not array of NVPair pointers
Try doing something along the lines of:
template <typename T>
class List
{
T Array[n];
size_t size;
size_t elements;
T dummy;
public:
List(size_t maxSize) : size(maxSize), elements(0)
{
Array = new T[size];
}
void operator+=(T&& add)
{ //moves element
//This was where it was failing because Array was not defined
Array[elements] = std::move(add);
elements++;
}
//The rest of the class
}
The constructor will create an array of NVPairs, then you assign the values as normal. Notice that you need 2 variables, one for the size, one for the number of elements currently in the array.
Related
Suppose I have a class called Entry:
template <typename K, typename V>
class Entry {
public:
Entry(K const &key, V const &val, size_t const hash_val) :
key(key), val(val), hash_val(hash_val), empty(false){
}
K getKey() const {
return key;
}
V getValue() const {
return val;
}
size_t getHash() const {
return hash_val;
}
bool isEmpty() const{
return empty;
}
private:
// key-value pair
K key;
V val;
// Store hash for reallocation
size_t hash_val;
// Store empty state
bool empty;
};
Then I create an array of objects
Entry<K, V>** entries = new Entry<K, V> *[100];
If I call entries[0]->isEmpty(), I get a segmentation fault. This makes sense to me, since I haven't actually instantiated a new object in memory. However, I want to be able to check whether a slot in the array actually points to a valid object. Currently, I've been setting each pointer to nullptr so I can check for equality later, but I was wondering if there was a better way.
You want optional. It's always either a valid object, or in an "empty" state.
#include <cstdio>
#include <optional>
#include <vector>
struct Foo {
int bar;
};
int main() {
std::vector<std::optional<Foo>> vfoo{
Foo{1}, std::nullopt, Foo{2}, Foo{3}, std::nullopt,
};
for (auto const& foo : vfoo) {
if (!foo)
std::puts("Not Initialized");
else
std::printf("Foo{%d}\n", foo->bar);
}
}
There is no way to check if a pointer has been initialized, because reading from an unitialized pointer is undefined behavior:
int* p;
if (p == something) ... // undefined behavior
You can initialize the pointer with nullptr and check for that:
int* p = nullptr;
if (p == nullptr) ...
However, then you are initializing the pointer.
For a dynamic array of Entry<K, V>* you can use a std::vector<Entry<K,V>*>. A container that can be used analogous to an array with empty slots is a std::unordered_map:
std::unordered_map<size_t,Entry<K,V>*> map;
Entry<K,V>* ptr = get_pointer_from_somewhere();
size_t index = 42;
if ( map.find(index) == map.end() ) {
// pointer was not initialized (actually pointer is not yet in the map)
map.insert( { index, ptr }); // now it is
}
Actually insert alone will tell you already if the element with key index was present in the map before.
I am trying create my own vector, I am at the beginning, and when compile e execute the code, i get "Program not responding". This is the code:
struct X
{
X(){};
~X(){};
int v1, v2, v3;
};
template<typename T>
class Vector
{
public:
// constructors
Vector();
Vector(unsigned s);
virtual ~Vector();
// overloaded operators
T operator[](unsigned index);
// others
void clear();
void add(T value);
unsigned getSize();
bool isEmpty();
private:
// pointer to first item of memory block
T* first;
unsigned size;
};
template<typename T>
Vector<T>::Vector()
{
first = NULL;
size = 0;
}
template<typename T>
Vector<T>::Vector(unsigned s)
{
size = s;
first = new T[s];
};
template<typename T>
Vector<T>::~Vector()
{
clear();
}
template<typename T>
void Vector<T>::clear()
{
for(unsigned i = size ; i > 0 ; i--)
delete &first[i];
first = NULL;
}
template<typename T>
void Vector<T>::add(T value)
{
T* temp = new T[size + 1]; // error happens here
// copy data to new location
for(unsigned i = 0 ; i < size ; i++)
temp[i] = first[i];
// delete older data
clear();
// add the new value in last index
temp[size + 1] = value;
// update the pointer
first = temp;
size++;
}
template<typename T>
T Vector<T>::operator[](unsigned index)
{
return first[index];
}
template<typename T>
unsigned Vector<T>::getSize()
{
return size;
}
template<typename T>
bool Vector<T>::isEmpty()
{
return first == NULL;
}
int main(int argc, char* args[])
{
Vector<X> anything;
X thing;
anything.add(thing);
anything.add(thing);
anything.add(thing); // if remove this line, program work fine.
}
As I commented, error happens in T* temp = new T[size + 1];.
If i define the value of v1, v2, v3 of X class, e.g. X() : v1(0), v2(0), v3(0) { }, the program works correctly.
If i change the type, e.g., Vector of int, he works perfectly.
If put X class in std::vector, work fine too.
Other comments are also accepted.
Can someone helpme?
Your description of the problem is incredibly vague, but I can point out problems with your code:
No vector copy constructor (causes double-deletes and crashes)
No vector copy assignment (causes double-deletes and crashes)
clear is incorrectly calling delete (causes crashes and corruption) (you should match your single new of an array with a single delete of the array. Don't loop over elements.
add is writing past the end of the array (causes crashes and corruption)
add is not exception safe
You have to fix at least the first four. The third and fourth are probably the causes of your hang.
You have a buffer overflow occurring.
T* temp = new T[size + 1]; // When size is 0, you allocate 1 space.
You then assign to the temp array, but in location temp[1], which isn't a valid location because your array has only 1 element. This is undefined behavior, and that this point, your program is free to continue however it chooses. In this case, it seems to loop indefinitely.
// add the new value in last index
temp[size + 1] = value; // When size is zero, your array is length '1', but
// you are accessing temp[1] which is outside the
// bounds of your allocated memory.
I am trying to make a basic HashMap. I am checking to see if an element exists at an index before inserting it there. When I insert my first element, it says that an element already exists at that position. I have gone through the debugger, and all of my values are as expected, except for map[hash]. I am anticipating a nullptr, but it is not coming. map[hash] has the following value:
- map[hash] 0xcdcdcdcd {key=??? value={...} next_element=??? } HashElement *
Can someone please explain to me what I am misunderstanding here? The unexpected result is on line 21 of HashMap.cpp. Here is the relevant code:
HashMap.h
#pragma once
#include <string>
#include "HashElement.h"
class HashMap
{
private:
HashElement **map;
int size;
public:
HashMap(int);
~HashMap();
int GetHash(int);
void Put(int, std::string);
};
HashMap.cpp
#include "HashMap.h"
#include <string>
HashMap::HashMap(int _size)
{
size = _size;
map = new HashElement*[size];
}
HashMap::~HashMap()
{
}
int HashMap::GetHash(int _key){
return _key % size;
}
void HashMap::Put(int _key, std::string _value){
int hash = GetHash(_key);
if (!map[hash]){ //Anticipated to be nullptr on first Put, but it skips to else
map[hash] = new HashElement(_key, _value);
}
else{
HashElement *lastElement = map[hash];
while (lastElement->next_element){
lastElement = lastElement->next_element;
}
lastElement->next_element = new HashElement(_key, _value);
}
}
HashElement.h
#pragma once
#include <string>
class HashElement
{
private:
int key;
std::string value;
public:
HashElement(int, std::string);
~HashElement();
HashElement *next_element;
int get_key();
std::string get_value();
};
HashElement.cpp
#include "HashElement.h"
HashElement::HashElement(int _key, std::string _value)
{
key = _key;
value = _value;
}
HashElement::~HashElement()
{
}
int HashElement::get_key(){
return key;
}
std::string HashElement::get_value(){
return value;
}
map[hash] is not a nullptr because you haven't initialized it to such.
map = new HashElement*[size];
Each element in the map array will have a random value after that line.
To fix this and initialize all elements to be nullptr:
map = new HashElement*[size]();
^^
map = new HashElement*[size];
Here, you are instantiating an array of size pointers, on the heap. As I understand your question, you are assuming that all of the instantiated pointers, in this new array, will be nullptr.
That is not the case. For "plain old data", or POD, its contents are not initialized by default. You'll have to explicitly initialize them:
for (size_t i=0; i<size; ++i)
map[i]=0;
... in the constructor
How might I initialise a std::vector from an array of structs, where the struct contains a union of different types. In other words, the array is used to store a number of values of a specific type, which can be int, char* etc.
This is my solution so far but I'm looking for a better approach:
The convert function returns a vector<int> if it stores ints or a vector<std::string> if it stores char*.
The Value type below is a struct containing a union called value. The Container class below points to a buffer of such Values.
// union member getter
class Getter
{
public:
void operator()(int8_t& i, const Value& value)
{
i = value.value.i;
}
void operator()(std::string& s, const Value& value)
{
s = std::string(value.value.s);
}
...
};
template<class T>
std::vector<T> convert(Container* container)
{
std::vector<T> c;
c.reserve(container->nrOfValues);
Getter g;
for(int i=0;i<container->nrOfValues;i++)
{
T value;
g(value, container->values[i]);
c.push_back(value);
}
return c;
}
Your problem is the union gives a different name to each value, which causes the need for a function that converts a name to a type, such as Getter::operator() returning a type and getting a named member of the union.
There isn't much you can do with this. You can save a variable declaration and a copy/string constructor on each item, but that's about it.
If you can't modify the original struct, you could initialize the vector with a length set of default value (which must be passed in), then iterate through using the getter as:
vector<T> v(length, defaultValue);
typename vector<T>::iterator iter = vec.begin();
for(int index = 0; *iter != vec.end() && index < length; ++iter, ++index) {
converter(*iter, array[index]);
}
Notice that this starts getting cumbersome in iterating the index and the iterator and verifying both are still valid in case of an accident...
If you can modify the original struct:
class Ugly { // or struct, it doesn't matter
public:
union {
char* s;
int i;
} value;
Ugly(char* s) {
value.s = s;
}
Ugly (const int& i) {
value.i = i;
}
operator std::string() const {
return std::string(value.s);
}
operator int() const {
return value.i;
}
};
Then your for loop becomes:
for(int i=0;i<container->nrOfValues;i++)
{
c.push_back(container->values[i]);
}
Note: You might create the vector and pass it as an argument to the copy function since it involves copying the data over during the return.
If you like some template magic, you could do it slightly different way:
// Source union to get data from
union U
{
int i;
char* s;
double d;
};
// Conversion type template function (declared only)
template <class T> T convert(const U& i_u);
// Macro for template specializations definition
#define FIELD_CONV(SrcType, DestField)\
template <> SrcType convert(const U& i_u)\
{ auto p = &DestField; return i_u.*p; }
// Defining conversions: source type -> union field to get data from
FIELD_CONV(int, U::i)
FIELD_CONV(std::string, U::s)
FIELD_CONV(double, U::d)
// Get rid of macro that not needed any more - just for macro haters ;-)
#undef FIELD_CONV
// Usage
template<class T> std::vector<T> convert(Container* container)
{
std::vector<T> c;
c.reserve(container->nrOfValues);
for(int i = 0; i < container->nrOfValues; ++i)
c.push_back(convert<T>(container->values[i]));
return c;
}
The advantage of this approach - it is short, simple and easy to extend. When you add new field to union you just write another FIELD_CONV() definition.
Compiled example is here.
I have a custom class as a key in a map. When I try to insert an item into the map, the program terminates. There has to be a problem with the creation of the key.
class MyKey {
char* data;
bool operator<(const MyKey& s) const {
for(int i = 0; i < (int)(sizeof(data)/sizeof(char)); i++) {
if(data[i] > s.data[i])
return false;
}
return true;
}
}
map<MyKey, char>* map = new map<MyKey, char>;
MyKey* key = new MyKey(...);
map->insert(make_pair(*key, '0'));
The program terminates at the insert.
You can't determine the size of an array from the pointer alone like you're attempting to-do in the for-loop of your operator< function ... You will have to, at some point, pass in the size of the array that is being pointed to by data so that you don't overflow the bounds of the array data is pointing to. Since data is a pointer, sizeof(data) simply returns the size of a pointer on your platform, not the size of the array being pointed to by data.
For C++, rather than using an allocated array, you should possibily use a STL container that you can directly query for the size of the container object ... this could include std::string if it's string-data, or std::vector<unsigned char> if it's just a bunch of binary bytes.
The following works and prints A.
#include <iostream>
#include <map>
using namespace std;
class Key
{
public:
Key(int x):data(x) {}
bool operator<(const Key &k) const { return(data < k.data); }
private:
int data;
};
int main()
{
Key myKey(10);
map<Key, char> m;
m.insert(make_pair(myKey, 'A'));
map<Key, char>::iterator it = m.find(myKey);
if (it != m.end())
{
cout << (*it).second << endl;
}
}
From your example code, the operator < would not be called because you only insert one element in the map. And you said you don't implement a copy constructor. So following code would be a problem:
class MyKey {
public:
MyKey()
{
data = new char[10];
}
~MyKey()
{
delete data;
}
private:
char* data;
};