Discussion:
Dynamic Allocation of Structure Array
(too old to reply)
Mike Copeland
2010-04-30 17:56:34 UTC
Permalink
I'm trying to declare and define a variable array - one which I
intend to populate, sort and access via a binary search. My problem is
that I cannot seem to convert scalar type dynamic allocation to that of
a structure I've created. Here's my structure:

struct dbeStruct
{
int bibNumber;
char source;
string body;
} dbeWork;

Here's my code attempt to define a dynamic array of this data:

dbeStruct *dbeZeroBibs = NULL;
dbeZeroBibs = new dbeStruct[2505];

I don't know what I've done wrong, and I don't understand the error
diagnostic (C2501) I'm getting from the (VS6.0) compiler. Please
advise. TIA
Victor Bazarov
2010-04-30 18:46:43 UTC
Permalink
Post by Mike Copeland
I'm trying to declare and define a variable array - one which I
intend to populate, sort and access via a binary search. My problem is
that I cannot seem to convert scalar type dynamic allocation to that of
struct dbeStruct
{
int bibNumber;
char source;
string body;
} dbeWork;
What's 'dbeWork'? You're defining an object here where you define the
struct?
Post by Mike Copeland
dbeStruct *dbeZeroBibs = NULL;
dbeZeroBibs = new dbeStruct[2505];
I don't know what I've done wrong, and I don't understand the error
diagnostic (C2501) I'm getting from the (VS6.0) compiler. Please
advise. TIA
Post the shortest example that gives you that error, then post the
*exact* compiler diagnostic. Consider that while some of us may still
have VC6 lying around, it's likely that we don't have the ability to use
*that* old of a compiler to try your code. BTW, why don't you upgrade?

Also, why don't you use 'std::vector<dbeStruct>' instead of the dynamic
array? It's so much easier to let the Standard Library functionality
handle dynamic memory allocation...

V
--
I do not respond to top-posted replies, please don't ask
Mike Copeland
2010-04-30 21:31:25 UTC
Permalink
Post by Victor Bazarov
Post by Mike Copeland
I'm trying to declare and define a variable array - one which I
intend to populate, sort and access via a binary search. My problem is
that I cannot seem to convert scalar type dynamic allocation to that of
struct dbeStruct
{
int bibNumber;
char source;
string body;
} dbeWork;
What's 'dbeWork'? You're defining an object here where you define the
struct?
Yes, I'm defining an object variable of the struct type. Is there
something wrong with that?
Post by Victor Bazarov
Post by Mike Copeland
dbeStruct *dbeZeroBibs = NULL;
dbeZeroBibs = new dbeStruct[2505];
I don't know what I've done wrong, and I don't understand the error
diagnostic (C2501) I'm getting from the (VS6.0) compiler. Please
advise. TIA
Post the shortest example that gives you that error, then post the
*exact* compiler diagnostic.
I will do that, in a followup post.
Post by Victor Bazarov
Consider that while some of us may still
have VC6 lying around, it's likely that we don't have the ability to use
*that* old of a compiler to try your code. BTW, why don't you upgrade?
Because I can't afford to purchase a new version...and 6.0 is all I
have. Also, I'm quite old and I tend to be leary of the effort to
upgrade to something (conversions, porting of existing
code/applications) that is unknown. Yes, that's not a good argument,
but I have ~1,000,000 lines of code and upgrading to (what?) is very
daunting.
Post by Victor Bazarov
Also, why don't you use 'std::vector<dbeStruct>' instead of the dynamic
array? It's so much easier to let the Standard Library functionality
handle dynamic memory allocation...
As I understand it, I'd need a scalar index for a vector. The data I
will be storing in my array doesn't have unique values in either the
"bibNumber" or "source" fields, and my application will be working only
with the "body" values: sorting and searching. Perhaps there's another
way, but the volume here is rather high and seems to call for a binary
search. <sigh...>
Brian Muth
2010-04-30 22:21:00 UTC
Permalink
Post by Mike Copeland
As I understand it, I'd need a scalar index for a vector. The data I
will be storing in my array doesn't have unique values in either the
"bibNumber" or "source" fields, and my application will be working only
with the "body" values: sorting and searching. Perhaps there's another
way, but the volume here is rather high and seems to call for a binary
search. <sigh...>
It sounds like you need something like:

struct dbeStruct
{
int bibNumber;
char source;
};

std::map<string, dbeStruct> dbeMap;

This gives you a mapping between a string (which you can use as a lookup
value) and the bibNumber and source character. Now you don't need to bother
with binary search algorithm.

Mind you, the downside is that you will need to familiarize yourself with
STL if this is new to you.
Mike Copeland
2010-04-30 21:48:05 UTC
Permalink
Post by Victor Bazarov
Post by Mike Copeland
struct dbeStruct
{
int bibNumber;
char source;
string body;
} dbeWork;
What's 'dbeWork'? You're defining an object here where you define the
struct?
Post by Mike Copeland
dbeStruct *dbeZeroBibs = NULL;
dbeZeroBibs = new dbeStruct[2505];
I don't know what I've done wrong, and I don't understand the error
diagnostic (C2501) I'm getting from the (VS6.0) compiler. Please
advise. TIA
Post the shortest example that gives you that error, then post the
*exact* compiler diagnostic. Consider that while some of us may still
have VC6 lying around, it's likely that we don't have the ability to use
*that* old of a compiler to try your code. BTW, why don't you upgrade?
Here's a complete program that demonstrates the problem, as well as
all error diagnostics. TIA
#include <iostream>
#include <string>
using namespace std;
struct dbeBuild
{
int bibNumber;
char source;
string body;
} dbeWork;

dbeBuild *dbeZeroBibs = NULL;
dbeZeroBibs = new dbeBuild[2505]; // the errors are on this line
// error C2501: 'dbeZeroBibs' : missing storage-class or type specifiers
// error C2040: 'dbeZeroBibs' : 'int' differs in levels of indirection
from 'struct dbeBuild *'
// error C2440: 'initializing' : cannot convert from 'struct dbeBuild
*' to 'int'
// This conversion requires a reinterpret_cast, a C-style cast or
function-style cast
// Error executing cl.exe.
// dynarray.exe - 3 error(s), 0 warning(s)

int main(int argc, char *argv[]) ////////// M A I N L I N E
////////
{

return 0;
}
Thomas J. Gritzan
2010-04-30 21:54:03 UTC
Permalink
Post by Mike Copeland
Here's a complete program that demonstrates the problem, as well as
all error diagnostics. TIA
#include <iostream>
#include <string>
using namespace std;
struct dbeBuild
{
int bibNumber;
char source;
string body;
} dbeWork;
dbeBuild *dbeZeroBibs = NULL;
dbeZeroBibs = new dbeBuild[2505]; // the errors are on this line
This is outside of any function. The above is a statement that has to be
inside of a function body.
Post by Mike Copeland
// error C2501: 'dbeZeroBibs' : missing storage-class or type specifiers
// error C2040: 'dbeZeroBibs' : 'int' differs in levels of indirection
from 'struct dbeBuild *'
[...]
--
Thomas
Mike Copeland
2010-04-30 22:21:01 UTC
Permalink
Post by Thomas J. Gritzan
Post by Mike Copeland
Here's a complete program that demonstrates the problem, as well as
all error diagnostics. TIA
#include <iostream>
#include <string>
using namespace std;
struct dbeBuild
{
int bibNumber;
char source;
string body;
} dbeWork;
dbeBuild *dbeZeroBibs = NULL;
dbeZeroBibs = new dbeBuild[2505]; // the errors are on this line
This is outside of any function. The above is a statement that has to be
inside of a function body.
Post by Mike Copeland
// error C2501: 'dbeZeroBibs' : missing storage-class or type specifiers
// error C2040: 'dbeZeroBibs' : 'int' differs in levels of indirection
from 'struct dbeBuild *'
[...]
Of course! How stupid of me. Sorry for the bother (but thanks!)...
However, it _does_ go to prove my point that Microsoft's diagnostics
are often incomprehensible. <sigh...> 8<{{
Victor Bazarov
2010-05-01 13:26:21 UTC
Permalink
Post by Mike Copeland
Post by Thomas J. Gritzan
Post by Mike Copeland
Here's a complete program that demonstrates the problem, as well as
all error diagnostics. TIA
#include <iostream>
#include <string>
using namespace std;
struct dbeBuild
{
int bibNumber;
char source;
string body;
} dbeWork;
dbeBuild *dbeZeroBibs = NULL;
dbeZeroBibs = new dbeBuild[2505]; // the errors are on this line
This is outside of any function. The above is a statement that has to be
inside of a function body.
Post by Mike Copeland
// error C2501: 'dbeZeroBibs' : missing storage-class or type specifiers
// error C2040: 'dbeZeroBibs' : 'int' differs in levels of indirection
from 'struct dbeBuild *'
[...]
Of course! How stupid of me. Sorry for the bother (but thanks!)...
However, it _does_ go to prove my point that Microsoft's diagnostics
are often incomprehensible. <sigh...> 8<{{
The diagnostic messages cannot possibly include all education materials
for the language you're using. Statements outside of functions are
*expected* to be declarations. A declaration without an explicit type
is assumed to be of 'int' type (thank C inventors for that and the
spirit of "backward compatibility"). Initialization of an 'int' with a
pointer produces your "different levels of indirection" message.

BTW, VC 2010 gives this diagnostic:

test.cpp(12): error C4430: missing type specifier - int assumed.
Note: C++ does not support default-int
test.cpp(12): error C2040: 'dbeZeroBibs' : 'int' differs in levels of
indirection from 'dbeBuild *'
test.cpp(12): error C2440: 'initializing' : cannot convert from
'dbeBuild *' to 'int'

Of course, it might be more helpful if it immediately flagged the
erroneous line as a *redeclaration/redefinition* of the variable, but I
guess MSVC developers have better things to do than to look for all
possible causes of the compiler's inability to compile a declaration.

There is logical explanation for everything, and there are going to be
people who aren't satisfied with anything you give them. It's a well
known phenomenon that one can't satisfy *everybody*. So, the principle
often adhered to is 'sapienti sat'.

Get yourself an Express version of Visual Studio 2010, it's *free*. At
least you're going to have a modern compiler. And a decent book on C++,
while you're at it. Being "quite old" is no excuse for staying behind
as far as technology is concerned. And, following a common idiom, you
have to realize that you _can't afford *not* to upgrade_ to a new
version. As for its being "daunting", I'll give you the very old
Russian saying: "Eyes are afraid but hands are doing".

Good luck!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thomas J. Gritzan
2010-05-01 15:53:27 UTC
Permalink
[statement outside of function]
Post by Victor Bazarov
Post by Mike Copeland
Of course! How stupid of me. Sorry for the bother (but
thanks!)... However, it _does_ go to prove my point that
Microsoft's diagnostics are often incomprehensible. <sigh...> 8<{{
The diagnostic messages cannot possibly include all education materials
for the language you're using. Statements outside of functions are
*expected* to be declarations. A declaration without an explicit type
is assumed to be of 'int' type (thank C inventors for that and the
spirit of "backward compatibility"). Initialization of an 'int' with a
pointer produces your "different levels of indirection" message.
C++ does not support default-int
test.cpp(12): error C2040: 'dbeZeroBibs' : 'int' differs in levels of
indirection from 'dbeBuild *'
test.cpp(12): error C2440: 'initializing' : cannot convert from
'dbeBuild *' to 'int'
Of course, it might be more helpful if it immediately flagged the
erroneous line as a *redeclaration/redefinition* of the variable, but I
guess MSVC developers have better things to do than to look for all
possible causes of the compiler's inability to compile a declaration.
Using Comeau Online[1], the error messages are much better. It flags the
redeclaration and even gives a hint to the cause of the error(s). So the
messages MSVC produces can/could be improved:

"ComeauTest.c", line 12: error: this declaration has no storage class or
type specifier, Wild guess: Should this be in a function block? Wild
Guess: You're using export but not using Comeau C++ 4.3.x
dbeZeroBibs = new dbeBuild[2505];
^
"ComeauTest.c", line 12: error: variable "dbeZeroBibs" has already been
defined
dbeZeroBibs = new dbeBuild[2505];
^
"ComeauTest.c", line 12: error: a value of type "dbeBuild *" cannot be
used to initialize an entity of type "int"
dbeZeroBibs = new dbeBuild[2505];

[1] http://www.comeaucomputing.com/tryitout
--
Thomas
Stephan T. Lavavej [MSFT]
2010-05-02 03:43:39 UTC
Permalink
VC10 Intellisense is powered by the EDG compiler front-end, which is the
same thing that Comeau is powered by, so hovering over VC10's red squiggles
can be especially enlightening.

In this case, "dbeZeroBibs" in "dbeZeroBibs = new dbeBuild[2505];" is
squiggled, and hovering over it reveals the message: "Error: this
declaration has no storage class or type specifier", which says that
Intellisense thinks that it's a declaration.

"Wild guess: Should this be in a function block?", which is especially
helpful, is either Comeau-specific, or not being shown by Intellisense (as a
library dev, I don't know the internals here).

Also: you should be using std::vector and std::lower_bound(), or std::map,
etc.

Stephan T. Lavavej
Visual C++ Libraries Developer
Post by Thomas J. Gritzan
[statement outside of function]
Post by Victor Bazarov
Post by Mike Copeland
Of course! How stupid of me. Sorry for the bother (but
thanks!)... However, it _does_ go to prove my point that
Microsoft's diagnostics are often incomprehensible. <sigh...> 8<{{
The diagnostic messages cannot possibly include all education materials
for the language you're using. Statements outside of functions are
*expected* to be declarations. A declaration without an explicit type
is assumed to be of 'int' type (thank C inventors for that and the
spirit of "backward compatibility"). Initialization of an 'int' with a
pointer produces your "different levels of indirection" message.
C++ does not support default-int
test.cpp(12): error C2040: 'dbeZeroBibs' : 'int' differs in levels of
indirection from 'dbeBuild *'
test.cpp(12): error C2440: 'initializing' : cannot convert from
'dbeBuild *' to 'int'
Of course, it might be more helpful if it immediately flagged the
erroneous line as a *redeclaration/redefinition* of the variable, but I
guess MSVC developers have better things to do than to look for all
possible causes of the compiler's inability to compile a declaration.
Using Comeau Online[1], the error messages are much better. It flags the
redeclaration and even gives a hint to the cause of the error(s). So the
"ComeauTest.c", line 12: error: this declaration has no storage class or
type specifier, Wild guess: Should this be in a function block? Wild
Guess: You're using export but not using Comeau C++ 4.3.x
dbeZeroBibs = new dbeBuild[2505];
^
"ComeauTest.c", line 12: error: variable "dbeZeroBibs" has already been
defined
dbeZeroBibs = new dbeBuild[2505];
^
"ComeauTest.c", line 12: error: a value of type "dbeBuild *" cannot be
used to initialize an entity of type "int"
dbeZeroBibs = new dbeBuild[2505];
[1] http://www.comeaucomputing.com/tryitout
--
Thomas
Loading...