代码之家  ›  专栏  ›  技术社区  ›  roxrook

如何将指针和指针传递给函数?

c++
  •  3
  • roxrook  · 技术社区  · 14 年前

    我实现了一个类似getline(…)的函数。所以我的第一个方法是:

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    
    void getstr( char*& str, unsigned len ) {
        char c;
        size_t i = 0;
        while( true ) {
            c = getchar(); // get a character from keyboard
            if( '\n' == c || EOF == c ) { // if encountering 'enter' or 'eof'
                *( str + i ) = '\0'; // put the null terminate
                break; // end while
            }
            *( str + i ) = c;
            if( i == len - 1 ) { // buffer full 
                len = len + len; // double the len 
                str = ( char* )realloc( str, len ); // reallocate memory
            }
            ++i;
        }
    }
    
    int main() {
        const unsigned DEFAULT_SIZE = 4;
        char* str = ( char* )malloc( DEFAULT_SIZE * sizeof( char ) );
        getstr( str, DEFAULT_SIZE );
        printf( str );
        free( str );
        return 0;
    }
    

    指向指针版本的指针(crahsed)

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    
    void getstr( char** str, unsigned len ) {
        char c;
        size_t i = 0;
        while( true ) {
            c = getchar(); // get a character from keyboard
            if( '\n' == c || EOF == c ) { // if encountering 'enter' or 'eof'
                *( *str + i ) = '\0'; // put the null terminate
                break; // done input end while
            }
            *( *str + i ) = c;
            if( i == len - 1 ) { // buffer full 
                len = len + len; // double the len 
                *str = ( char* )realloc( str, len ); // reallocate memory
            }
            ++i;
        }
    }
    
    int main() {
        const unsigned DEFAULT_SIZE = 4;
        char* str = ( char* )malloc( DEFAULT_SIZE * sizeof( char ) );
        getstr( &str, DEFAULT_SIZE );
        printf( str );
        free( str );
        return 0;
    } 
    

    但是这个版本崩溃了(访问冲突)。我试着运行调试器,但找不到它崩溃的地方。我在运行Visual Studio 2010,你们能告诉我如何修复它吗?

    我遇到的另一件奇怪的事情是,如果不使用“&”,它只适用于Visual Studio,而不适用于g++。那就是

    void getstr( char* str, unsigned len ) 
    

    3 回复  |  直到 12 年前
        1
  •  4
  •   NG.    14 年前

    您的指针崩溃可能在重新分配中

    *str = ( char* )realloc( str, len )

    *str = ( char* )realloc( *str, len )

    正如Steve指出的,如果realloc失败,您的代码将泄漏原始代码,因此可以将其更改为:

    char* tmp = (char*) realloc(*str, len)
    if (tmp) {
        *str = tmp
    } else {
        // realloc failed.. sigh
    } 
    
        2
  •  5
  •   fredoverflow    14 年前

    然后,我认为我应该切换到纯C而不是使用半C/C++。

    我建议另一个方向。全面展开C++。

        3
  •  0
  •   Pete Kirkham    14 年前

            *str = ( char* )realloc( str, len ); // reallocate memory
    

    如果两者不匹配 str *str

    我很想重写它,以便它返回字符串,或者错误时返回零,而不是有一个void返回和一个in/out参数(就像fgets那样,这似乎是您复制行为的函数)。或者包装这样的功能。这种样式不会让您感到困惑,因为您只处理指向char的指针,而不是指向char的指针。

        char* getstr_impl ( char* str, unsigned len ) {...}
    
        void getstr( char** str, unsigned len ) {
            *str = getstr_impl ( *str, len );
        }