Like Tree4Likes
  • 1 Post By vart
  • 2 Post By iMalc
  • 1 Post By MutantJohn

Writing functions to prove that variables cannot be changed by another function.

This is a discussion on Writing functions to prove that variables cannot be changed by another function. within the C Programming forums, part of the General Programming Boards category; It is said that variables in a function cannot be changed by another function. Only by using pointers can variable ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Location
    Still looking..
    Posts
    35

    Writing functions to prove that variables cannot be changed by another function.

    It is said that variables in a function cannot be changed by another function. Only by using pointers can variable values be changed.

    I am writing some functions to try to prove this theory, but I can't get it right.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int x = 10;
        printf("default x value is %d\n",x);
        try1(x);
        printf("x after try1 is %d\n", x);
        try2(&x);
        printf("x after try2 is %d\n", x);
        
        return 0;
    }
    Code:
    #include <stdio.h>
    
    void try1(int x){
        printf("x in try1 is %d\n", x);
        x++;
        printf("x in try1 after ++ is %d\n", x);
    }
    
    void try2(int *x){
        printf("x in try2 is %d\n", *x);
        *x++;
        printf("x in try2 after ++ is %d\n", *x); // Error, always gives me the address memory number
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    try
    Code:
    (*x)++
    Krabiki likes this.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,331
    Quote Originally Posted by Krabiki View Post
    It is said that variables in a function cannot be changed by another function. Only by using pointers can variable values be changed.
    Or in the case of bad code using a bad index on an array. Example program. Volatile is used to force x to be located on stack to prevent compiler optimizer from using an immediate value instead of x:

    Code:
    #include <stdio.h>
    
    void modifyx(void);
    
    int main()
    {
        volatile int x = 1234;
        printf("original value of x %d\n", x);
        modifyx();
        printf("modified value of x %d\n", x);
    
        return(0);
    }
    
    void modifyx(void)
    {
        volatile int y[1];
        int i;
    
        for(i = 0; i < 30; i++){
            if(y[i] == 1234)
                y[i] = 4321;
        }
    }
    output
    Code:
    original value of x 1234
    modified value of x 4321

  4. #4
    Registered User
    Join Date
    Apr 2013
    Location
    Still looking..
    Posts
    35
    Quote Originally Posted by vart View Post
    try
    Code:
    (*x)++
    Thanks a lot. Hmm why is the parentheses required in this case ?

  5. #5
    Registered User
    Join Date
    Apr 2013
    Location
    Still looking..
    Posts
    35
    Wow rcgldr, thanks for the example, the volatile keyword sounds foreign to me though haha =)

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Krabiki View Post
    Thanks a lot. Hmm why is the parentheses required in this case ?
    It had to do with the precedence of the operators * and ++.
    Without the parentheses, you increment the pointer that is pointing to the variable x.
    With the parentheses, you say, go bring me the value that the pointer is pointing to and THEN increment what you brought me, thus the value. Of course, since we use the ++ operator, then the value is stored where the pointer points to.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    Apr 2013
    Location
    Still looking..
    Posts
    35
    Quote Originally Posted by std10093 View Post
    It had to do with the precedence of the operators * and ++.
    Without the parentheses, you increment the pointer that is pointing to the variable x.
    With the parentheses, you say, go bring me the value that the pointer is pointing to and THEN increment what you brought me, thus the value. Of course, since we use the ++ operator, then the value is stored where the pointer points to.
    Oh thanks for the explanation. Didn't thought of operator precedence being the issue =)

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by rcgldr View Post
    Or in the case of bad code using a bad index on an array. Example program. Volatile is used to force x to be located on stack to prevent compiler optimizer from using an immediate value instead of x:

    Code:
    #include <stdio.h>
    
    void modifyx(void);
    
    int main()
    {
        volatile int x = 1234;
        printf("original value of x %d\n", x);
        modifyx();
        printf("modified value of x %d\n", x);
    
        return(0);
    }
    
    void modifyx(void)
    {
        volatile int y[1];
        int i;
    
        for(i = 0; i < 30; i++){
            if(y[i] == 1234)
                y[i] = 4321;
        }
    }
    output
    Code:
    original value of x 1234
    modified value of x 4321
    That example is undefined behaviour, and pure evil. It could destroy other data on the stack that it should not be touching, not to mention it makes an assumption about the direction of the stack, which is false under other architectures. So in some cases it would not work at all. And then there' the issue that it may be accessing memory outside the bounds of the stack. Or if run under an interpreter you'd probably get who knows what kind of horrible issues.

    Ugh, that's so horribly evil that you've done a disservice by posting it at all.

    Basically "volatile" adds nothing useful to the conversation. If the variable not not have its address taken from the scope it exists in, then for all intents nad purposes, no other code can modify its value. It's as simple as that.
    vart and phantomotap like this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    1,331
    Quote Originally Posted by iMalc View Post
    That example is undefined behaviour.
    The point was to prove that is its possible to change a variable due to a faulty function. In this case a malicious function was used, but a similar thing could happen due to a programming error.

    I have an issue with the original premise of "writing functions to prove that variables cannot be changed by another function", since this is an attempt to prove a "negative" (proving that that something doesn't exist), by creating a set of functions. How large would a set of functions have to be in order to prove "that variables cannot be changed by another function"?

    Quote Originally Posted by iMalc View Post
    Basically "volatile" adds nothing useful to the conversation.
    In the case of microsoft compiler with optimizations enabled, then without the "volatile", there is no instance of the variable "x", instead "x" is replaced by with an immediate value of 1234 by the compiler. I could have used some other method to force the compiler to create an instance of "x", but volatile was the simplest way.
    Last edited by rcgldr; 07-09-2013 at 02:56 AM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,258
    Quote Originally Posted by rcgldr
    I have an issue with the original premise of "writing functions to prove that variables cannot be changed by another function", since this is an attempt to prove a "negative" (proving that that something doesn't exist), by creating a set of functions. How large would a set of functions have to be in order to prove "that variables cannot be changed by another function"?
    Only three, by engineer's induction: if the variable declared in main cannot be changed by try1, try2, and try3, then it cannot be changed by any other function. Duh!
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    1,331
    Quote Originally Posted by rcgldr View Post
    I have an issue with the original premise of "writing functions to prove that variables cannot be changed by another function", since this is an attempt to prove a "negative" (proving that that something doesn't exist), by creating a set of functions. How large would a set of functions have to be in order to prove "that variables cannot be changed by another function"?
    Quote Originally Posted by laserlight View Post
    Only three, by engineer's induction: if the variable declared in main cannot be changed by try1, try2, and try3, then it cannot be changed by any other function.
    I did a search for "engineer's induction" and all I find are joke sites. Do you have a link for "engineer's induction" that explains what this is?

    Rather than trying to prove a negative with some set of functions, it seems it should just be stated that the rules of C and C++ prevent proper functions with defined behavior from modifying the variables of other functions unless some form of pointer or reference is involved.

    The function I created disproves the premise that functions can't modify variable in other functions, but it did so by exploiting an out of range index, which is not a proper function.
    Last edited by rcgldr; 07-09-2013 at 03:49 AM.

  12. #12
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,275
    I think that was actually a joke lol. Wow, the first time I've ever seen one on the C board not made by me. It's a new day!
    iMalc likes this.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by rcgldr View Post
    Rather than trying to prove a negative with some set of functions, it seems it should just be stated that the rules of C and C++ prevent proper functions with defined behavior from modifying the variables of other functions unless some form of pointer or reference is involved.
    Indeed, you were correct earlier that no amount of negative results disproves a positive.

    Yes it should just be stated that the language rules disallow it, e.g. as I tried to in post 8.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    2,690
    Quote Originally Posted by MutantJohn View Post
    I think that was actually a joke lol. Wow, the first time I've ever seen one on the C board not made by me. It's a new day!
    It's not that unusual - here's an example:

    Programing jokes? huh cliche

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. const parameter of function is being changed
    By JonathanS in forum C++ Programming
    Replies: 5
    Last Post: 10-10-2012, 10:56 AM
  2. Replies: 21
    Last Post: 06-24-2011, 03:57 AM
  3. Replies: 6
    Last Post: 12-02-2009, 08:47 AM
  4. Changed my main function, now this.
    By Shamino in forum Windows Programming
    Replies: 3
    Last Post: 02-18-2008, 04:57 PM
  5. Replies: 3
    Last Post: 11-28-2006, 03:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21