Thread: User defined functions

  1. #16
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    34
    Quote Originally Posted by WoodSTokk
    Undefined Behavior! You return a pointer to 'text', but 'text' is a local variable inside the function 'eingabe'.
    At the time the function returns, the variable (and its space in memory) become invalid.
    So the pointer points to a invalid memory.
    You are confusing with something. The function input () returns the value contained in the local variable "text" to the global variable "text". This value is then processed further in the function laenge ().

    Local variables are always temporary within a function, and thus their content. As soon as the function is left, this variable virtually no longer exists. This does not matter here, because each time the program is called, the local variable 'text' is initialized with a new value.

    Nothing and nobody shows points to on an invalid memory area.

  2. #17
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    734
    This does not matter here, because each time the program is called, the local variable 'text' is initialized with a new value.

    Nothing and nobody shows points to on an invalid memory area.
    Please elaborate. How does the initialization of the local variable prevent the issue?

    Code:
    char *eingabe(void)
    {
        char local_text[MAX];
    
        // ...
    
        return strtok(local_text, "\n"); // points into local_text address space
    }
    
    int main(void)
    {
        char *text;
        text = eingabe(); // UB; at time of assignment, local_text is already gone
    }

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,041
    Quote Originally Posted by Kernelpanic
    The function input () returns the value contained in the local variable "text" to the global variable "text".
    Your program doesn't have a global variable named text; it does have three different variables named text, each of which is local to a different function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Nov 2018
    Posts
    6
    Quote Originally Posted by tabstop View Post
    Yeah, that's not it. You need a pointer-to-a-pointer for the syntax to work, and for what you are trying to do to actually succeed, you need to pass in the address of a variable. So your declaration is good, but you need to pass &s1 (and your compiler has been jumping up and down trying to tell you this the whole time).
    Yep, that was that. Big thanks for your help, again!

  5. #20
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    34
    Quote Originally Posted by laserlight View Post
    Your program doesn't have a global variable named text; it does have three different variables named text, each of which is local to a different function.
    Okay, that's the correct technical designation. Is that a global variable?
    Code:
    #include <stdio.h>
    
    
    int x = 111;
    
    
    int main(void)
    {
        printf("%d", x);
        return(0);
    }

  6. #21
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    34
    Quote Originally Posted by -Adrian View Post
    Please elaborate. How does the initialization of the local variable prevent the issue?

    Code:
    char *eingabe(void)
    {
        char local_text[MAX];
    
        // ...
    
        return strtok(local_text, "\n"); // points into local_text address space
    }
    
    int main(void)
    {
        char *text;
        text = eingabe(); // UB; at time of assignment, local_text is already gone
    }
    I don't understand these question. Where is the problem? Where can this goes wrong?

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,041
    Yes, x is a global variable as it has external linkage (i.e., unless hidden by a more local name, the name x refers to the object represented by that variable, globally as in throughout the program). If you had declared x to be static, we might call it a file scope variable as it would have internal linkage (i.e., unless hidden by a more local name, the name x refers to the object represented by that variable, throughout that translation unit, which typically means that source file, but if it was declared in a header, then the source file + header).

    Quote Originally Posted by Kernelpanic
    Where is the problem? Where can this goes wrong?
    The problem is that in main, text points to the first element of an array that no longer exists, so if you try to access what text points to, it results in undefined behaviour.
    Last edited by laserlight; 5 Days Ago at 03:38 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    34
    Quote Originally Posted by laserlight View Post
    Yes, x is a global variable as it has external linkage (i.e., unless hidden by a more local name, the name x refers to the object represented by that variable, globally as in throughout the program).
    . . .
    Thanks for Your answer. - The first part . . . please a little bit later.

    The problem is that in main, text points to the first element of an array that no longer exists, so if you try to access what text points to, it results in undefined behaviour.
    But the second part - I do not understood the problem in this case.


    Okay, the array is here not longer exist - the local variable is no longer exist . . . and . . .
    Maybe You mean a fundamental problem, but in this case I can not see it.

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,041
    Quote Originally Posted by Kernelpanic
    Okay, the array is here not longer exist - the local variable is no longer exist . . . and . . .
    Maybe You mean a fundamental problem, but in this case I can not see it.
    Yes, it is a "fundamental problem":
    Quote Originally Posted by C11 Annex J.2
    The behavior is undefined in the following circumstances:
    (...)
    • The value of a pointer to an object whose lifetime has ended is used (6.2.4).
    Quote Originally Posted by C11 Clause 6.2.4 Paragraph 2 (part)
    If an object is referred to outside of its lifetime, the behavior is undefined. The value of a pointer becomes indeterminate when the object it points to (or just past) reaches the end of its lifetime.
    So what does all this stuff about undefined behaviour mean?
    Quote Originally Posted by C11 Clause 3.4.3
    undefined behavior
    behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements

    NOTE Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

    EXAMPLE An example of undefined behavior is the behavior on integer overflow.
    In other words, your example program from post #7 could have "unpredictable results". Or the compiler could detect this situation and refuse to compile your code. Or perhaps as an Easter egg joke, the compiler could detect this situation, document the joke somewhere deep in the compiler documentation, and so arrange for your program to print "Hello world!" instead of the program that you had in mind.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,327
    Quote Originally Posted by Kernelpanic View Post
    Thanks for Your answer. - The first part . . . please a little bit later.



    But the second part - I do not understood the problem in this case.


    Okay, the array is here not longer exist - the local variable is no longer exist . . . and . . .
    Maybe You mean a fundamental problem, but in this case I can not see it.
    You are returning a pointer to a particular memory address; however, once the function ends you no longer own that memory address and the system has/could have given that memory to some other program which is using it for its own purposes, which means that value that is there is no longer the value you were expecting/hoping it to be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User-defined functions
    By naya22 in forum C++ Programming
    Replies: 6
    Last Post: 04-10-2007, 11:25 AM
  2. User defined functions
    By alexpos in forum C Programming
    Replies: 2
    Last Post: 10-23-2005, 02:53 PM
  3. New to user defined functions
    By Extropian in forum C Programming
    Replies: 4
    Last Post: 08-15-2005, 10:45 PM
  4. user-defined functions
    By MyntiFresh in forum C++ Programming
    Replies: 22
    Last Post: 07-10-2005, 01:11 PM
  5. Help! User defined functions
    By AdrenalinFlow in forum C Programming
    Replies: 3
    Last Post: 02-22-2003, 07:36 PM

Tags for this Thread