Re: Help-a-Dummy Please
- From: "Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx>
- Date: Mon, 8 Sep 2008 11:42:49 -0500
Victor Bazarov wrote:
Ben Voigt [C++ MVP] wrote:
File switches.hThat does not look correct. An array with no size? It's basically
#ifndef SWITCHES
#define SWITCHES
class switches
{
public:
switches(int new_numArgs, char* new_Args[]);
~switches();
void showSwitches();
private:
int numArgs;
char * Args[];
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.
};Now, here you _dereference_ the 'Args' pointer that does *not* have
#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;
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
.
- References:
- Help-a-Dummy Please
- From: PvdG42
- Re: Help-a-Dummy Please
- From: Victor Bazarov
- Re: Help-a-Dummy Please
- From: Ben Voigt [C++ MVP]
- Re: Help-a-Dummy Please
- From: Victor Bazarov
- Help-a-Dummy Please
- Prev by Date: Re: A problem about static object in DLL
- Next by Date: problem with SQL Server 2005 sp called from VC++ 6.0
- Previous by thread: Re: Help-a-Dummy Please
- Next by thread: Re: Help-a-Dummy Please
- Index(es):
Relevant Pages
|