Re: Initialize dynamic allocated data before main get error?
From: Uwe Schnitker (schnitkerAffenschaukel_at_sigma-c.com)
Date: 04/30/04
- Previous message: Adam: "Re: temporary file in C++"
- In reply to: Chris \( Val \): "Re: Initialize dynamic allocated data before main get error?"
- Next in thread: Francis Glassborow: "Re: Initialize dynamic allocated data before main get error?"
- Reply: Francis Glassborow: "Re: Initialize dynamic allocated data before main get error?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 30 Apr 2004 00:20:07 -0700
"Chris \( Val \)" <chrisval@bigpond.com.au> wrote in message news:<c6qj2l$e77c4$1@ID-110726.news.uni-berlin.de>...
> "Tony Johansson" <johansson.andersson@telia.com> wrote in message
> news:2t3kc.91138$dP1.279813@newsc.telia.net...
> | Hello Experts!!
> |
> | Here an easy question. If I initialize the dynamic allocated q global which
> | means before main I get a lot of compilation error.
> | I just want to know why I can't do that.
> |
> | I can assign an integer to tal and I can assign the string "this is a test"
> | to the name field before the main without any problem.
> | If I instead move the initialize of q to the main part it works.
> |
> | int tal = 5;
> | char namn[] = "this is a test";
> |
> | int *q = new int[5];
> | q[0] = 0;
> | q[1] = 1;
> | q[2] = 2;
> | q[3] = 3;
> | q[4] = 4;
> |
> | int main()
> | {
> | return 0;
> | }
>
> You are not allowed to assign to globals in the same
> global scope, but you are allowed to *initialise* a
> global at the point you declare it.
To the OP:
It is important to understand that the = has a different meaning in
both cases:
int i = 5; // initialization
i = 7; // assignment
Note that you can use the first form at global scope, to define a global
variable -I'm not going to discuss the "evilness" of those here - but the
second code piece cannot be used outside a function.
> In the case of arrays, they cannot be initialised full
> stop, so the only way to do it is via assignment, in
> another scope.
Ahem, what's wrong with:
int a[] = {3,4,5,6,7};
> Cheers.
> Chris Val
BTW, these kind of initialization is called static initialization.
There's also a possibillity to do dynamic initialization:
Either use a function to initialize a global variable:
int* foo() {
int* q = new int[5];
q[0] = 0;
q[1] = 1;
q[2] = 2;
q[3] = 3;
q[4] = 4;
return q;
}
int* p = foo();
or define a global variable of a suitable class type:
class bar {
public:
bar() : bq(new int[5]) {
bq[0] = 0;
bq[1] = 1;
bq[2] = 2;
bq[3] = 3;
bq[4] = 4;
}
privat:
int* bq;
};
bar b;
Note that with dynamic initialization, you will run into the
"Static Initialization Disorder Fiasco" sooner or later if you
use it often in a bigger project - apart from the other really bad
problems you will get from using globals a lot.
HTH,
Uwe
- Previous message: Adam: "Re: temporary file in C++"
- In reply to: Chris \( Val \): "Re: Initialize dynamic allocated data before main get error?"
- Next in thread: Francis Glassborow: "Re: Initialize dynamic allocated data before main get error?"
- Reply: Francis Glassborow: "Re: Initialize dynamic allocated data before main get error?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|