Thread: About malloc,realloc,pointer and array

  1. #1
    Registered User
    Join Date
    Jun 2010
    Location
    beijing
    Posts
    23

    About malloc,realloc,pointer and array

    i have an expression in an array, i want the expression to omit space and tab characters and put it into an buffer,but i dont know how to go on to do it.




    Code:
    #include <string.h>
    
    char * InitExpression(char *pExpression, char *pBuf)
    {
    	char *p = NULL;
    	int i = 0;
    
    	pExpression = (char *)malloc(1);
    	p = pExpression;
    
    	while ('\0' != *pBuf)
    	{
    		if (32 != *pBuf && 9 != *p)
    		{
    			*p = *pBuf;
    			p = (char *)realloc(pExpression, 1);
    			++p;
    		}
    		++pBuf;
    	}
    	return pExpression;
    }
    
    int main()
    {
    	struct Polyn *pUnitaryPolynomial = NULL;
    	char cExpression[] = {"	95 x ^  2	-	4  x +	2	"};
    	char *szExpression = NULL;
    
    	szExpression = InitExpression(szExpression, cExpression);
    
    	return 0;
    }

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Code:
    if (32 != *pBuf && 9 != *p)
    I'm sure you intended to check a different variable.

    Code:
    p = (char *)realloc(pExpression, 1);
    The second argument to realloc is the size of the new block of memory, not how much to increase it by.
    Calling realloc inside of a loop seems like a bad idea to me anyway - you'll want to consider ways of allocating enough space for the new string on the first malloc.
    Consider this post signed

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by bernt View Post
    The second argument to realloc is the size of the new block of memory, not how much to increase it by.
    Calling realloc inside of a loop seems like a bad idea to me anyway - you'll want to consider ways of allocating enough space for the new string on the first malloc.
    Actually using realloc on a byte by byte basis is a real bad idea (Read: Bottleneck). Probably our friend would be better watching the buffer pointer as he adds stuff and reallocating in at least 64 byte blocks.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    It is better practice to check against ' ' and '\t' (space and tab) rather than the ASCII numeric equivalents. Easier to read, more portable if there was ever any question about which character mapping is in use, etc.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    xian china
    Posts
    31
    u can malloc a buffer first, and test if the buffer is full, if it is full, then u can relloc the buffer

  6. #6
    Registered User
    Join Date
    Jun 2010
    Location
    beijing
    Posts
    23
    now, i updated the code:
    Code:
    char * InitExpression(char *pExpression, char *pBuf)
    {
    	char *p = NULL;
    	int i = 0;
    
    	pExpression = (char *)malloc(1);
    
    	while ('\0' != *pBuf)
    	{
    		p = pExpression;
    		if (' ' != *pBuf &&	'\t' != *pBuf)
    		{
    			*(p+i) = *pBuf;
    			pExpression = (char *)realloc(pExpression, 1);
    			++i;
    		}
    		++pBuf;
    	}
    	return pExpression;
    }
    and i wanna put the pBuf into pExpression without any space and tab, but the content of the pExpression is always wrong, i dont know how to modify it!help

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You don't even need to realloc() really.
    pExpression cannot take more space than strlen(pBuf) + 1.
    Just allocate that amount and if you want, realloc() /shrink to actual size.

    Code:
    char *ret = malloc( strlen(pExpression) + 1 );
    // check malloc return...
    while( *pBuf != '\0') {
       if(*pBuf != ' ' && *pBuf != '\t')
          *ret++ = *pBuf;
    
       pBuf++;
    }
    *ret = '\0';   // don't forget!

  8. #8
    Registered User
    Join Date
    Jun 2010
    Location
    beijing
    Posts
    23
    thanks Bayint Naung, i now know how to code ^_^
    Code:
    char * InitExpression(char *pExpression, char *pBuf)
    {
    	char *p = NULL;
    
    	pExpression = (char *)malloc(strlen(pBuf) + 1);
    	p = pExpression;
    
    	while ('\0' != *pBuf)
    	{
    		if (' ' != *pBuf &&	'\t' != *pBuf)
    		{
    			*p++ = *pBuf;
    		}
    		++pBuf;
    	}
    
    	*p ='\0';
    
    	return pExpression;
    }

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Since you are returning dynamic memory allocation.
    1st parameter of your function is redundant.
    Question 4.8

    Code:
    char * InitExpression(char *pBuf)
    {
    	char *p = NULL;
            char *pExpression, 
    	pExpression = (char *)malloc(strlen(pBuf) + 1);
    	p = pExpression;
    
    	while ('\0' != *pBuf)
    	{
    		if (' ' != *pBuf &&	'\t' != *pBuf)
    		{
    			*p++ = *pBuf;
    		}
    		++pBuf;
    	}
    
    	*p ='\0';
    
    	return pExpression;
    }
    And you need not cast malloc return value. Question 7.7

  10. #10
    Registered User
    Join Date
    Jun 2010
    Location
    beijing
    Posts
    23
    pBuf includes the content of my expression, and in my main function i code:
    Code:
    int main()
    {
        char cExpression[] = {"-2+4x^2-3"};
        char *szExpression = NULL;
    
        szExpression = InitExpression(szExpression, cExpression);
    
        free(szExpression);
        szExpression = NULL;
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM