Thread: Fallbacks and suggestions

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,138

    Fallbacks and suggestions

    Mostly done with fallbacks for defines that should be in limits.h (now switched to checking for a define called NO_STDC when including them directly is unwanted)

    Begun work on fbstdint.h (started prefixing them with fb to indicate the header does fallbacks for anything not already defined) and decided I'd start splitting it off into it's own project then get my alu project to rely on those instead then setup the proper github project for it (or maybe gitlab, need to investigate the difference). Thought I'd post my initial code for the fbstdint.h so peops can point out any mistakes while I start preping the github project for it to be split off into.

    Code:
    #ifndef FBSTDINT_H
    #define FBSTDINT_H
    
    #include "fblimits.h"
    
    #ifndef NO_STDC
    #ifdef __cplusplus
    #include <cstdbool>
    #include <cstddef>
    #include <cstdint>
    #include <cinttypes>
    #else
    #include <stdbool.h>
    #include <stddef.h>
    #include <stdint.h>
    #include <inttypes.h>
    #endif
    #endif
    
    #ifndef __size_t_defined
    #define __size_t_defined
    #undef SIZE_MAX
    #undef SIZE_T_C
    #undef SIZE_T_WIDTH
    #undef SIZEOF_SIZE_T
    #undef SIZE_END_BIT
    #undef PRI_SIZE_T
    #undef SCN_SIZE_T
    typedef unsigned long size_t;
    #define SIZE_MAX ULONG_MAX
    #endif
    
    
    #ifndef SIZEOF_SIZE_T
    #define SIZEOF_SIZE_T SIZEOF(SIZE_MAX)
    #endif
    
    #ifndef SIZE_T_WIDTH
    #define SIZE_T_WIDTH (SIZEOF_SIZE_T * CHAR_BIT)
    #endif
    
    #ifndef SIZE_END_BIT
    #define SIZE_END_BIT ~(SIZE_MAX>>1)
    #endif
    
    #ifndef SIZE_T_C
    #if SIZE_MAX > ULONG_MAX
    #define SIZE_T_C(VAL) VAL##LLU
    #else
    #define SIZE_T_C(VAL) VAL##LU
    #endif
    #endif
    
    #ifndef PRI_SIZE_T
    #if SIZE_MAX > ULONG_MAX
    #define PRI_SIZE_T "ll"
    #else
    #define PRI_SIZE_T "l"
    #endif
    #endif
    
    #ifndef SCN_SIZE_T
    #if SIZE_MAX > ULONG_MAX
    #define SCN_SIZE_T "ll"
    #else
    #define SCN_SIZE_T "l"
    #endif
    #endif
    
    #endif /* FBSTDINT_H */

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    765
    Problematic:

    Code:
    #ifndef SIZE_END_BIT
    
    #define SIZE_END_BIT ~(SIZE_MAX>>1)
    
    #endif
    Right shifts of signed integers are implementation dependent... Could be a Logical or Arithmetic shift (ISO 9989).

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,091
    Quote Originally Posted by flp1969 View Post
    Problematic:

    Code:
    #ifndef SIZE_END_BIT
    
    #define SIZE_END_BIT ~(SIZE_MAX>>1)
    
    #endif
    Right shifts of signed integers are implementation dependent... Could be a Logical or Arithmetic shift (ISO 9989).
    That's only the case if the left operand is negative, but here SIZE_MAX should be positive, and it looks like it would be unsigned.
    Last edited by laserlight; 08-07-2020 at 04:46 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,138
    Quote Originally Posted by flp1969 View Post
    Problematic:

    Code:
    #ifndef SIZE_END_BIT
    
    #define SIZE_END_BIT ~(SIZE_MAX>>1)
    
    #endif
    Right shifts of signed integers are implementation dependent... Could be a Logical or Arithmetic shift (ISO 9989).
    Quote Originally Posted by laserlight View Post
    That's only the case if the left operand is negative, but here SIZE_MAX should be positive, and it looks like it would be unsigned.
    From my experience it certainly always has been unsigned, to my knowledge ssize_t was supposed to resolve the need for a signed version. flp1969, do you happen to know of a scenario where it is signed?

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,795
    What compiler are you using that doesn't have size_t?! size_t has been part of C since at least c89 and possibly even before that

    Edit: I also don't understand what you mean by "fallbacks for defines that should be in limits.h" when size_t is supposed to be in stddef.h (although it could be in other places also). I guess I'm just confused about what you're trying to achieve...
    Last edited by Hodor; 08-07-2020 at 06:02 AM.

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    747
    Maybe awsdert is trying to write code that will work in a language that is similar to but not exactly C? (If the language doesn't have all of the definitions then it's not actually C, or it's a very old version of C.)

  7. #7
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,138
    Quote Originally Posted by Hodor View Post
    What compiler are you using that doesn't have size_t?! size_t has been part of C since at least c89 and possibly even before that

    Edit: I also don't understand what you mean by "fallbacks for defines that should be in limits.h" when size_t is supposed to be in stddef.h (although it could be in other places also). I guess I'm just confused about what you're trying to achieve...
    An example of defines that should both be in limits.h but not always is the LONG_LONG_* and LLONG_*, I'm certain I read somewhere that at one point it was only LONG_LONG_*, there's also the kernel where at the moment it's treated as part of the main kernel header, it would be nice to unify the surface level includes in both user and kernel land, it is also for that reason that I cannot assume size_t is defined hence the check for __size_t_defined. Basically I aiming for using user level includes if available but have a fallback for when I can't and let those same fall backs work in the unlikely event the user level headers missed something (or just want an outright replacement for faulty/incomplete functions like sprintf in microsoft headers in some cases), finally I also want to use this library as the default in mitsy (the hobby compiler I'm attempting to make... I have too many hobbies :\) so that it doesn't care whether it is using windows kit or linux headers or even another compiler's headers, it just wants to do one job, compile, what headers to use should be a user decision, not a compiler decision

  8. #8
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,138
    Quote Originally Posted by christop View Post
    Maybe awsdert is trying to write code that will work in a language that is similar to but not exactly C? (If the language doesn't have all of the definitions then it's not actually C, or it's a very old version of C.)
    To be honest I kinda wanna try compiling under the K&C compiler at a later date so anything I might have to define is something I wanna make sure has a fallback definition, I also wanna try passing to the Primeval C compiler once I actually get mitsy working, not a big deal on that last one though since that's on the level of dumb/joke challenges taken just because interested in trying.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEW PC, suggestions
    By Shadow in forum Tech Board
    Replies: 9
    Last Post: 12-29-2007, 09:57 PM
  2. PSU Suggestions?
    By ober in forum Tech Board
    Replies: 9
    Last Post: 06-02-2005, 03:20 PM
  3. Suggestions
    By Taikon in forum C Programming
    Replies: 1
    Last Post: 03-26-2005, 11:28 AM
  4. Any suggestions
    By sweets in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 05-09-2002, 10:10 AM
  5. Suggestions on IDE????
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 04-18-2002, 02:47 PM

Tags for this Thread