Re: Initialize dynamic allocated data before main get error?

From: Uwe Schnitker (schnitkerAffenschaukel_at_sigma-c.com)
Date: 04/30/04

  • Next message: cpp: "Header file + implementation file with template"
    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


  • Next message: cpp: "Header file + implementation file with template"

    Relevant Pages

    • Re: VS2008 destroys static objects before global (non-static) objects?
      ... destroyed before globals. ... While the class definitions are in one DLL o is defined in another DLL ... DLLs are loaded p_ in 2.dll is initialized first followed by an initialization of o and again p_ in 1.dll. ... As the destructor of p_ is then called first which frees all memory managed by the memory manager I get an access violation when o is destroyed. ...
      (microsoft.public.vc.language)
    • Re: Question about register_globals in php.ini
      ... >> server to avoid some attack? ... all global variables does not guarantee that would happen, ... do not fully control your execution path, then your initialization code ... If the initialization of globals happen at the designated entry point, ...
      (comp.lang.php)
    • Re: Order loading shared libraries.
      ... The order of initialization of global C++ objects in separate ... compilation units is *undefined*. ... have any globals at all. ... In order to understand recursion you must first understand recursion. ...
      (comp.sys.hp.hpux)
    • Re: Mixed code - Crashes in Release Mode
      ... Giovanni Dicanio wrote: ... initialization code. ... initialization, and instead pack them somewhere in a struct, and ... if these globals are constructed inside library code and not his ...
      (microsoft.public.vc.language)
    • Re: Global variables being unset somehow!
      ... I don't think the initialization code is a problem, ... triggering the unset of the globals. ... I have an obscure bug that seems to be being caused by global variables ...
      (microsoft.public.access.gettingstarted)

    Loading