Question on some 'borrowed' code

This is a discussion on Question on some 'borrowed' code within the C Programming forums, part of the General Programming Boards category; I have a question regarding the following lines of code Code: #include <math.h> struct st { double x[1]; double (*func)(double); ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195

    Question on some 'borrowed' code

    I have a question regarding the following lines of code

    Code:
    #include <math.h> 
    struct st { 
        double x[1]; 
        double (*func)(double); 
    
    
    }; 
    
    
    int main (void) { 
        struct st s; 
        s.func = sqrt; 
        s.x[0] = 1.0; 
        s.x[1] = 33.7;  /* bad */ 
        (void)s.func(s.x[0]); 
        return  0; 
    
    
    }
    How can assigning 33.7 to s.x[1] possibly overwrite the function
    pointer? I thought assigning s.x[1]=33.7 was legal because it was within the array boundries.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by cdalten
    I have a question regarding the following lines of code

    Code:
    #include <math.h> 
    struct st { 
        double x[1]; // your making an array of size 1, indexed from 0 to 0
        double (*func)(double); 
    
    
    }; 
    
    
    int main (void) { 
        struct st s; 
        s.func = sqrt; 
        s.x[0] = 1.0; 
        s.x[1] = 33.7;  /* bad */ 
        (void)s.func(s.x[0]); 
        return  0; 
    
    
    }
    How can assigning 33.7 to s.x[1] possibly overwrite the function
    pointer? I thought assigning s.x[1]=33.7 was legal because it was within the array boundries.
    Look at the red.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Okay, I see. Once again I'm demonstrating my lack of any real mastery over the C language. Ugghhh...

  4. #4
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by cdalten
    Okay, I see. Once again I'm demonstrating my lack of any real mastery over the C language. Ugghhh...

    Actually that's a very common problem in the C world, so don't feel bad.

    I've been programming C for more than 20 years now, and, on occasion, still make mistakes like that. Now granted, it is usually when modifying code that I've previous written and I forget to increase the size of the array, but it is still an array sizing problem.

    The hard part is that when you have large amounts of code and they start failing with the most bizarre errors -- that undersized array can sometimes be very hard to find....
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hash table code question
    By Boxknife in forum C Programming
    Replies: 20
    Last Post: 08-13-2008, 06:55 AM
  2. Code Question
    By lyoncourt in forum C Programming
    Replies: 3
    Last Post: 09-16-2007, 05:33 PM
  3. Hi all i have a question about a code
    By iweapons in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2007, 12:05 PM
  4. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 01:22 PM
  5. End of Code Loop Question
    By JuanSverige in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 11:35 AM

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