Thread overview | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 13, 2002 Multiple void file pointer definition with new Q. | ||||
---|---|---|---|---|
| ||||
I'm trying to initialize a pointer to a group of pointers in a class, and while this command worked in other compilers, I get an Error: integer constant expression expected. keyx.akeyf = new (void (*[keyx.numf])()); Using a constant in place of keyx.numf removes the error, however, I can't use a constant in this case, I need a variable. If anyone can point out the proper syntax I'd appreciate it. |
June 13, 2002 Re: Multiple void file pointer definition with new Q. | ||||
---|---|---|---|---|
| ||||
Posted in reply to E. Trelmar | "E. Trelmar" wrote: > I'm trying to initialize a pointer to a group of pointers in a class, and while > this command worked in other compilers, I get an Error: integer constant > expression expected. > keyx.akeyf = new (void (*[keyx.numf])()); Try: keyx.akeyf = new ( void * ) [ keyx.numf ]; You can change the code like: #ifdef __DMC__ keyx.akeyf = new ( void * ) [ keyx.numf ]; #else keyx.akeyf = new (void (*[keyx.numf])()); #endif > Using a constant in place of keyx.numf removes the error, however, I can't use a > constant in this case, I need a variable. > If anyone can point out the proper syntax I'd appreciate it. |
June 13, 2002 Re: Multiple void file pointer definition with new Q. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jan Knepper | In article <3D08FE45.A0D69045@smartsoft.cc>, Jan Knepper says... > >"E. Trelmar" wrote: > >> I'm trying to initialize a pointer to a group of pointers in a class, and while >> this command worked in other compilers, I get an Error: integer constant >> expression expected. >> keyx.akeyf = new (void (*[keyx.numf])()); > >Try: >keyx.akeyf = new ( void * ) [ keyx.numf ]; > >You can change the code like: > >#ifdef __DMC__ >keyx.akeyf = new ( void * ) [ keyx.numf ]; >#else >keyx.akeyf = new (void (*[keyx.numf])()); >#endif > >> Using a constant in place of keyx.numf removes the error, however, I can't use a >> constant in this case, I need a variable. >> If anyone can point out the proper syntax I'd appreciate it. > Tried that syntax, got this result: Error: need explict cast to convert from: void ** to : void (*C func)()* I've been trying a wide variety of syntaxes, but I can't find one that doesn't generate errors. |
June 13, 2002 Re: Multiple void file pointer definition with new Q. | ||||
---|---|---|---|---|
| ||||
Posted in reply to E. Trelmar | You can try: void (**p)() = new (void(*)())[100]; // okay with DMC and gcc, but BCC is buggy here... The best is: typedef void (*F)(); F *p = new F[100]; // easy to understand by humans, works with *any* compiler In other words, KISS... no offense! Laurentiu "E. Trelmar" <E._member@pathlink.com> wrote in message news:aeat4v$v79$1@digitaldaemon.com... > I'm trying to initialize a pointer to a group of pointers in a class, and while > this command worked in other compilers, I get an Error: integer constant > expression expected. > keyx.akeyf = new (void (*[keyx.numf])()); > Using a constant in place of keyx.numf removes the error, however, I can't use a > constant in this case, I need a variable. > If anyone can point out the proper syntax I'd appreciate it. > > |
June 14, 2002 Re: Multiple void file pointer definition with new Q. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jan Knepper | Jan Knepper schrieb...
> > keyx.akeyf = new (void (*[keyx.numf])());
>
> Try:
> keyx.akeyf = new ( void * ) [ keyx.numf ];
Hi Jan, that's not the same! E. Trelmars new expression should result in a array of pointer to function returning void. Your Expression would result in ONE void pointer-pointer that is immediatly indexed with keyx.numf resulting in an uninitialized void pointer.
Regards,
Heinz
|
June 14, 2002 Re: Multiple void file pointer definition with new Q. | ||||
---|---|---|---|---|
| ||||
Posted in reply to E. Trelmar | E. Trelmar schrieb...
> keyx.akeyf = new (void (*[keyx.numf])());
> Using a constant in place of keyx.numf removes the error, however, I can't use a
> constant in this case, I need a variable.
> If anyone can point out the proper syntax I'd appreciate it.
Seems DMC has a problem here. But there is a workaround! I assume that akeyf is declared like this:
struct {
//... something else
void (**akeyf)();
} keyx;
Instead of
keyx.akeyf = new (void (*[keyx.numf])());
that should be correct you can introduce a typedef:
typedef void (*FUNC_PTR)();
keyx.akeyf = new FUNC_PTR[keyx.numf];
Now DMC compiles!
Regards,
Heinz
|
June 14, 2002 Re: Multiple void file pointer definition with new Q. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Laurentiu Pancescu | Laurentiu Pancescu schrieb...
> void (**p)() = new (void(*)())[100]; // okay with DMC and gcc, but BCC is
> buggy here...
That should not be ok, because the new-expression end after the last ')' giving one pointer to pointer to function that is indexed with 100! Shouldn't work on any compiler. The typedef-ed Version is much better!
New expressions with complicated types are a bit tricky and not so easy to understand.
Regards,
Heinz
|
June 14, 2002 Re: Multiple void file pointer definition with new Q. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz Saathoff | "Heinz Saathoff" <hsaat@bre.ipnet.de> wrote in message news:MPG.1773b49b8e390d589896a2@news.digitalmars.com... > Laurentiu Pancescu schrieb... > > void (**p)() = new (void(*)())[100]; // okay with DMC and gcc, but BCC is > > buggy here... > > That should not be ok, because the new-expression end after the last ')' giving one pointer to pointer to function that is indexed with 100! Shouldn't work on any compiler. The typedef-ed Version is much better! Actually, it should: precedence of operator[] is bigger than the precedence of operator new, so the expression that new sees contains the square brackets, like "new type[100]". But that's indeed a good proof that just if you *can* do something, it doesn't mean it's also a good idea to do it... such code is almost unreadable! I also think the typedef solution is clean and easy to understand, and it should be always used in such cases. About BCC: it complains it cannot assign a void(*)() to a void(**)() without cast. No comment! > New expressions with complicated types are a bit tricky and not so easy to understand. That's right! After all, there's a good reason why typedef exists in the language, and probably ISO-C++ comittee will also add templatized typedefs - so it shouldn't be considered an obsolete feature. :) Regards, Laurentiu |
June 14, 2002 Re: Multiple void file pointer definition with new Q. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Laurentiu Pancescu | Laurentiu Pancescu schrieb... > "Heinz Saathoff" <hsaat@bre.ipnet.de> wrote in message news:MPG.1773b49b8e390d589896a2@news.digitalmars.com... > > Laurentiu Pancescu schrieb... > > > void (**p)() = new (void(*)())[100]; // okay with DMC and gcc, but BCC > is > > > buggy here... > > > > That should not be ok, because the new-expression end after the last ')' giving one pointer to pointer to function that is indexed with 100! Shouldn't work on any compiler. The typedef-ed Version is much better! > > Actually, it should: precedence of operator[] is bigger than the precedence of operator new, so the expression that new sees contains the square brackets, like "new type[100]". I once thought the same but was told that this is not the case (by some professionals in the german C++ newsgroup). Reading chapter 5.3.4 on the valid forms of new expression you find new-expression: new new-placement_opt new-type-id new-initializer_opt new new-placement_opt ( new-type-id ) new-initializer_opt Note the second form with explicit parenthesis. Without the optional new-initializer the new expression ends at the closing ')'. The usual higher prceeding of [] does not apply because it can only by applied to an object in a new expression. The opject is what new returns. > But that's indeed a good proof that just if > you *can* do something, it doesn't mean it's also a good idea to do it... > such code is almost unreadable! I also think the typedef solution is clean > and easy to understand, and it should be always used in such cases. Right! The problem is that you can make declarations much easyer with typedefs but need the original type for accessing the object, as in typedef void (*FUNC_PTR)(); FUNC_PTR *p = new FUNC_PTR[10]; // table of 10 functions // ... (*p[2])(); // call third func in table > About BCC: it complains it cannot assign a void(*)() to a void(**)() without > cast. No comment! BCC is right here! BTW, my PC-Lint also complains and gives this messages: --- Module: ab.cpp _ void (**p)() = new (void(*)())[100]; ab.cpp 3 Error 64: Type mismatch (initialization) (void (**)(void) = void (*)(void)) ab.cpp 3 Warning 416: creation of out-of-bounds pointer (100 beyond end of data) by operator '[' [Reference: file ab.cpp: line 3] _ void (**p)() = new (void(*)())[100]; ab.cpp 3 Info 815: Arithmetic modification of unsaved pointer ab.cpp 3 Warning 415: access of out-of-bounds pointer (100 beyond end of data) by operator '[' [Reference: file ab.cpp: line 3] ab.cpp 3 Info 831: Reference cited in prior message > > New expressions with complicated types are a bit tricky and not so easy to understand. > > That's right! After all, there's a good reason why typedef exists in the language, and probably ISO-C++ comittee will also add templatized typedefs - so it shouldn't be considered an obsolete feature. :) What do you mean with templatized typedefs? Regards, Heinz |
June 14, 2002 Re: Multiple void file pointer definition with new Q. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz Saathoff | He wasn't very clear in his example... No typedefs or anything.
In basic the 'new' when wrong because he used different syntax as I tried to
explain:
new ( type* ) [ size ]; instead of new ( type * [ size ] );
I think the latter will not work with DMC++
Jan
Heinz Saathoff wrote:
> Jan Knepper schrieb...
> > > keyx.akeyf = new (void (*[keyx.numf])());
> >
> > Try:
> > keyx.akeyf = new ( void * ) [ keyx.numf ];
>
> Hi Jan, that's not the same! E. Trelmars new expression should result in
> a array of pointer to function returning void.
> Your Expression would result in ONE void pointer-pointer that is
> immediatly indexed with keyx.numf resulting in an uninitialized void
> pointer.
>
> Regards,
> Heinz
|
Copyright © 1999-2021 by the D Language Foundation