My task is to construct a compiler which lexes, parses and analyses the resultant Abstract Syntax Tree to ensure type matching, avoid duplicate declaration etc.
It was instructed for us to construct a Symbol Table, which holds a map of variables and their types for each scope. I opted for a vector of maps. I preferred this over a stack since I can iterate over it when checking for variables at any scope.
I constructed Push, Pop, Lookup and Insert operations for this structure as shown below and my idea was to hold a reference to the latest map in the vector and add variables to it. As a new scope is entered a push operation is carried out creating a new map in the vector within which to store the arrays.
When a scope is exited a Pop operation is done to remove the map at the end of the vector and acquire the previous map which is now at the back of the vector.
Via debugging I've noticed the vector is simply holding no map details and working by reference seems to be doing nothing to update the vector that's supposed to be holding this map. How do I correctly reference a map inside a vector and maintain this structure?
Symbol Table:
struct SymbolTable {
// Stack defining scopes holding identifier / type details
std::vector<std::map<std::string,std::string>> _scopeVector;
// Tracks current working stack
std::map<std::string,std::string> _currentMap;
SymbolTable() = default;
void Push() {
std::map<std::string,std::string> *_tempMap;
_tempMap = new std::map<std::string,std::string>();
_scopeVector.push_back(*_tempMap);
_currentMap = _scopeVector.back();
}
void Insert(std::string p_name, std::string p_type) {
_currentMap.insert(std::make_pair(p_name,p_type));
}
// Returns type if found, empty if not
std::string Lookup (std::string p_name) {
for (int i = 0; i < _scopeVector.size(); i++) {
if (_scopeVector[i].find(p_name) == _scopeVector[i].end()) {
// No match yet
} else {
return _scopeVector[i].find(p_name)->first; // return var name
}
}
std::cerr << "Type name " << p_name << " not found in all of stack" << std::endl;
return "";
}
void Pop () {
_scopeVector.pop_back();
_currentMap = _scopeVector.back();
}
};
SymbolTable *ST;
Class constructor which sets up the Symbol Table:
SemanticAnalysisVisitor() {
ST = new SymbolTable();
ST->Push();
}
Debugger image of an empty vector but populated map
As for your problem (I think) the statement
_currentMap = _scopeVector.back();
copies the map from the vector.
_currentMap will always be a separate and distinct map, totally unrelated from whatever is in the vector.
It seems you want to use references, but that's not possible in this case (unless you do a redesign).
You could solve this problem (and the memory leak I mentioned in a comment) by having a vector of (smart) pointers to the maps, and make _currentMap a (smart) pointer as well.
Related
I have a playlist class which contains a vector of song pointers.
class Playlist {
public:
Playlist();
Playlist(string);
vector<Song*> GetSongList() const;
private:
vector<Song*> songs;
string name;
};
The function GetSongList() is as follows:
vector<Song*> Playlist::GetSongList() const {
return songs;
}
In my main method I have a vector of playlist pointers: vector<Playlist*> playlists
I want to remove a song pointer from a specific playlist, but I'm having trouble. This is my current code to remove the indicated song:
Playlist* desiredPlaylist = playlists.at(index);
vector<Song*> temporarySongList = desiredPlaylist->GetSongList();
cout << "Pick a song index number to delete: ";
cin >> index;
temporarySongList.erase(tempSongList.begin() + index);
When I erase the song from the vector and re-output the contents of playlists.at(index).GetSongList() the song is not removed. I think the reason why is that calling GetSongList() does not retrieve the actual song vector, but just returns a copy, so I'm not altering the original vector. How do I directly remove the song from the original?
Use a member function remove_playlist to remove the song from the playlist. std::list (in place of vector) is recommended since frequent delete, move and insert is required within the playlist. Also use smart-pointer to avoid memory leaks like this std::list<std::shared_ptr<Song>>
void Playlist::remove_playlist(int index)
{
if( index < songs.size() )
{
auto v = songs.at(index);
songs.erase(std::remove(songs.begin(), songs.end(), v), songs.end());
}
}
You are right, the problem is caused by returning a copy.
You can either return a pointer
vector<Song*>* Playlist::GetSongList() {
return &songs;
}
or a reference
vector<Song*>& Playlist::GetSongList() {
return songs;
}
to your playlist.
A pointer is preferrable when it may happen, that there is no song list available and you thus sometimes have to return nullptr. So not in your example, because the member playlist is always available.
In most other cases, returning a reference is preferrable. Note that the method is not marked as const, because accesses to the returned vector<Song*>& alter the Playlist instance.
Another technique is to not return the member at all, but instead use a delegate to change the member.
void Playlist::ChangeSongList( const std::function<void(vector<Song*>&)>& fn ) {
fn(songs);
}
This has the benefit that the class which supplies the member can lock the access to the member in a threaded environment and is better informed when the member is changed (e.g. for debugging purposes - ChangeSongList could dump the contents before and after calling fn())
Anyway, returning a copy has often also performance problems and thus is often not preferrable. If you want to return a const member, you should use a const reference instead:
const vector<Song*>& Playlist::GetSongList() const {
return songs;
}
Please note that the answer seccpur gave is the most preferrable option in everyday life - a class should and usually takes care of its members itself, and don't let some outside code handle it. But this answer doesn't describe the difference in returning copies, pointers and references.
Quote
Playlist* desiredPlaylist = playlists.at(index);
vector<Song*> temporarySongList = desiredPlaylist->GetSongList();
cout << "Pick a song index number to delete: ";
cin >> index;
temporarySongList.erase(tempSongList.begin() + index);
You are creating a copy by doing this
vector<Song*> temporarySongList = desiredPlaylist->GetSongList();
So you are erasing from your local copy.
Consider changing
vector<Song*> GetSongList() const { return songs };
to
vector<Song*>& GetSongList() { return songs } ;
Sorry I can't paste the full code but I will try my best to explain the problem. I have a vector of structure. I am passing the vector to a function which fills the vector with structure and returns the vector. But when I try to access the vector elements, I get only the last element inserted. I think the problem is somewhere that I am storing the address of the structure and hence vector only retains the last value but i am not sure how to correct this.
Here is my structure:
struct NA
{ element1;
element2;
};
Here is how I pass my vector after declaring it:
Vector<NA> del;
func(del);
Here is my function: ( q is a variable having results from a stored procedure
func(Vector<NA> &dels)
{
NA& del(*new NA);
while(q.nextquery())
{
while(q.nextrow())
{
q.bind(del.element1);
q.bind(del.element2);
dels.insert(&del);
}
return dels.entries()
}
NA& del(*new NA);
while(q.nextquery())
dels.insert(&del);
Obviously, you are creating only one del object, but you insert it many times in the same vector. At the end, all you vector's entries will point to the same object.
What you want is probably to create a new del object for each entry in your vector. therefore, put the creation statement inside the loop.
func(Vector<NA> &dels)
{
while(q.nextquery())
{
while(q.nextrow())
{
NA& del(*new NA); // <-- Here
q.bind(del.element1);
q.bind(del.element2);
dels.insert(&del);
}
return dels.entries()
}
func() is pushing multiple copies of a single pointer to a single dynamically allocated object into the vector. Each loop iteration is modifying that same object, so every entry in the vector will end up pointing to the data of the last query record that was read.
Try something more like this instead:
func(Vector<NA> &dels) {
while(q.nextquery()) {
while(q.nextrow()) {
NA *del = new NA;
q.bind(del->element1);
q.bind(del->element2);
dels.insert(del);
}
}
return dels.entries();
}
On the other hand, why are you storing pointers in the vector? And using a non-standard vector class? It would be safer to store actual objects into a standard std::vector class instead:
void func(std::vector<NA> &dels) {
while(q.nextquery()) {
while(q.nextrow()) {
NA del;
q.bind(del.element1);
q.bind(del.element2);
dels.push_back(del);
}
}
}
I have the following classes in a program.
class Class1 {
public:
boost::ptr_vector<Class2> fields;
}
class Class2 {
public:
std:string name;
unsigned int value;
}
I want to write a member function in Class1 that returns a reference or pointer to an element in fields based on Class2's name variable. I don't have to be concerned with the lifetime of the objects in the container.
Currently, I am returning an iterator to the element I want after the function searches from the start of the vector to the element.
boost::ptr_vector<Class2>::iterator getFieldByName(std::string name) {
boost::ptr_vector<Class2>::iterator field = fields.begin();
while (field != fields.end()) {
if (field->name.compare(name) == 0) {
return field;
}
++field;
}
return fields.end();
}
The problems that I'm facing are:
(1.) I need to have fast random access to the elements or the program sits in getFieldByName() too long (a boost::ptr_vector<> is too slow when starting at the beginning of the container)
(2.) I need to preserve the order of insertion of the fields (so I can't use a boost::ptr_map<> directly)
I have discovered Boost::MultiIndex and it seems like it could provide a solution to the problems, but I need to use a smart container so that destruction of the container will also destruct the objects owned by the container.
Is there anyway to achieve a smart container that has multiple methods of access?
You can use two containers. Have a boost::ptr_map<> that stores the actual data, and then have a std::vector<> that stores pointers to the nodes of the map.
boost::ptr_map<std::string, Class2> by_field;
std::vector<Class2 const*> by_order;
void insert(Class2* obj) {
if (by_field.insert(obj->name, obj).second) {
// on insertion success, also add to by_order
by_order.push_back(obj);
}
}
This will give you O(lg n) access in your getFieldByName() function (just look it up in by_field) while also preserving the order of insertion (just look it up in by_order).
I haven't been working with C++ for a long time and now I need to do some small "project".
I reviewed few topics trying to find solution for my problem with no results...
Here is "pseudo-code" (Pair is my class):
Pair** pairs;
I get size of table from input and create table of pointers:
pairs = new Pair*[size];
I have a loop which creates some object and puts it's reference into table of pointers. More or less:
while (...)
{
Pair pair(...); // calling constructor, creating object type of Pair.
pairs[i] = &pair;
i++;
}
The problem is that &pair is the same number everytime (every step of loop).
Let's say the address of first created object is 1234.
So with every step in loop it overrides this object at 1234. So each pointer in pairs points to the same address -> same object.
I would like to force creating this object at new place in memory.
I've been trying to put those objects into other table and then pass their reference to table of pointers:
Pair* clearPairs = new Pair[size];
while (...)
{
clearPairs[i] = Pair(...);
pairs[i] = &clearPairs[i];
i++;
}
But problem still occurs.
Any tips?, mistakes/errors in my code (or thinking)? Shall I implement some "copying constructor"?
while (...)
{
Pair pair(...); // calling constructor, creating object type of Pair.
pairs[i] = &pair;
i++;
}
pair is allocated with automatic storage duration and goes out of scope upon each iteration of the loop. You are saving a pointer to an invalid object (a bunch of them).
You should use a collection of smart pointers if you really need pointers (this is true when objects cannot be copied or copies are expensive), which may not be necessary at all. Try this instead:
vector<Pair> pairs;
// and if you need pointers...
vector<unique_ptr<Pair>> pairs;
while(whatever) {
pairs.push_back(Pair(...));
// or...
pairs.push_back(unique_ptr<Pair>(new Pair(...)));
}
I am storing multiple objects in a vector of pointers to these objects, like so, in C++:
vector<Host *> somevector;
I then initialize each item like this:
somevector.push_back(new Host(x));
The object takes an initializing argument.
As I read through data (strings) and end up with a list of object pointers, I add them to an internal vector inside the object itself:
somevector.at(i)->add(string data);
However, it appears that all the data have been added to the same object, and even though the objects have different names, their internal vector that stores this data are identical.
I searched various keywords to solve this and I think it is an issue with a copy constructor (I currently am using default). How do I account for the vector inside the object that I am copying? Do I have to make the vector in the object a field so that a new one is created in the copy constructor?
EDIT:
I've replicated the code for the object class:
vector<string> v;
Host::Host(string _x): x(_x)
{
}
Host::~Host()
{
}
string Host::name()
{
return x;
}
string Host::link(int r)
{
int i = r % v.size();
return v.at(i);
}
void Host::add(string data)
{
v.push_back(data);
}
So I am using this vector inside the host object to store a bunch of strings. Then, when I call link from my main program, I pass it a random number, and I want to get a random string from the list inside the object. However, my link() calls are coming back with strings that should not have been stored into the object.
From what I can see from the example code you have posted 'v' isn't a member object of Host. So calls to Host::add are simply pushing back to a globally available vector. Is this perhaps where your problem is?