[m-users.] Compiler warning in my readdir.m

Peter Wang novalazy at gmail.com
Mon Oct 14 15:47:11 AEDT 2019


On Sun, 13 Oct 2019 23:30:37 -0500, Julian Fondren <jfondren at minimaltype.com> wrote:
> On 2019-10-13 23:08, Volker Wysk wrote:
> > Hi
> > 
> > There might be an issue in my fixed version of readdir.m. When building
> > the module, I get a compiler warning:
> > 
> > Making Mercury/os/readdir.o
> > readdir.m: In function ‘readdir_module0’:
> > readdir.m:70:18: warning: assignment discards ‘const’ qualifier from
> > pointer target type [-Wdiscarded-qualifiers]
> > readdir.m: In function ‘readdir_module1’:
> > readdir.m:70:18: warning: assignment discards ‘const’ qualifier from
> > pointer target type [-Wdiscarded-qualifiers]
> > 
> > This is in the C code at this place (the line in the middle):
> > 
> >         if (errno == 0) {
> >            Entry = \"\";
> >            Result = 0;
> > 
> > I've attached the complete file.
> > 
> > Changing the 'Entry =' line to 'Entry = (char*) \"\"' avoids the
> > warning message. I guess it's not an error, but I'm not completely
> > sure. I'm not experienced with the foreign language interface (yet).
> > Maybe it's the type of "Entry" should be "char * const", rahter than
> > "char *".
> > 
> 
> A string literal like "" is going to be in read-only memory, so the
> const stuff helps slightly to prevent accidental writing to it.
> What you're doing though is pretty weird: returning a string just
> so that Mercury can test for that string.
> 
> Probably you arrived at this design because per the pred
> declaration you're obliged to return a string even when there's no
> useful string to return. What you can do though, is just not set
> Entry at all (untested):

You should assign valid values to all output variables, in particular
because the debugger may try to print the values out. You can set a
variable to a constant string like this:

    Entry = MR_make_string_const(""")

Peter


More information about the users mailing list