Returning char* from function in C ?



  • Code below does not result in a meaningful output as expected.
    But why when one or both of the printf-lines (actually commented out and marked "//*") are avtive ?

    #include <stdio.h>
    
    char* test_1() {
        static char a[3] = {'A', 'B'};
        return a;
    }
    
    char* test_2() {
        char a[3] = {'A', 'B'};
        printf("func_2= %s\n", a);
        return a;
    }
    
    char* test_3() {
        char a[3] = {'A', 'B'};
        return a;
    }
    
    int main(void)
    {
    // *   printf("main_1= %s\n", test_1() );
    // *   printf("main_2= %s\n", test_2() );
        printf("main_3= %s\n", test_3() );
        return 0;
    }
    

  • Lifetime Qt Champion

    Hi,

    In test_2 and test_3 you return the address of a local variable which is destroyed at the end of the function. Your compiler should be telling you that with warnings.



  • yes, the compiler do so:
    .../main.c:20: warning: function returns address of local variable [-Wreturn-local-addr] return a;

    Anyway, I get a correct result which is not expected.
    Just at random ?


  • Lifetime Qt Champion

    What correct result ?



  • with all three printf -lines in main uncommented:

    main_1= AB
    func_2= AB
    main_2= AB
    main_3= AB
    

    The prior question is why main_3 outputs "AB" too.
    I understand the local variable warning.


  • Lifetime Qt Champion

    Cheer luck, what compiler/OS are you using ?



  • gcc version 4.8.1
    OpenSuse 13.1 64

    "Cheer luck," ! OK, sufficient answer, I was not sure about that.
    thx


  • Qt Champions 2016

    @wally123
    It's not exactly luck. Although the memory for the local variable was freed nothing prevents you from reading that memory block. Allocating and freeing memory is just a way of telling the C runtime what is whose. If you try to to write over the memory pointed by the pointer you should get an Access violation , but reading memory that is not yours is a valid operation. The lucky part is that the printf function has not used up that memory location for its own data, this is why you get it printed correctly.

    Kind regards.


  • Lifetime Qt Champion

    @kshegunov Reading from an non-allocated block of memory enters the "Undefined Behavior" territory. While it may "work", it still is luck that he doesn't get an Access Violation.


  • Qt Champions 2016

    @SGaist
    In principle I agree. There's the argument, however, that here there aren't any memory conversions. Basically the memory is read byte by byte without any conversion/dereferencing and the like. C, because this is CRT related, is low-level enough to allow it and I have not seen any OS that actually prevents it. It is certainly a very, very bad way to do things, because if there is paging or other OS-managed memory operations in place, this approach could run into a serious trouble, not to mention that any function can at any point use that part of the stack.

    Kind regards.


  • Lifetime Qt Champion

    All in all, we both agree: it's bad to ignore compiler warnings and try to access unallocated memory :)



  • I also do agree :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.