How to pass a pointer and a pointer to a function?

I am implementing a function that acts like getline (..). Therefore my initial approach:

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

Then, I think I should switch to pure C instead of using half C / C ++. So I change char * & to char **: Pointer to a pointer version (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;
} 

But this version crashed, (violation of access rights). I tried to run the debugger, but I could not find where it crashed. I am running Visual Studio 2010, so could you guys show me how to fix this?

Another weird thing I came across is that if I leave "&" out, it only works with Visual Studio, but not with g ++. it

void getstr( char* str, unsigned len ) 

, , , , . , **, * & . , Visual Studio, ?

+3
3

, , realloc

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

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

, , realloc , , , - :

char* tmp = (char*) realloc(*str, len)
if (tmp) {
    *str = tmp
} else {
    // realloc failed.. sigh
} 
+4

, , C C/++.

. ++.

+5

, ,

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

str - - *str - .

I will be tempted to rewrite it so that it returns a string or zero on error, instead of having a void return and an in / out parameter (like fgets, which seems to be the function you're sorting (copying behavior). Or wrap such a function. This style does not allow you to get confused, since you are ever dealing with a pointer to a char, not a pointer to a pointer to a char.

    char* getstr_impl ( char* str, unsigned len ) {...}

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

Source: https://habr.com/ru/post/1774168/


All Articles