Author Topic: %I64d or %lld in the plugins\compilergcc\depslib\src\cache.c  (Read 313 times)

Online ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6063
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
%I64d or %lld in the plugins\compilergcc\depslib\src\cache.c
« on: February 04, 2025, 05:35:09 am »
Code
		/* C::B patch: Compatibility with 64 bit compiler / OS */
#if defined(_WIN64)
sscanf(buf, "%I64d %n", &timeval, &n);
#else
sscanf(buf, "%lld %n", &timeval, &n);
#endif

When I build the C::B, I see some code in the "plugins\compilergcc\depslib\src\cache.c" cause some warnings:

Code
D:\code\cb\cb_sf_git\cccrash2019\src\plugins\compilergcc\depslib\src\cache.c: In function 'cache_write':
D:\code\cb\cb_sf_git\cccrash2019\src\plugins\compilergcc\depslib\src\cache.c:173:33: warning: format '%d' expects argument of type 'int', but argument 3 has type 'time_t' {aka 'long long int'} [-Wformat=]
  173 |                 fprintf(f, "%I64d %s\n", h->time, h->file);
      |                             ~~~~^        ~~~~~~~
      |                                 |         |
      |                                 int       time_t {aka long long int}
      |                             %I64lld


I'm using msys2/mingw64's g++ compiler, and the "#if defined(_WIN64)" is true, so my question is:

why not just use the %lld?
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Wkerry

  • Multiple posting newcomer
  • *
  • Posts: 76
Re: %I64d or %lld in the plugins\compilergcc\depslib\src\cache.c
« Reply #1 on: February 04, 2025, 08:39:58 am »
Try the following:
      #if  (_USE_LONG_TIME_T)
         sscanf(buf, "%ld %n", &timeval, &n);
      #else
         #if defined(PRId64)
            sscanf(buf, "%" PRId64 " %n", &timeval, &n);
         #else
            sscanf(buf, "%lld %n", &timeval, &n);
         #endif
      #endif

and
      #if  (_USE_LONG_TIME_T)
         fprintf(f, "%ld %s\n", h->time, h->file);
      #else
         #if defined(PRId64)
            fprintf(f, "%" PRId64 " %s\n", h->time, h->file);
         #else
            fprintf(f, "%lld %s\n", h->time, h->file);
         #endif
      #endif

This works better than the existing code. Give it a quick hack/ try, but you may find a better way.

Online ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6063
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Try the following:
      #if  (_USE_LONG_TIME_T)
         sscanf(buf, "%ld %n", &timeval, &n);
      #else
         #if defined(PRId64)
            sscanf(buf, "%" PRId64 " %n", &timeval, &n);
         #else
            sscanf(buf, "%lld %n", &timeval, &n);
         #endif
      #endif

and
      #if  (_USE_LONG_TIME_T)
         fprintf(f, "%ld %s\n", h->time, h->file);
      #else
         #if defined(PRId64)
            fprintf(f, "%" PRId64 " %s\n", h->time, h->file);
         #else
            fprintf(f, "%lld %s\n", h->time, h->file);
         #endif
      #endif

This works better than the existing code. Give it a quick hack/ try, but you may find a better way.

Hi, thanks, I just tested it. I think your suggested code is better than the current code.  :)
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.