用我的C字符串反向函数进行Stange行为 [英] Stange behavior with my C string reverse function

查看:90
本文介绍了用我的C字符串反向函数进行Stange行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是一个业余程序员...
阅读时,第二次也是相隔两年多的时间,kochan的用Objective-C编程,现在是第六版。指针一章,当我开始使用C编程时,我试图复兴过去...
因此,我尝试使用char指针来编程反向C字符串函数...
最后,我得到了期望的结果,但是...也有一个非常奇怪的行为,我无法以自己的编程经验来解释...

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...

首先是代码:

这是一个.m文件,

#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;
}

第二条注意事项:

此代码在具有Mac OS X Maverick和 CLANG (clang -fobjc-arc $ file_name -o $ file_name_base)的MacBook Pro中编译。

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

NSPrint 只是 printf 的包装,以打印使用 stringWithFormat:arguments:构建的 NSString >

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

第三种奇怪的行为:

如果我不评论所有评论的 printf 声明,一切工作都很好,即所有 printf 函数都会打印其必须打印的内容,包括 main 函数中的最后一个 printf

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.

如果我取消注释一个,而只是随机选择的那些注释 printf 函数,那么一切正常,我得到了正确的答案 printf 结果,包括中的最后一个 printf ong> main 函数。

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.

如果我将所有注释的 printf 函数保持原样,则我只能保持空白线,最后一个 printf 位于 main 块中,每个传递的参数有一条黑线...

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...

最糟糕的是,如果我在 main 中使用了 NSPrint 函数,而不是 printf ,我会得到想要的结果:!

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 :)

推荐答案

返回一个局部数组,该局部数组在函数退出时超出范围。取消引用内存会导致未定义的行为。

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

这篇关于用我的C字符串反向函数进行Stange行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆