Varibale scope and memory allocation

This is a discussion on Varibale scope and memory allocation within the C Programming forums, part of the General Programming Boards category; Hi guys, I'm working on a school project right now for theory of language. I'm a C newbie and I ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    2

    Varibale scope and memory allocation

    Hi guys, I'm working on a school project right now for theory of language. I'm a C newbie and I encountered a problem that I can't seem to solve.

    Any way, let me give you some background on the project. I'm suppose to implement a language generator from a given set of grammar.

    I have my lexical analyzer working that spits out tokens. The next step is, I'm constructing the grammar as a form of link-list/arrays.

    Here are my data structures
    Code:
    struct token{
    	// Terminal or Non-Terminal
    	int type;
    	// If it is a non terminal, then it is the name, else, it is the content
    	char *content_ptr;
    	
    	// The node is non-terminal, node_ptr points to an array of nodes below the current node.
    	struct token_array *aptr;
    };
    
    struct token_array{
    	int num_of_elements;
    	struct token *tptr;
    };
    If the data strucutre is built correctly, it should look something like this

    token_array ->an array of tokens
    each token then points to NULL if it is a terminal or another token_array that points to more tokens in an array (non terminals).

    And here is the code in trouble,
    Code:
    struct token_array build(FILE *file){
    
    	int first_token=TRUE;
    	
    	char *char_ptr;
    	
    	struct token token_ptr;
    	
    	struct token_array grammar = {0, NULL};
    	struct token_array temp_token_array = {0, NULL};
    	
    	do{
    		char_ptr = next_token(file);
    	
    		if(char_ptr!=NULL){
    			token_ptr = *token_wrapper(char_ptr);
    			
    			if(char_ptr[0]=='\n'){
    				first_token=TRUE;
    				grammar.tptr[grammar.num_of_elements-1].aptr = &temp_token_array;
    				temp_token_array.num_of_elements=0;
    				temp_token_array.tptr=NULL;
    			}
    			else{		
    				if(first_token==TRUE){
    					first_token=FALSE;
    					
    					grammar.num_of_elements++;
    					grammar.tptr=realloc(grammar.tptr, grammar.num_of_elements*sizeof(struct token));
    					grammar.tptr[grammar.num_of_elements-1]=token_ptr;
    					
    				}
    				else{
    					temp_token_array.num_of_elements++;
    					temp_token_array.tptr=realloc(temp_token_array.tptr, temp_token_array.num_of_elements*sizeof(struct token));
    					temp_token_array.tptr[temp_token_array.num_of_elements-1]=token_ptr;
    				}
    			}
    								   	
    			printf("%s\n", char_ptr);
    		}
    	}while(char_ptr!=NULL); 
    	
    	print_grammar(grammar);
    	
    	return grammar;
    }
    the main function look somethins like this....
    Code:
       	grammar = build(file);
        	
        	printf("-------Grammar--------");
        	print_grammar(grammar);
    The main problem is, when use printf, it destroyes the data structure. I get random values. If I do printf within the build function then everything is fine.

    I think it have to do with the scope of the variable....
    But I really is cruless

    Thanks in advance for any help you can provide!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,639
    Your intuition was correct.


    Code:
    struct token_array grammar = {0, NULL};
    struct token_array temp_token_array = {0, NULL};

    Variables declared within a function are allocated within it's stack frame, meaning they are destroyed when the function exits. You can pass the address of 'grammer' as a parameter of 'build' since you only need one persistent object to serve that purpose, but that won't work for 'temp_token_array' since it is actually a placeholder for many objects, in which case you'll have to allocate memory from the heap using 'malloc' (you could also do this with 'grammer', if you like).
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    2
    Thanks
    I'm already using realloc within the function to allocate memory, and if the pointer is NULL then realloc will be the same as malloc won't it?
    Code:
    grammar.tptr=realloc(grammar.tptr, grammar.num_of_elements*sizeof(struct token));
    
    temp_token_array.tptr=realloc(temp_token_array.tptr, temp_token_array.num_of_elements*sizeof(struct token));

    More questions:
    Whats the difference between function stack and the heap? Since both uses the same main memory. And I'm wondering how are they managed differently so they don't interfer with each other's memory space? Also, when a program allocates memory from the heap, hOw does it know what memory are free and what not?

    Sorry for asking so many questions

    Thanks
    Last edited by Guang Yan Wang; 02-25-2004 at 08:29 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 12:57 AM
  3. Scope of memory allocation
    By Xzyx987X in forum C Programming
    Replies: 7
    Last Post: 01-11-2004, 05:35 PM
  4. Memory allocation and Var Scope
    By penney in forum C Programming
    Replies: 1
    Last Post: 03-31-2003, 11:38 AM
  5. scope of memory allocation
    By doubleanti in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2002, 09:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21