Class out-of-scope problem?

21 views
Skip to first unread message

Harry Potter

unread,
Oct 11, 2012, 10:20:03 AM10/11/12
to cpp-pro...@googlegroups.com
Hi!  I've been creating a cross-platform C compiler using ANSI/ISO C++ and am running into a problem.  I have a function that handles variable declarations.  This function declares a label within the auto namespace (Term correction?) as:
 
        Label* l=new Label;     //Tmp. label
 
If the routine reaches a '(' in the input to indicate a function, it calls another function.  Before the function call, the name of the function is retrieved correctly.  However, within the called function, the function name returns garbage.  What can cause this?  I'm thinking that maybe the class loses scope during the call.  I'm sorry that I'm not revealing more code; there's alot of code, and it's very vcomplex.  I'd appreciate any advice.

Beyeler Studios

unread,
Oct 12, 2012, 12:15:47 PM10/12/12
to cpp-pro...@googlegroups.com
Hi Harry ^^

from your post I guess Label contains some kind of a string (char pointer)? And when you're later trying to read that string, it appears garbled?
This can happen, when you dereference a char pointer e.g. to a deleted character array. The memory might be overwritten by another allocation, but the address might still be valid for your programme (that way you don't get an access violation exception).
See if you can trace your Label pointer l and find out two things: 1) is it really l that you are passing to the function in question? 2) do you delete l (or the pointer to the "name of the function" inside the object) before you dereference it in the function in question?

Next time try to at least add the definition (minimum fields, constructor/deconstructor) of Label, or try to explain more, how the "name of the function" is represented, how you're passing it around and how you're trying to access it in your code.

Best,
BeyelerStudios

Harry Potter

unread,
Oct 12, 2012, 1:34:25 PM10/12/12
to cpp-pro...@googlegroups.com
Thank you for responding.  The name of the label is stored as a char*, and a member function call returns the name of the variable as written for the assembler.  To test the class, I wrote several lines that output the label name to the console window at different points in the compilation.  One line before the function-processing call, the name is displayed properly.  One of the first tasks of the latter is to print the name of the label.  It displays garbage.  Later on in the course of the compiler, it crashes.  There could be no interference from another application or anther part of the same application, as it is a simple console tool.  I plan to see if maybe the address of either the Label class or the name string is off and get back to you on Monday.

FredK

unread,
Nov 15, 2012, 2:37:23 PM11/15/12
to cpp-pro...@googlegroups.com
You say you are storing the nameas a "char *".
Do you make a COPY of the  name, or just point your char * variable to the name? What is the return value of the member function call - does it point to newly allocated memory, or to a local string that goes out of scope when the function returns?
Reply all
Reply to author
Forward
0 new messages