Thread: tricky mallocing

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    tricky mallocing

    i programmed my way into a problem, i cant free memory i malloced.

    Code:
    int html_findstarttag(char *src, char tag[]) {
        char *search = malloc( strlen(tag) + 3 );
        
        html_createstarttag(search, tag);
        
        return whereis_string(src, search);
    }
    if i free search i can cant send it to my other function. is there any way around creating another varible just for the return?

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    You don't have to free it in the function that you allocated it... so if it works in this instance, free it after the function call to html_findstarttag() in the calling function.
    Away.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    freeing it outside of the function would be messy.

    i need search. html_createstarttag just adds <> to it. i need to send that string to whereis_string, after it gets it it can be free. but that function is main functions return value.

    could this be made a static var? would it free itself after the function is done on its own?

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    14
    C99 would accept that:
    Code:
    int html_findstarttag(char *src, char tag[]) {
        char search[strlen(tag) + 3 ];
        
        html_createstarttag(search, tag);
        
        return whereis_string(src, search);
    }
    but is it worth it just to avoid declaring a new int?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by P.Phant
    but is it worth it just to avoid declaring a new int?
    Exactly. Either use an integer to temporarily store the return value from the whereis function, or change your function call so it uses an integer pointer as an argument:
    Code:
    function( ... , int *where )
    {
        ... do stuff ...
    
        *where = whereis( ... );
    
        free( theString );
    }
    Really though, why aren't you, the orginal poster, just using an integer to store the return value of the function? As stated, there is no reason to design horrible code just because you don't feel like declaring an integer.

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

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    Really though, why aren't you, the orginal poster, just using an integer to store the return value of the function?
    im not doing anything to it, it just seems alittle wastefull, but a int isnt really that big. i think ive been playing around with too many 4kb computers.....

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by mart_man00
    im not doing anything to it, it just seems alittle wastefull, but a int isnt really that big. i think ive been playing around with too many 4kb computers.....
    How is it wasteful? It's only there until the function returns. Then it's gone.

    More than likely, you can do your search without the overhead of calling malloc and free, if you just take the time to redesign your search function.

    Tags are easy to search for. Especially if you use recursion. Without recursion, they're still easy. Just run through the string until you find a < (strchr anyone?). Then see if the next characters match your "tag". If so, you've found it. If not, run strchr again.

    No malloc needed. No int needed.

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

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    malloc is used just to turn a string into a tag.

    the search does do what you said quzah, it just returns the location in the string instead of a pointer to the address.

    ill just throw in a extra int...

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by mart_man00
    malloc is used just to turn a string into a tag.

    the search does do what you said quzah, it just returns the location in the string instead of a pointer to the address.

    ill just throw in a extra int...
    That's the easiest fix. I was just illustrating, that if you're worried about declaring an extra varaible, you can avoid using malloc all together if you feel like it, by simply changing how your search function works.

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

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeah, I'm replying to myself, but here's a Quick Hack(TM), which will illustrate what I'm talking about (ie: Improving your search function.):
    Code:
    int where( char *s, char *t, char u )
    {
    	char *v = s;
    
    	while( (v = strchr( v, u )) != NULL )
    	{
    		if( v+1 < strlen( s ) + s )
    		{
    			if( *(v+1) == *t && strstr( v+1, t ) == v+1 )
    				break;
    		}
    	}
    	return v == NULL ? -1 : v - s;
    }
    Thus, your code would be something like:
    Code:
    int html_findstarttag(char *src, char tag[]) {
        return where( src, tag, '<' );
    }
    See, no variable needed. Naturally you'd want to fine tune it to fit your specifics, but that should illustrate what I was talking about.

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

  11. #11
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    code from my str_whereis.c
    Code:
    int str_whereis_string(char *src, char search[]) {
        char *location = strstr(src, search);
    
        if(location == NULL)
            return -1;
    
        return (strlen(src) - strlen(location));
    }
    basicly everything in my str_cat.c(need for my tag code i posted before)
    Code:
    int str_catbefore(char *src, char string[]) {
        char *newstring = malloc( strlen(src) + strlen(string) + 1 );
    
        strcpy(newstring, string);
        strcat(newstring, src);
    
        strcpy(src, newstring);
        free(newstring);
    
        return 0;
    }
    
    int str_catafter(char *src, char string[]) {
        char *newstring = malloc( strlen(src) + strlen(string) + 1 );
    
        strcpy(newstring, src);
        strcat(newstring, string);
    
        strcpy(src, newstring);
        free(newstring);
    
        return 0;
    }
    
    int str_catboth(char *src, char before[], char after[]) {
        str_catbefore(src, before);
        str_catafter(src, after);
    
        return 0;
    }
    my tml_createstarttag function code.
    Code:
    int html_createstarttag(char *dest, char tag[]) {
        char *string = malloc( strlen(tag) + 3 );
        strcpy(string,tag);
    
        catboth(dest, "<", ">");
        strcpy(dest, string);
    
        free(string);
    
        return 0;
    }
    i attempted to make it more all purpose. im not thrilled about all my includes, but i like this style better(keep in mind im the n00b here). what do you guys think?

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int str_whereis_string(char *src, char search[]) {
        char *location = strstr(src, search);
    
        if(location == NULL)
            return -1;
    
        return (strlen(src) - strlen(location));
    }
    That isn't what you want. You should be returning the address of the greater, minus the address of the lesser. This will give you the difference, as per my example.

    return location - src;

    That gives you the position (number of characters) past the start of src that location is.

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

  13. #13
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    That gives you the position (number of characters) past the start of src that location is.
    thats what i wanted! alot of times i take apart strings with loops, i figured this would come in handy.

    i know i can use pointers for these kinds of things too.

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I've been playing along at a little slower pace, but this is what I'd dreamed up.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int foo(const char *src, const char *tag)
    {
       int result = -1;
       char *search = malloc( strlen(tag) + 3 );
       if ( search != NULL )
       {
          const char *ptr;
          sprintf(search, "<%s>", tag);
          ptr = strstr(src, search);
          if ( ptr != NULL )
          {
             result = ptr - src;
          }
          free(search);
       }
       return result;
    }
    
    int main(void)
    {
       const char text[] = "some text and <href> and other stuff", tag[] = "href";
       printf("text = \"%s\", tag = \"%s\", i = %d\n", text, tag, foo(text, tag));
       return 0;
    }
    
    /* my output
    text = "some text and <href> and other stuff", tag = "href", i = 14
    */
    Since much of it was already discussed, I'll just point towards the use of sprintf as a potential replacement for catboth.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    Since much of it was already discussed, I'll just point towards the use of sprintf as a potential replacement for catboth.
    not a bad idea, i forgot about that one(actually, ive never found a use for it till now)....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tricky C Question
    By ganesh bala in forum C Programming
    Replies: 11
    Last Post: 01-28-2009, 12:58 PM
  2. tricky output problem, dont understand
    By panfilero in forum C Programming
    Replies: 6
    Last Post: 11-12-2005, 12:30 AM
  3. Tricky #define macro...
    By willkoh in forum Windows Programming
    Replies: 4
    Last Post: 04-06-2005, 12:09 PM
  4. very simple but tricky... please help
    By surdy in forum C Programming
    Replies: 6
    Last Post: 10-05-2004, 12:03 PM
  5. A tricky macro
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 07:04 PM