Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static const char * - defined but not used

Tags:

c++

qt

We need to define a const static char pointer in each and every header (.h) and source (.cpp) file to comply with company coding standards.

static const char * one_time_param = "ABCDEFG";

When compiled, the compiler is generating lot of "defined but not used" warnings. Does someone have a solution to this issue, please?

-Wno-unused-parameter

Using the above compiler flag, we can suppress these warnings. But, this also suppresses some other unused parameters which might need attention. We tried these solutions which only work for function parameters.

Q_UNUSED

in Qt, and

#define UNUSED(x) ((void)(x))

Previous question of similar kind:

How can I hide "defined but not used" warnings in GCC?

like image 871
ramtheconqueror Avatar asked Nov 29 '11 14:11

ramtheconqueror


People also ask

What is a static const char * in C++?

const char* is a pointer to a constant char, meaning the char in question can't be modified. char* const is a constant pointer to a char, meaning the char can be modified, but the pointer can not (e.g. you can't make it point somewhere else).

Can const char * be reassigned?

char* const says that the pointer can point to a char and value of char pointed by this pointer can be changed. But we cannot change the value of pointer as it is now constant and it cannot point to another char.

How do you declare a constant character in C++?

To declare a constant member function, place the const keyword after the closing parenthesis of the argument list. The const keyword is required in both the declaration and the definition.

Can we use static with const?

So combining static and const, we can say that when a variable is initialized using static const, it will retain its value till the execution of the program and also, it will not accept any change in its value.


2 Answers

It's usually const pointer too in this case, so try to use:

static const char * const one_time_param = "ABCDEFG";
like image 190
kelviN Avatar answered Oct 28 '22 08:10

kelviN


First - the company coding standards are arguably wasting space. If you're going to do that, then use an array instead of a char * so you store just the data and not a pointer and the data:

static const char one_time_param[] = "ABCDEFG";

Next, presumably this is for file identification - that, at least, is what I use it for. There are several things to be aware of, learned from experience over a number of years. (I still like to embed version numbers in the source files - I haven't whole-heartedly moved to DVCS because of this.)

  1. To avoid the warnings, you have to make the symbols visible outside the file.
  2. That, in turn, means you have to make the variable names unique.
  3. I'm currently using names based on file name: jlss_id_filename_c[] etc.

    #ifndef lint
    /* Prevent over-aggressive optimizers from eliminating ID string */
    const char jlss_id_errno_c[] = "@(#)$Id: errno.c,v 3.3 2011/09/07 22:33:45 jleffler Exp $";
    #endif /* lint */
    
  4. The AT&T SVR4 C compiler and support software supported a #ident directive:

    #ident "@(#)$Id: errno.c,v 3.3 2011/09/07 22:33:45 jleffler Exp $"
    

    The compiler included the strings in a 'comments' section in the object file, and a tool (mcs) to manipulate the comments section (options -d to delete it and -c to compress it, IIRC). This section was part of the binary, but not loaded into memory at runtime.

  5. At one point in GCC's evolution, in conjunction with the command line options I was using, I got warnings unless I declared as well as defined the variable, so my 'template' for new source file generates:

    #ifndef lint
    /* Prevent over-aggressive optimizers from eliminating ID string */
    extern const char jlss_id_filename_c[];
    const char jlss_id_filename_c[] = "@(#)$Id$";
    #endif /* lint */
    

    However, I normally remove the declaration these days, and don't get compiler warnings.

  6. As an alternative to using the file name as the basis of variable name, you could generate a UUID or GUID name in hex and use that as the variable name, with a prefix to ensure the first character is alphabetic.

  7. In headers, you don't want that material defined in every source file that includes the header because (a) it becomes a noticable (but not necessarily significant) overhead on program size, and (b) you can't multiply define global variables (you can multiply declare them; that's not a problem). So, my headers have a stanza like:

    #ifdef MAIN_PROGRAM
    #ifndef lint
    /* Prevent over-aggressive optimizers from eliminating ID string */
    const char jlss_id_stderr_h[] = "@(#)$Id: stderr.h,v 10.3 2011/11/28 04:49:24 jleffler Exp $";
    #endif /* lint */
    #endif
    

    Then, when I want the headers to define the values, I have #define MAIN_PROGRAM at the top of the corresponding source file. For example, from running what errno on a program of that name, I get the output:

    errno:
    $Id: errno.c,v 3.3 2011/09/07 22:33:45 jleffler Exp $
    $Id: range.h,v 1.8 2008/02/11 07:39:36 jleffler Exp $
    $Id: stderr.h,v 10.3 2011/11/28 04:49:24 jleffler Exp $
    $Id: errhelp.c,v 8.5 2009/03/02 19:13:51 jleffler Exp $
    $Id: range2.c,v 1.8 2008/02/11 08:44:50 jleffler Exp $
    $Id: stderr.c,v 10.7 2011/11/28 04:49:24 jleffler Exp $
    stderr.c configured with USE_STDERR_FILEDESC
    stderr.c configured with USE_STDERR_SYSLOG
    

Old-style

This is a complete (and very useful) program illustrating the old-style of doing business.

/*
@(#)File:            $RCSfile: al.c,v $
@(#)Version:         $Revision: 1.4 $
@(#)Last changed:    $Date: 1996/08/13 11:14:15 $
@(#)Purpose:         List arguments one per line
@(#)Author:          J Leffler
@(#)Copyright:       (C) JLSS 1992,1996
@(#)Product:         :PRODUCT:
*/

/*TABSTOP=4*/

#include <stdio.h>
#include <stdlib.h>

#ifndef lint
static const char sccs[] = "@(#)$Id: al.c,v 1.4 1996/08/13 11:14:15 johnl Exp $";
#endif

int main(int argc, char **argv)  
{ 
    while (*++argv) 
        puts(*argv);
    return(EXIT_SUCCESS); 
}

NB: When that is compiled, the version string is not included in the binary (or the object file). This does not currently give me any warning when compiled with GCC 4.6.1 compiled on MacOS X 10.7.2:

gcc -m64 -g -O -std=c99 -pedantic -Wall -Wshadow -Wpointer-arith -Wcast-qual \
    -Wstrict-prototypes -Wmissing-prototypes -o al al.c

When I run what al, I get no identification output.

like image 21
Jonathan Leffler Avatar answered Oct 28 '22 07:10

Jonathan Leffler