Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Calling all C++ Gods and compiler people

Calling all C++ Gods and compiler people
Thread Tools
Fresh-Faced Recruit
Join Date: Aug 2001
Location: College
Status: Offline
Reply With Quote
Mar 11, 2004, 11:32 PM
 
I'm writing a compiler for a subset of the Java language and am at my wit's end with this bug I've encountered.

This is how things look internally:

Code:
classes variables P * ------> x int -4 Q * y int -8 z boolean -12 methods parameters M int * ------> a int 0 * variables (locals) | d int -4 | | -------> ParseTree
corresponding to this code:

Code:
class P { //variables int x; int y; boolean z; // methods int M(int a) { // <---parameters // variables (locals) int d; // some code... (this goes in the ParseTree) } } class Q { // empty }

I'm having no trouble with the compiler writing process, but rather with some coding: copying a ParseTree then accessing it later. Basically what happens, is that when a class extends another, it inherits the base class' methods and those methods' ParseTrees as well.

What I want to be able to do is copy the ParseTree and stick it in the class' method's spot where it belongs, and also put a pointer to it in a deque. Then later I'll cycle through all the ParseTree*s in the deque for other processing.

When I try and access the ParseTrees from the deque I'm getting a segfault, which I'm unable to correct. I've been in & out of gdb all day and can't find the error. This is my first large scale project that I've written in C++ so I'm having a little trouble with memory management, but it seems like I'm not A) copying the ParseTree correctly, or B) I'm not putting it in the deque correctly, or C), I'm not accessing it from the deque correctly.

Now, this is a school project so I don't think I should be posting my source, but I'll provide what I think is sufficient.

Email me if you need more. jdgoettsch AT ucdavis DOT edu

Code:
Inserting the ParseTree: ParseTree* ppp; ppp = new ParseTree( mit->second.Get_PT_Ref() ); gst->methods.find(mit->first)->second.Insert_ParseTree(ppp); q2.push_back(ppp); // gst is a GlobalSymbolTable* // GlobalSymbolTable has a member multimap<char*, EntityAttribute> methods; // mit points to a map that holds the methods: // multimap<char*, EntityAttribute>* mit; // EntityAttribute holds a ParseTree* // ParseTree& EntityAttribute::Get_PT_Ref() {return *parse_code}
and the ParseTree copy constructor:
Code:
ParseTree::ParseTree(ParseTree& p) { root = p.root; } // root is a private member of ParseTree that points to the first node Node::Node(Node& n) { first_child = n.first_child; second_child = n.second_child; third_child = n.third_child; owning_class = n.owning_class; // just a char* owning_method = n.owning_method; // just a char* t = n.t; } // first_child, etc, are Node*
then accessing through the deque:
Code:
while(!q2.empty()) { cout << "\nDeque size: " << q2.size() << endl; q2.front()->DFS(); // it segfaults here q2.pop_front(); // q2 is deque<ParseTree*> q2 // void ParseTree::DFS() is a depth-first search // that runs perfectly when called from other locations // (which is why I think this part of the code is ok)

I should also add the everything compiles fine with g++, calling g++ -Wall -ansi ...
     
Junior Member
Join Date: Nov 2001
Location: Seattle
Status: Offline
Reply With Quote
Mar 14, 2004, 05:16 AM
 
My guess is that the problem lies in copying the ParseTree. When class members contain pointers to dynamically allocated memory, then just copying pointers will make the copy point to the same memory. When the original object is destroyed, it deallocates the memory, causing the copied object to contain dangling pointers.

If you want a true copy of the tree then you have to allocate memory and copy all the data contained in every pointer, rather than just setting the pointers equal. This is known as a "deep" copy rather than a "shallow" copy. In your code, this would apply to copying the root node, all the child nodes, and the char*s. (Not sure about "t = n.t;" as I don't know what type t is.)

Good luck. Glad I'm not writing a compiler!
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:01 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2