Why would you try to use a return value that you have not set? If it is set to 
NULL then you will core dump unless you check for NULL so it would not help 
you. The values are not initialized because I don't know what they are, they 
might be object references or something that cannot be set to NULL. I changed 
from assuming a nullable target because everyone complained ;-)

But I assure you that you can initialize all your values in the @init{} 
section. Where is it that you are having problems. I think that your question 
might not be the one you are asking.

Jim

> -----Original Message-----
> From: antlr-interest-boun...@antlr.org [mailto:antlr-interest-
> boun...@antlr.org] On Behalf Of Cristian Târºoagã
> Sent: Wednesday, May 19, 2010 2:08 PM
> To: antlr-interest@antlr.org
> Subject: [antlr-interest] C target - initialization of return/scope
> structures
> 
> Hi All,
> 
> My name is Chris, I started to use antlr and I like it a lot!
> I use C++ and I have successfully used it to generate some sourcecode.
> 
> I need to use C++: I want std::string, std::vector and more things like
> this.
> But since I use the C target, it didn't took too much time to get into
> some
> quirks though.
> 
> One of the problems I had/have is this: structures used for return
> values
> and those used for scope values are NOT initialized.
> 
> Since I tried to use a std:string as a scoped value, I quickly got a
> nice
> crash since my string was created using malloc.
> 
> These are (well) known problems, I know that. I found some posts from
> other
> guys having the same problems.
> I also found some recommendations on how to avoid initialization
> problems.
> E.g:
> http://www.mail-archive.com/il-antlr-
> inter...@googlegroups.com/msg02614.html
> 
> The hint there was to use pointers, and:
> 
> 1. define ANTLR3_MALLOC / ANTLR3_FREE to override antlr's allocators
> 
> or
> 
> 2. manually allocate/deallocate those pointers, probably inside @init
> and
> @after
> 
> 
> I'd like to have a clean solution to this, but I can't see how any of
> these
> two options can properly work.
> 
> 
> Option 1: I can't override antlr allocator like suggested
> #define ANTLR3_MALLOC(request) new request()
> because ANTLR_MALLOC is actually called with an argument which is
> actually
> the SIZE of the type that will be allocated and not the TYPE itself.
> I think a simple change inside antlr can fix this, but until then.... I
> tried the other way...
> 
> 
> Option 2: I can't use @init and @after because this will create memory
> leaks.
> Imagine that I have a scoped value x. I would do @init {x = new X();}
> and
> @after{delete x;}
> When rule is fully matched, this works perfectly.
> But when the parser fails, the code the pops the scoped value from the
> stack
> is called (and my piece of code inside @after is skipped) so I will get
> a
> memory leak!!
> I noticed that the scoped values also have a free function pointer
> inside
> (member), that can take care of deallocation in that situations, but I
> couldn't find a way to set it. (?)
> 
> 
> So:
> - my suggestion: change the ANTLR_MALLOC macro (change the name to
> ANTLR_ALLOC and change the impl to take as arg the type itself, so that
> a
> c++ impl could override it with 'new')
> - my suggestion: generate a properly initialized structure (I know,
> it's C
> code, but still...once you have such a smart StringTemplate lib, this
> shouldn't be a problem)
> - my question: what would be a clean way to allocate/deallocate
> pointers
> (without leaks)?
> 
> 
> THANKS a lot for ANTLR and for your help!
> 
>    Chris
> 
> 
> PS: I have some other problems too with the C target: I wasn't able to
> use
> composite grammars with C++. I will get back on this later :-)
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address




List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to il-antlr-inter...@googlegroups.com.
To unsubscribe from this group, send email to 
il-antlr-interest+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.

Reply via email to