Thread: Is this program correct ?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    87

    Is this program correct ?

    Code:
    #include <stdio.h>
    
    typedef struct
    {
            double x, y, z;
    
    }vector;
    
    double *vector_iterator(vector *v, int axis)
    {
           switch (axis)
           {
                  case 0: return (&v->x);
                  case 1: return (&v->y);
                  case 2: return (&v->z);
           }
    
    }
    
    int main(void)
    {
           vector v;
    
           v.x = 5;
           v.y = 3;
           v.z = 2;
    
           *(vector_iterator(&v, 0)) = 3; /* Note this */
           printf("%f", v.x);
           return 0;
    
    }
    This is fine with my compiler (Output is 3.000000), but I'm unsure
    about function call on the left side of = operator (lvalue ?)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Apart from this
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function `vector_iterator':
    foo.c:18: warning: control reaches end of non-void function
    If you did fall off the end of the function, you'd get a garbage pointer and a whole heap of trouble when you dereference it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    Quote Originally Posted by Salem View Post
    Apart from this
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function `vector_iterator':
    foo.c:18: warning: control reaches end of non-void function
    If you did fall off the end of the function, you'd get a garbage pointer and a whole heap of trouble when you dereference it.
    I understand what you are saying. I actually got this warning in many other programs when there was no return value specified at the end of the function. What is the solution or can I ignore this warning ?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Return a value like you're supposed to. Try NULL perhaps.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    Quote Originally Posted by MacGyver View Post
    Return a value like you're supposed to. Try NULL perhaps.
    trying to store a value at a NULL address may result in undefined behaviour. I think before the assignment takes place using an assert statement may be helpful :

    assert(vector_iterator(&v, axis) != NULL);

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Of course it's undefined. That is the point. You're supposed to check if the pointer returned is NULL.

    Asserting is another option, of course, but that seems a little heavy to stop the program dead in its tracks for this.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    Quote Originally Posted by MacGyver View Post
    Of course it's undefined. That is the point. You're supposed to check if the pointer returned is NULL.

    Asserting is another option, of course, but that seems a little heavy to stop the program dead in its tracks for this.
    I don't know, I need to do some mathematical calculations in my project and I'm just debugging right now so that's why I'm using assertions.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    >trying to store a value at a NULL address may result in undefined behaviour
    You should know that, the control will never reach that stage unless the precondition within the function are not satisfied. Perhaps you have to include some error checking to make sure the return value is NULL. The logic remains perfect.

    ssharish

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Something like this
    Code:
    double *vector_iterator(vector *v, int axis)
    {
           switch (axis)
           {
                  case 0: return (&v->x);
                  case 1: return (&v->y);
                  case 2: return (&v->z);
           }
           assert(0);  // how did we pass the switch?
           return NULL;
    }
    In debug, we'll drop into the debugger for analysis of the problem.
    In release, at least it will fault in a predictable manner, rather than perhaps stumbling on for a while only to crash in some totally random place.

    Or you could be brutal and call exit() instead of returning.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    One more "solution" would be to have "static double junk;" in function, and return &junk after switch... But, in rare cases is this satisfactory...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program, please take a look
    By Hermisky in forum C++ Programming
    Replies: 2
    Last Post: 02-02-2006, 10:13 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM