Incorrect value for __argc

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Incorrect value for __argc

John Brown
The following program is supposed to show the number of arguments
passed to it at run time. It produces the correct result on
Windows XP/gcc-4.8.1, but on Windows 8.1/gccc-4.8.1 it always prints 0.
The program works on Windows 8.1 if it is compiled with the Microsoft
compiler (from Visual Studio 2015 Community Edition).
 

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

int main()
{
   printf ("ARGC : %d\n", __argc);
   printf ("ARGC : %d\n", *__p___argc());
   return 0;
}

Regards,
John Brown.


------------------------------------------------------------------------------

_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Incorrect value for __argc

Derek Hunter-2
On 17/08/2015 06:39, John Brown wrote:

> The following program is supposed to show the number of arguments
> passed to it at run time. It produces the correct result on
> Windows XP/gcc-4.8.1, but on Windows 8.1/gccc-4.8.1 it always prints 0.
> The program works on Windows 8.1 if it is compiled with the Microsoft
> compiler (from Visual Studio 2015 Community Edition).

> #include <stdio.h>
> #include <stdlib.h>
>
> int main()
> {
>     printf ("ARGC : %d\n", __argc);
>     printf ("ARGC : %d\n", *__p___argc());
>     return 0;
> }

I have never seen this construct before so I googled it and my first
impression was that it was some kind of Microsoft specific macro to
return the widechar version of command line parameters.

Is there some reason you are not using the normal method? Here's my
declaration of main():

int main(int argc, char *argv[]);

Cheers,
Derek.


------------------------------------------------------------------------------
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Incorrect value for __argc

Keith Marshall-3
In reply to this post by John Brown
On 17/08/15 06:39, John Brown wrote:
> The following program is supposed to show the number of arguments
> passed to it at run time.

PEBKAC.  You have no right to assume any such thing; what this program
does exhibit is undefined behaviour.  With (my) line numbers inserted:

> 1: #include <stdio.h>
> 2: #include <stdlib.h>
> 3:
> 4: int main()
> 5: {
> 6:   printf ("ARGC : %d\n", __argc);
> 7:   printf ("ARGC : %d\n", *__p___argc());
> 8:   return 0;
> 9: }

Line numbers 6 and 7 refer to variables within the namespace reserved
for the compiler implementation; as such, they should not be addressed
in user space, and when you do so the behaviour is strictly undefined.

> The program works on Windows 8.1 if it is compiled with the Microsoft
> compiler (from Visual Studio 2015 Community Edition).

What meaning those variables may have to Microsoft's compiler is quite
irrelevant; their names begin with two initial underscores, marking them
as reserved, and implementation specific.  Our implementation may use
them for any alternative purpose whatsoever, (or may not have any use
for them at all).  If they are used, their use is undocumented.  When
you buy into undefined behaviour, you get exactly what you pay for: any
(unpredictable) outcome is possible.  There is no such thing as a
"correct" value for "__argc", (therefore neither can any value be
legitimately described as "incorrect").

--
Regards,
Keith.

------------------------------------------------------------------------------
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Incorrect value for __argc

John Brown
On Mon, 17 Aug 2015 20:53:23 +0100, Keith Marshall wrote:
>
> On 17/08/15 06:39, John Brown wrote:
> > The following program is supposed to show the number of arguments
> > passed to it at run time.
>
> PEBKAC. You have no right to assume any such thing; what this program
> does exhibit is undefined behaviour.

[Explanation of why it is bad to access __argc snipped]

> --
> Regards,
> Keith.
>

Do you mind if I forward this to the glib maintainers? It's their code, not mine.

Regards,
John Brown.



------------------------------------------------------------------------------

_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Incorrect value for __argc

Sergio NNX
> Is there some reason you are not using the normal method? Here's my
> declaration of main():
 
> int main(int argc, char *argv[]);

Maybe some people are just masochists?

------------------------------------------------------------------------------

_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Incorrect value for __argc

Keith Marshall-3
In reply to this post by John Brown
On 18/08/15 01:01, John Brown wrote:

> On Mon, 17 Aug 2015 20:53:23 +0100, Keith Marshall wrote:
>>
>> On 17/08/15 06:39, John Brown wrote:
>>> The following program is supposed to show the number of arguments
>>> passed to it at run time.
>>
>> PEBKAC.  You have no right to assume any such thing; what this program
>> does exhibit is undefined behaviour.  
>
> [Explanation of why it is bad to access __argc snipped]
>
>
> Do you mind if I forward this to the glib maintainers?

Feel free.  Why would I mind?  Anything posted here -- or indeed on any
such public mailing list -- is effectively in the public domain, but it
may be more convenient to simply point them to the list archive:
http://thread.gmane.org/gmane.comp.gnu.mingw.user/44812/focus=44814

--
Regards,
Keith.

------------------------------------------------------------------------------
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Incorrect value for __argc

John Brown



On Tue, 18 Aug 2015 05:40:44 +0100, Keith Marshall wrote:

> On 18/08/15 01:01, John Brown wrote:
>> On Mon, 17 Aug 2015 20:53:23 +0100, Keith Marshall wrote:
>>>
>>> On 17/08/15 06:39, John Brown wrote:
>> Do you mind if I forward this to the glib maintainers?
>
> Feel free.  Why would I mind?  Anything posted here -- or indeed on any
> such public mailing list -- is effectively in the public domain, but it
> may be more convenient to simply point them to the list archive:
> http://thread.gmane.org/gmane.comp.gnu.mingw.user/44812/focus=44814
>

While attempting to build glib-2.44.1 (the latest stable release for Linux)
I discovered that they fixed it. From the ChangelLog:

> commit 24d614357a1ab42af8a55db189a8ac05f67c013d
> Author: Ignacio Casal Quinteiro <[hidden email]>
> Date:   Mon Jul 14 08:54:45 2014 +0200
>
>     gspawn-win32: do not rely on __argc and __argv global vars.
>
>     Since we are getting passed Unicode values these global vars
>     might not have the correct value. Instead always get the wide
>     arguments  and convert them to utf8 to use them.
>
>     https://bugzilla.gnome.org/show_bug.cgi?id=733146

Their reasoning is not the same as yours, but the variables are gone.

Unfortunately this version cannot be built by MinGW, but that will
be the subject of another post.

Regards,
John Brown.

     
------------------------------------------------------------------------------
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe