Thread: memory allocation problem

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    29

    Question memory allocation problem

    hi,

    there is a memory allocation problem in this method..because although it compiles and runs ok...it stuffs up one of my other variables...

    can someone please help

    thanks alot



    Code:
    char* setSearchTag(char *search) {
    
    	char *newSearchTag;
    	char *startTag = "<";
    	char *endTag = ">";
    	int newSearchTagLength = strlen(search) + strlen(startTag) + strlen(endTag);	
    	
    
    	if ((newSearchTag = ( char * )malloc(sizeof(char) * (newSearchTagLength)+1)) == NULL){
    		fprintf(stderr, "Unable to allocate %d bytes of memory for current search\n", newSearchTagLength+1);
    		exit(1);
    	}		
    
    	strcpy(newSearchTag, startTag);
    	strcat(newSearchTag,search);
    	strcat(newSearchTag,endTag);
    
    	return newSearchTag;
    				
    }

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    10
    this is a c programming board... go post this in c++ im sure someone will be able to help you

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    29
    this is C....???? compiled using gcc!...anyone know how to fix

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    29
    maybe u needed more detail to help me...

    here is where i called it...and the variable that was stuffing up is file (passed into searchForTags()...

    Code:
    void searchForTags(char *file) {
    	
    	char *currentSearch;
    	int *startOfData;
    	int *endOfData;
    	char *titles;	
    
    	currentSearch = setSearchTag("title");
    	startOfData = search(file, currentSearch, "start");
    	
    
    	currentSearch = setSearchTag("/title");	
    	endOfData = search(file, currentSearch, "end");
    	
    }
    
    char* setSearchTag(char *search) {
    
    	char *newSearchTag;
    	char *startTag = "<";
    	char *endTag = ">";
    	int newSearchTagLength = strlen(search) + strlen(startTag) + strlen(endTag);	
    	
    
    	if ((newSearchTag = ( char * )malloc(sizeof(char) * (newSearchTagLength)+1)) == NULL){
    		fprintf(stderr, "Unable to allocate %d bytes of memory for current search\n", newSearchTagLength+1);
    		exit(1);
    	}		
    
    	strcpy(newSearchTag, startTag);
    	strcat(newSearchTag,search);
    	strcat(newSearchTag,endTag);
    
    	return newSearchTag;
    				
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What is your search function? Are you passing your arguments to it in the correct order? Additionally, I'll mention it since I don't see it, you never free anything you're using either. My guess is your search function is the problem. Try testing the setSearchTag function by itself in another program or set up some in your current to make sure it is doing what you want it to. Or just add a few simple printf statements to make sure it's giving you the output you expect.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    29
    i still couldnt find the problem...i steped through setSearchTag()...it does exactly what i want...but if...i print the variable file before and after the method call...it changes....which is really odd...ill post my code again with the search function...thanks again for anyhelp

    Code:
    void searchForTags(char *file) {
    	
    	char *currentSearch;
    	int *startOfData;
    	int *endOfData;
    	char *titles;	
    
    	currentSearch = setSearchTag("title");
    	startOfData = search(file, currentSearch, "start");
    	
    
    	currentSearch = setSearchTag("/title");	
    	endOfData = search(file, currentSearch, "end");
    	
    }
    
    char* setSearchTag(char *search) {
    
    	char *newSearchTag;
    	char *startTag = "<";
    	char *endTag = ">";
    	int newSearchTagLength = strlen(search) + strlen(startTag) + strlen(endTag);	
    	
    
    	if ((newSearchTag = ( char * )malloc(sizeof(char) * (newSearchTagLength)+1)) == NULL){
    		fprintf(stderr, "Unable to allocate %d bytes of memory for current search\n", newSearchTagLength+1);
    		exit(1);
    	}		
    
    	strcpy(newSearchTag, startTag);
    	strcat(newSearchTag,search);
    	strcat(newSearchTag,endTag);
    
    	return newSearchTag;
    				
    }
    
    int* search(char *file, char *currentSearch, char *tagType) {
    
    	int error = 0;
        regex_t re;
        size_t a = 2;
        regmatch_t arrayOfMatches[1000];
    	int counter=0;
    	int *Matches;
    	    
        if (regcomp(&re, currentSearch, REG_EXTENDED|REG_ICASE) != 0) {
    
            return(0);      
    
        }
    
    	if ((Matches = malloc(sizeof(int))) == NULL){
    			fprintf(stderr, "Unable to reallocate %d bytes of memory for current search\n", sizeof(int));
    			exit(1);
    	}
    
    
    	printf("-----XXXXXXXXXXXXXXXXXXXXXXXXXX-----\n");
        
    	/* Finds the matches on the line */
    	error = regexec (&re, file, a, arrayOfMatches, 0);
    
    	/* while matches found */
    	while (error == 0) {    
    	 		
    		if(strcmp(tagType,"start")){
    			Matches[counter] = arrayOfMatches[counter].rm_eo;
    		}
    		
    		else{
    			Matches[counter] = arrayOfMatches[counter].rm_so;
    		}
    
    		if ((Matches = (int *)realloc(Matches, sizeof(int))) == NULL){
    			fprintf(stderr, "Unable to reallocate %d bytes of memory for file\n", sizeof(int));
    			exit(1);
    		}
     		
      	  	/* This call to regexec() finds the next match */
        	error = regexec (&re, file + arrayOfMatches[counter].rm_eo, 1, arrayOfMatches, REG_NOTBOL);
        	
    		counter++;
        }
    
    	if (error == 0) {    
    	
     		return 0;
        }
    	
    	regfree(&re);
        
        return Matches;
    
    }

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    29
    ok...ive fixed the problem in setSearchTag...i needed to use calloc instead of malloc....

    but i now get a bus error in the search method above...gdb tells me its on the line where i do my realloc 'ing...which is odd cause i copyed from a textbook...any ideas ?:

    Code:
    if ((Matches = (int *)realloc(Matches, sizeof(int))) == NULL){
    			fprintf(stderr, "Unable to reallocate %d bytes of memory for file\n", sizeof(int));
    			exit(1);
    		}

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > if ((Matches = (int *)realloc(Matches, sizeof(int))) == NULL){

    The above code really isn't doing anything, because you are only asking for sizeof(int) space, which is enough room for only one int, not an array. The amount you are reallocating should be getting bigger each time.

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    29
    thats correct...each time i add something to the array...all i need is one int...
    which is 1 element...isnt it?

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >each time i add something to the array
    realloc() resizes the memory block based on the amount you are requesting.

    Here's a description of realloc

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >this is a c programming board... go post this in c++ im sure someone will be able to help you
    What part of that code isn't C?

    >The amount you are reallocating should be getting bigger each time.
    Or smaller depending on your need.

    >if ((Matches = (int *)realloc(Matches, sizeof(int))) == NULL){
    Bad idea. What if realloc fails (assuming you actually were resizing a dynamic array)? This is better:
    Code:
    int *save = realloc ( Matches, new_size * sizeof *Matches );
    if ( save != NULL ) {
      Matches = save;
    }
    else {
      /* No resize, Matches is the same */
    }
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Apr 2004
    Posts
    29
    thanks for all ur help ppl...i fixed all my memory problems

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >>The amount you are reallocating should be getting bigger each time.
    >Or smaller depending on your need.

    Well, I assumed from ccoder's code, bigger was the plan.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with linked list and shared memory
    By Sirfabius in forum C Programming
    Replies: 10
    Last Post: 11-10-2008, 04:45 PM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  4. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM
  5. Memory Problem - I think...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-24-2001, 12:14 PM