Error with 'free(pointer)'

This is a discussion on Error with 'free(pointer)' within the C Programming forums, part of the General Programming Boards category; Hello, I am writing a simple function which removes non numeric characters in a string. Here I use a char ...

  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
    20,981
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,046
    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,047
    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 09:28 PM.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,185
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21