Board index » cppbuilder » Define a Global Struct

Define a Global Struct


2006-12-04 09:07:40 AM
cppbuilder93
Hi all,
I need to define a 'Struct' that can be accessed in all the forms of a
project.
To do this I have tried:
in the main (start-up) form, called 'Main' I have:
"Main.h"
========
#define TotalNo 30
struct cashflow
{
double bc;
double ival;
double inf;
double cif;
double cbd;
double dcc;
};
extern cashflow cost[];
now in "Main.cpp"
=================
cashflow cost[TotalNo];
---------------------------------------------------------------
Now in the second "Second" form, I am trying to access it.
I have,
#include "Main_.h"
in the beginning, so that the variables in Main should be accessible.
So in Second.cpp I have
for (i=0; i<TotalNo; i++)
{
cost[i].cif = cost[i].bc*(cost[i].inf-1);
cost[i].dcc = (cost[i].bc*(er/100);
}
But including the above causes "Access Violations" to occur at varing
places during program run.
(But not specifically AT the above line)
Thus I assume the Struct is not being defined / accessed correctly.
So anyone know the problem?
--
Charles Hacker
 
 

Re:Define a Global Struct

Quote
for (i=0; i<TotalNo; i++)
{
cost[i].cif = cost[i].bc*(cost[i].inf-1);
cost[i].dcc = (cost[i].bc*(er/100);
}
Your last line won't compile (missing a closing parenthesis). From the
looks of it you probably accidentally typed the opening parenthesis when
writing your post. If possible, it is best to copy and paste your actual
code when writing to the newsgroups.
Quote
But including the above causes "Access Violations" to occur at varing
places during program run.
What are the AV's you are getting? Do you get those AV's after commenting
out any access to the array 'cost'?
The only potential problem I see is that 'cost' is unitialized. And the
only code you've shown uses 'cost' as if it were already initialized. Which
means that you either have other code that initializes the array or you need
to initialize the array at definition:
cashflow cost[TotalNo] = {0.0};
If this doesn't fix your problem then you may have to post more code here in
order for the real problem to be made visible.
- Clayton
 

Re:Define a Global Struct

Clayton Arends wrote:
Quote

Your last line won't compile (missing a closing parenthesis). From the
looks of it you probably accidentally typed the opening parenthesis when
writing your post. If possible, it is best to copy and paste your actual
code when writing to the newsgroups.
Yes I did strip down the code.
The code is from a student project that I am helping them to debug.
The project has about 10 forms in it, with many lines of code, so I was
just puitting in the relevant parts.
Quote
What are the AV's you are getting? Do you get those AV's after commenting
out any access to the array 'cost'?
Yes, when I comment out the STRUCT declarations, and all refrences to
the Struct 'cashflow', the AV stop.
Thus I was assuming the STRUCT definition must be somehow incorrect.
(Not knowing Borland C that well, I was unsure).
So if you are stating the STRUCT declarations are okay, then the AVs
must be due to something else.
I will keep looking.
--
Charles Hacker
 

{smallsort}

Re:Define a Global Struct

Charles Hacker < XXXX@XXXXX.COM >wrote:
Quote

[...] So if you are stating the STRUCT declarations are
okay, then the AVs must be due to something else.
That is not what Clayton said. What he said was that there
were no problems with the definition but he didn't see any
initialization for the structure.
Since commenting-out those lines removed the AV's then the
problem *must* be because the structure was not (correctly)
initialized.
He also asked what the exact error message was which you
didn't provide. If the address is at 00000000 then you are
using a NULL pointer to something somewhere and the compiler
should stop close to that line.
If the address is non-zero, then you have an invalid pointer
which may have been valid at one time.
~ JD
 

Re:Define a Global Struct

Charles Hacker < XXXX@XXXXX.COM >wrote:
Quote
I need to define a 'Struct' that can be accessed in all the
forms of a project.
I just happened to read this, so my answer is rather late: I
usually avoid globals, and pass the data from form to form where
needed instead. But yes, there are things that are so much of
global character that it makes sense to hold them just once
instead of passing them around. In VCL this can be easily done
with a TDataSet. Just place your stuff there and access the
dataset pointer as you do with other forms' pointers.
Quote
extern cashflow cost[];
Any reason for not using a std::vector here?
 

Re:Define a Global Struct

"Thorsten Kettner" < XXXX@XXXXX.COM >wrote:
Quote
In VCL this can be easily done with a TDataSet.
Oops that should be TDataModule. And don't forget to place it
first in the auto-create list in project options.