Re: Help-a-Dummy Please

Tech-Archive recommends: Fix windows errors by optimizing your registry



Victor Bazarov wrote:
Ben Voigt [C++ MVP] wrote:
File switches.h
#ifndef SWITCHES
#define SWITCHES

class switches
{
public:
switches(int new_numArgs, char* new_Args[]);
~switches();

void showSwitches();
private:
int numArgs;
char * Args[];
That does not look correct. An array with no size? It's basically
equivalent to

char ** Args;

(a pointer to a pointer to char).

Not at all.

Ah.. Thank you for forcing me to look. According to 8.3.4/1, it's an
array of "unknown bound of char*, an incomplete object type". Those
are incomplete types (along with 'void'), and, according to 3.9/6,
"Objects shall not be defined to have an incomplete type", which to
me means the line
char * Args[];

should make the program ill-formed. Does it? Or are we talking of
some Microsoft extension to the language that makes the code
well-formed?

Well, that makes the error the line in main which defines a variable of that
type. However casting a pointer, to an memory region with the extra space
specifically allocated (placement new, anyone?), could work reliably.

Declaring an incomplete type is most certainly not a problem, nor is having
a pointer to an incomplete type (think void*).

I think the MS extension is that the unknown bound is treated as size zero.



};

#endif

File switches.cpp
#include <iostream>
#include "switches.h"

using std::cout;
using std::endl;

switches::switches(int new_numArgs, char * new_Args[])
{
numArgs = new_numArgs;
*Args = *new_Args;
Now, here you _dereference_ the 'Args' pointer that does *not* have
any valid value (yet). That's just plain wrong. I strongly
recommend

Actually, it's writing past the end of the array, which is wrong.

How do we know it's writing past the end if we don't know where the
end actually is?

No
uninitialized values are used.

OK, my assessment was based on a wrong premise, I see it now. Doesn't
make yours correct, though.

The assignment is equivalent to:

Args[0] = new_Args[0];

Args is an array, not actually a pointer. It automatically decays
to a pointer with a well-defined value.

This isn't Java or .NET where an array type is a reference to an
array stored elsewhere that needs to be initialized.

abandoning the idea of using naked pointers and instead start using
standard containers. Declare 'Args' to be a vector of string
objects and initialise it correctly.

That would be good.

}



V


.



Relevant Pages

  • Re: Banks and economy
    ... Such a declarator makes the contained identifier ... "array of" specification are adjacent, ... type', and as I quoted to Phil, the incomplete type can't be ... parameters since array types used there are converted to pointer types. ...
    (alt.lang.asm)
  • Re: pointer to an int array
    ... ("array of int of unknown size"). ... pointer to incomplete type (again, this is not a constraint violation, ...
    (comp.lang.c)
  • Re: Help-a-Dummy Please
    ... #ifndef SWITCHES ... (a pointer to a pointer to char). ... Actually, it's writing past the end of the array, which is wrong. ...
    (microsoft.public.vc.language)
  • Re: Help-a-Dummy Please
    ... #ifndef SWITCHES ... (a pointer to a pointer to char). ... array of "unknown bound of char*, ...
    (microsoft.public.vc.language)
  • Re: The result of ++ is not an lvalue?
    ... the end of an array). ... Another way a perfectly good pointer value can fail to ... incomplete type, e.g., a `void*' or a `struct opaque*'. ... And if somebody nit-picks that `struct opaque' might ...
    (comp.lang.c)