C/Objective-C memory issue?

Discussion in 'Mac Programming' started by mdeh, Apr 25, 2010.

  1. mdeh macrumors 6502

    Joined:
    Jan 3, 2009
    #1
    Hi all,
    I wonder if I can get some input to solve this issue.
    Background.
    I am working on a method in Obj-C that uses some straight c functions.


    Here is the code:

    Code:
    -(NSString*) charToBinary: (NSString *) aCharacter
    {
    	char *charForTest = (char *)[aCharacter UTF8String];
    	char *temp = (decToBin(charForTest));
    	NSLog(@"%s", temp);
    	NSString *bytesOfString =  [NSString stringWithCString:temp
    												  encoding:NSUTF16StringEncoding];
    	return bytesOfString;
    	
    }
    

    For completeness, the function decToBin, which is probably not correct,( but as yet, unable to get to that point of checking the logic).

    Code:
    char *decToBin(char * inputChar)
    {
    	char binaryArr[EIGHT_BYTE_ARRAY];
    	
    	int i = 0;
    	int charValue = (int) inputChar[0];
    	while (charValue) {
    		
    		
    		binaryArr[i++] =  (charValue % 2) ? '1': '0';
    		charValue /= 2;
    		
    	}
    	binaryArr[i] = '\0';
    	
    	
    	char *t = reverseString(binaryArr);
    	return t;
    	
    }

    Here is what happens. in the Obj-C code, "temp" initially contains a string, of the nature "100110" etc. But, as soon as the line bytesOfString is called, temp reverts to \"000
    My thinking is that, and this is where my knowledge of memory management in C is rudimentary, that "temp" is being released? But not sure. Would appreciated some insight.
    And, the other function mentioned.


    Code:
    char *reverseString(char  *inputString)
    {
    	int i, j;
    	char t[strlen(inputString)+1];
    	strcpy(t,inputString);
    	for (i = 0, j = strlen(inputString) - 1; j >= 0; j--, i++)
    	{
    		*(inputString +i) = *(t + j);
    	}
    	
    	inputString[i] = '\0';
    	
    	//printf("\"InputString\":%s\n", inputString);
    	
    	return inputString;
    }
    
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Your decToBin() is ultimately returning a pointer to a local variable (the array binaryArr). Local variables do not survive after a function returns. This is the same for C or Objective-C. Strictly speaking, the correct term is "automatic", as "local" only refers to scope, not lifetime.

    http://en.wikipedia.org/wiki/Local_variable
    http://en.wikipedia.org/wiki/Automatic_variable

    Refer to a C reference manual if the Wikipedia articles aren't sufficient. The C keyword (hence, the Objective-C keyword) is 'auto', and is the implicit storage type for all variables declared inside function bodies, just as 'static' is the implicit storage type for all variables declared outside function bodies.

    Also, your loop in decToBin() will never end if charValue is negative, and will overflow its buffer until it kills something. You should iterate exactly 8 times, not indefinitely. If you want to produce a string with no leading zeros, you need a better algorithm than what you have.

    This code is also completely wrong:
    Code:
    stringWithCString:temp encoding:NSUTF16StringEncoding
    
    The encoding of temp (if temp actually survived long enough) is not UTF16. It's ASCII or UTF8, or another 8-bit encoding compatible with ASCII in the first 128 code-points. I know this because decToBin() is operating on char types, not wchar_t types, and the default interpretation of the character constants '1' and '0' is ASCII.
     
  3. mdeh thread starter macrumors 6502

    Joined:
    Jan 3, 2009
    #3
    Hi chown33,
    As always, thanks.

    Ok...I was on the right path!!! Thanks :)


    This did the trick (plus the change to the encoding)

    char binaryArr[EIGHT_BYTE_ARRAY];

    char *decToBin(char * inputChar)
    {

    extern char binaryArr[];

    etc etc.
     

Share This Page