Stange behavior with my C string reverse function

柔情痞子 提交于 2019-12-11 14:43:22

问题


I'm just an amateur programmer... And when reading, for the second time, and more than two years apart, kochan's "Programming in Objective-C", now the 6th ed., reaching the pointer chapter i tried to revive the old days when i started programming with C... So, i tried to program a reverse C string function, using char pointers... At the end i got the desired result, but... got also a very strange behavior, i cannot explain with my little programming experience...

First the code:

This is a .m file,

#import <Foundation/Foundation.h>
#import "*pathToFolder*/NSPrint.m"

int main(int argc, char const *argv[])
{
    @autoreleasepool
    {
        char * reverseString(char * str);
        char *ch;

        if (argc < 2)
        {
            NSPrint(@"No word typed in the command line!");
            return 1;
        }

        NSPrint(@"Reversing arguments:");

        for (int i = 1; argv[i]; i++)
        {
            ch = reverseString(argv[i]);
            printf("%s\n", ch);
            //NSPrint(@"%s - %s", argv[i], ch);
        }
    }

    return 0;
}

char * reverseString(char * str)
{
    int size = 0;

    for ( ; *(str + size) != '\0'; size++) ;

    //printf("Size: %i\n", size);

    char result[size + 1];
    int i = 0;

    for (size-- ; size >= 0; size--, i++)
    {
        result[i] = *(str + size);
        //printf("%c, %c\n", result[i], *(str + size));
    }

    result[i] = '\0';

    //printf("result location: %lu\n", result);
    //printf("%s\n", result);

    return result;
}

Second some notes:

This code is compiled in a MacBook Pro, with MAC OS X Maverick, with CLANG (clang -fobjc-arc $file_name -o $file_name_base)

That NSPrint is just a wrapper for printf to print a NSString constructed with stringWithFormat:arguments:

And third the strange behavior:

If I uncomment all those commented printf declarations, everything work just fine, i.e., all printf functions print what they have to print, including the last printf inside main function.

If I uncomment one, and just one, randomly chosen, of those comment printf functions, again everything work just fine, and I got the correct printf results, including the last printf inside main function.

If I leave all those commented printf functions as they are, I GOT ONLY BLANK LINES with the last printf inside main block, and one black line for each argument passed...

Worst, if I use that NSPrint function inside main, instead of the printf one, I get the desired result :!

Can anyone bring some light here please :)


回答1:


You're returning a local array, that goes out of scope as the function exits. Dereferencing that memory causes undefined behavior.




回答2:


You are returning a pointer to a local variable of the function that was called. When that function returns, the memory for the local variable becomes invalid, and the pointer returned is rubbish.



来源:https://stackoverflow.com/questions/25603908/stange-behavior-with-my-c-string-reverse-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!