Thread: String malloc & free?

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    1

    Question String malloc & free?

    Hello, guys!
    This code works, but I not certain that it is correct :-)
    i do this
    tmp = (char *) malloc(strlen(s)+1);
    but i don't free memory, possible to do it?
    Possible to do this more effectively and beautifully? ;-)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXFIELD	15
    
    char *get_field(const char *s, char *name)
    {
    	char *tmp, *field;
    	int i;
    
    	tmp = (char *) malloc(strlen(s)+1);
    	if (!tmp)
    		return NULL;
    	strcpy(tmp, s);
    	
    	if ((field = strstr(tmp, name)) != NULL) {
    		field += strlen(name);
    		for(i=0; *tmp != '&' && *tmp != '\0' && i < MAXFIELD; i++)
    			tmp++;
    		*tmp = '\0';
    	}
    	return field;   /* NULL if not found */
    }
    
    int main(void)
    {
    	char *str = "http://www.google.com/search?q=FreeBSD";
    	char *fn;
    
    	if (fn = get_field(str, "q="))
    		printf ("%s\n", fn);
    	return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    	tmp = (char *) malloc(strlen(s)+1);
    	if (!tmp)
    		return NULL;
    	strcpy(tmp, s);
    To set the final character to null, I'd just do:

    tmp[strlen(s)] = '\0';

    I'm not sure the purpose of your 'name' check there. Are you truncating the string? If so, you should really free whatever is beyond the truncation point.

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

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    To set the final character to null, I'd just do: tmp[strlen(s)] = '\0';
    That's a good habit, quzah, but strcpy automatically appends a null to the end of the target, so no need there...

    All in all, I think the coding is good style and better than average. Good work, Tracker.

    Still, not freeing memory is not very good, if not just because it is needlessly wasteful. So my suggestion would be to free it just before returning from the function.

    There are a few discrepancies too. You loop through "temp", but why? Look ->

    Code:
    char *get_field(const char *s, char *name)
    {
    	char *tmp, *field;
    	int i;
    
    	tmp = (char *) malloc(strlen(s)+1);
    	if (!tmp)
    		return NULL;
    	strcpy(tmp, s);
    	
    	if ((field = strstr(tmp, name)) != NULL) 
    	 field += strlen(name);
    
                    //..........all done!........//
    
    	free(temp);
    
    	return field;   /* NULL if not found */
    }



    Anyway, it is a cool function.

    Here's another way, too:



    Code:
    char *get_field(const char *s, char *name)
    {
    	char *field;
    
      	field = strstr( s, name );
    
    	if( field != NULL)
          field += strlen(name);
    
    	return field;   /* NULL if not found */
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Help needed with backtracking
    By sjalesho in forum C Programming
    Replies: 1
    Last Post: 11-09-2003, 06:28 PM
  4. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  5. Ask about free funtion using with malloc
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 05-12-2002, 04:43 PM