Thread: User defined functions

  1. #16
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    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
    Guest
    Guest
    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
    28,413
    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
    11
    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
    105
    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
    105
    Quote Originally Posted by Guest 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
    28,413
    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; 11-08-2018 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
    105
    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
    28,413
    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,336
    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.

  11. #26
    Registered User
    Join Date
    Nov 2018
    Posts
    11
    Wont make a new thread- will ask here instead. I have a new task. This time about structures and accesing them. So my task was to write a small database of students (program asks how many students will be entered, then user inputs all the info needed, and last- program shows what was entered). I wrote a basic program...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct data {
        char name[20];
        char surname[20];
        int course;
        char group[10];
    };
    int my_count();
    
    
    int main(){
        struct data *ptr;
        int k,i;
        char name;
        k = my_count();
        ptr = (struct data*)malloc(k*sizeof(struct data));
            for (i=0; i<k; ++i)
            {
                printf("Enter students name:\n");
                    scanf("%s", &(ptr+i)->name);
                printf("Enter students surname:\n");
                    scanf("%s", &(ptr+i)->surname);
                printf("Enter students course:\n");
                    scanf("%d", &(ptr+i)->course);
                printf("Enter students group:\n");
                    scanf("%s", &(ptr+i)->group);
            }
            for (i=0; i<k; ++i)
            {
                printf("\n\n...Info about student: %d...\n", i+1);
                printf("Name-->\t\t |%s|\n",(ptr+i)->name);
                printf("Surname-->\t |%s|\n",(ptr+i)->surname);
                printf("Course-->\t |%d|\n",(ptr+i)->course);
                printf("Group-->\t |%s|\n",(ptr+i)->group);
            }
        free(ptr);
    return 0;
    }
    int my_count(){
        static int k;
        printf("How much students do you want to enter?\n");
        scanf("%d",&k);
        return k;
    }
    However, I need to make user-defiened functions for both- information input and ouput. Tricky part here is that I can use only one input function, to get everything (name,surname,course,group).

    Now I am a bit confused. I tried to use something like "ptr->x" to make function, where "x" should vary, but it isnt correct

  12. #27
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Without real code I can not help you.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  13. #28
    Registered User
    Join Date
    Nov 2018
    Posts
    11
    Quote Originally Posted by stahta01 View Post
    Without real code I can not help you.
    What do you mean? I included my code in the previous post.

  14. #29
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Raismans View Post
    What do you mean? I included my code in the previous post.
    Post your attempt and the error message.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

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