Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Returning char* from function in C ?
Forum Updated to NodeBB v4.3 + New Features

Returning char* from function in C ?

Scheduled Pinned Locked Moved Solved C++ Gurus
12 Posts 3 Posters 4.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    deleted28
    wrote on last edited by
    #1

    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;
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Offline
        D Offline
        deleted28
        wrote on last edited by deleted28
        #3

        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 ?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          What correct result ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • D Offline
            D Offline
            deleted28
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Cheer luck, what compiler/OS are you using ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • D Offline
                D Offline
                deleted28
                wrote on last edited by
                #7

                gcc version 4.8.1
                OpenSuse 13.1 64

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

                kshegunovK 1 Reply Last reply
                0
                • D deleted28

                  gcc version 4.8.1
                  OpenSuse 13.1 64

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

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by kshegunov
                  #8

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

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

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

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    kshegunovK 1 Reply Last reply
                    0
                    • SGaistS SGaist

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

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #10

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

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

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

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1
                        • D Offline
                          D Offline
                          deleted28
                          wrote on last edited by
                          #12

                          I also do agree :)

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved