#1
  1. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95

    control access to structure data in C


    yes i know C++ is made for just this purpose, encapsulating data, but lets say i was one of those people who does things the hard way, so:

    is there any possible hideously ugly way (uglier == better) to do this. i thought there i would be able to define a macro function like [x], but they have to be alphanumeric. lets say i have this structure with a member,
    Code:
    u_char                  **packet_q;
    now whenever someone tries to access it like,
    Code:
    packet_q[x]; or packet_q[x][y];
    i want to bounds check first against another member(s) of the structure. i could use a regular function, but i'd rather be able to doit simply, like:
    Code:
    packet_q$x$;
    or something similar. is that possible? if not with macros, then some sort of preprocessor code that will expand to what i want? the main problem i see is the lack of a *this pointer for structures. the max value i want to check against is a member of the same structure as packet_q. so i was thinking if i can get the address of the packet_q i could figure out it's offset in relation to the max value i need. does that make sense?

    for now i'm doing this:
    Code:
    #define a(x) ( (x <= max) ? x : max )
    
    and 
    
    packet_q[a(x)]
    but this means i need a #define or global variable, max, or worse adding another variable to pass to the macro.
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Are you trying to get a prize for writting obscure code or something?

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,378
    Rep Power
    1510
    I know that the [] are operators. You might check if you can overload them, but I don't think so. If not, anyone who uses that member is still going to use some other operator with it-- you can overload most of the rest of the operators to do the bounds checking.
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn
  6. #4
  7. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    Originally posted by mitakeet
    Are you trying to get a prize for writting obscure code or something?
    lol i think so :D i get these strange ideas in my head and then i get determined to make them work. i was toying with overloading [] in C++, but g++ won't compile unless [] pertains to a certain class. ah well at least i tried.

IMN logo majestic logo threadwatch logo seochat tools logo