Page 2 of 2

Posted: Tue May 03, 2011 5:48 pm
by dougcl
reachmexyz wrote:did the code that priyadarshikunal sent worked. Since there is no free statement, the routine would blow up if there are many incoming records. Do a vmstat and the free memory should be coming down drastically depending on the number of records you are processing.
What do you think should be freed?

By the way, I am confused by the malloc statement and I think it's incorrect.

I believe it should be:

Code: Select all

char *result = (char *)malloc (strlen(str)); 
Actually, this is the full expression, but unnecessary because sizeof(char *) is always 1.

Code: Select all

char *result = (char *)malloc (strlen(str) * sizeof(char *)); 

Posted: Wed May 04, 2011 3:03 am
by priyadarshikunal
As its just assinging the value to the pointer it should not create any problem.

Even though, it will create problems as the free statement can't be written

need to replace it with new operator

Code: Select all

#include "stdio.h" 
#include "string.h" 
#include "stdlib.h"      
#include "ctype.h" 

char* ConvMCT(char *str)  //Function with string input and string  
{ 
   char *result = new char[sizeof(str)*sizeof(char *)]; 
   int x=0, Flag=1;  // Setting Flag to 1 to make the first letter capital. 
   char CheckStr[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 

   while(*str) 
   { 
      if(Flag=1)  //Check if the last character was not alphabet. 
      { 
         if(isalpha(*str) and islower(*str)) //Convert to uppercase if its a lower case alphabet. 
         { 
            result[x] = upper(*str); 
         } 
         else 
         { 
            result[x] = *str; //No Change if its already in uppercase or not an alphabet. 
         } 
          
      } 
      else 
      { 
         if(isalpha(*str) and isupper(*str)) 
         { 
            result[x] = tolower(*str); //Convert to lowercase except the first character. 
         } 
         else 
         { 
            result[x] = *str; 
         } 
      } 

      if(!strchr(CheckStr, *str))   //Check if the string is not a-z and A-Z. 
      { 
         Flag=1; 
      } 
      else 
      { 
         Flag =0; 
      } 
      ++x; 
      ++str; 
   } 
   result[x] = '\0'; //Terminate the string 
   return result; //Return the replaced string 
}
can't check the code at the moment but if you have used it and there is some problem, please let me know/post the updated version so that it helps others in future.

Posted: Thu May 05, 2011 11:18 am
by dougcl
Are we saying that these are equivalent?

Code: Select all

char *result = new char[sizeof(str)*sizeof(char *)];

Code: Select all

char *result = (char *)malloc (strlen(str) * sizeof(char *)); 

Posted: Fri May 06, 2011 7:41 am
by priyadarshikunal
Sizeof gives the byte size and strlen returns the length and hence here it should be fine, new operator will release the memory itself so no memory issue should get created.

Posted: Thu Sep 10, 2015 11:46 pm
by pbttbis
Thanks for the code!!

C++ will be new to many, so just a note for others wanting to use the code above. It needs to be Flag==1 Or Flag>0 otherwise it uppercases the entire string.

Also upper must be toupper.

Also on my compiler I had to change "and" to "&&".

I hope it saves others some time debugging...