Thread: C function bug?

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    13
    Rep Power
    0

    Question C function bug?


    Hi All,

    I have the following code and something is not quite working...please help!

    void CollectNumber(int **y) {
    int i=4;
    *y=&i;
    }
    void Function(void) {
    unsigned long *tmp;
    CollectNumber(&tmp);
    printf("%d\n", *tmp);
    double a=tmp*3.245955565688887755451225457478887755,b;
    printf(" Enter a number larger than %g !\n", a)
    scanf("%g", b);
    printf("%g\n",b);
    }

    thanks!
    tan
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    68
    Rep Power
    11
    Let's start with fixing this:
    Code:
    CollectNumber(&tmp); 
    printf("%d\n", *tmp);
    tmp doesn't point to any memory that you own, so you'll likely get a segmentation fault by trying to access it.
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1803
    Firstly, why make the people taht you want to help you work so hard? Tell us in what way it is not working!

    However, at a glance, passing back a pointer to a local variable is bound to fail. The variable goes out of scope when the function exits, and the memory used is available for use, and may be modified by other code. In this case, the stack frame of the printf() function called immediatly after CollectNumber() will almost certainly overwrite that of CollectNumber(), if stack frames are optimised out and registers are used, the registers, wil be re-used and modify the value in **tmp. To make this work, declare i as static:
    Code:
    static int i = 4 ;
    or better, pass i back via the function return.
    Code:
    int CollectNumber() 
    { 
        int i=4; 
        return( i ) ;
    }
    .
    Also look again at your levels of indirection. You are setting the pointer tmp to 4, which is probably not what you want, 4 is meaningless for a pointer value. In the printf() you then dereference tmp to display the value at the address 4. This will probably cause an exception, but will certainly produce junk. tmp need not be a pointer, multiplying a pointer by a float makes no sense. The parameter y need not be a pointer-to-pointer, and the type of y does not agree with the type of tmp - for no good reason, use either int of long. Thats just for starters, for such a short peice of code, that's probably enough.

    Note if your compiler raised warnings (which with this code I hope it did), treat them as errors, they are normally semantic errors even if they are not syntactic errors. If no warnings were raised, check the compiler options to report the maximum warning level, your code quality will improve as a result.

    Clifford.
    Last edited by clifford; September 17th, 2003 at 10:11 AM.

IMN logo majestic logo threadwatch logo seochat tools logo