Thread: Error with 'free(pointer)'

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    19

    Error with 'free(pointer)'

    Hello,

    I am writing a simple function which removes non numeric characters in a string. Here I use a char pointer by allocating memory to it. However, when I try to free the allocated memory, the program fails at runtime . Kindly let me know why this is happening.

    Code:
    char * FilterNumerics(char * Input, int iLength)
    	{
    	int i=0,j=0;
    	char *temp;
    	temp = (char *)malloc(iLength + 2);
    	if(!temp)
    		{
    		printf("Memory Allocation Failed\n");
    		return(1);
    		}
    	memset(temp,'\0',strlen(temp));
    	for (i = 0; i < iLength; i++) 
    		{    
    		if (Input[i] >= '0' && Input[i] <= '9') 
    			{        
    			temp[j] = Input[i];
    			j++;
    			}
    		}
    	free(temp);
    	return(0);
    	}
    Also please let me know if there is a better way to achieve the above functionality.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    you corrupt the memory with memset. strlen looks for a '\0' character but you haven't copied anything into the memory for temp. use the same method for initializing the memory that you did for creating it.
    Code:
    memset(temp, '\0', iLength+2);

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem could lie here:
    Code:
    memset(temp,'\0',strlen(temp));
    Since temp points to uninitialised memory (hence the point of the memset), what strlen(temp) returns is anyone's guess. You probably meant to write iLength + 2 instead of strlen(temp), but in that case perhaps just using calloc() instead of malloc() and memset() would be better.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Also:
    Code:
    if (Input[i] >= '0' && Input[i] <= '9')
    Consider isdigit().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Can you post the error you are getting?

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Yeah the problem was indeed with memset. After correction it is working fine now.Thanks everyone.

    However, one doubt I still have is , when I executed the same code in main, it worked fine. But in a function call it was errorring out. Any ideas why it happened that way?

    The error I WAS getting was :
    dbgheap.c line 1010: "Expression: _crtisvalidheappointer(puserdata)"
    Last edited by dunxton; 03-04-2009 at 10:28 PM.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Using malloc() and strlen() in main() like you have now is bound to have problems. As laserlight pointed out, strlen() of temp is unknown.
    Post the code as it appears in main() and someone might be able to help out.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Doing these kinds of things invokes undefined behavior, which is, well... undefined. So it can do anything. It can work, it can fail, it can blow up your house, or even launch nuclear missiles.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed