c++ strange allocation memory behavior

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • thomas

    #1

    c++ strange allocation memory behavior

    Hello suppose I have simple class like this :
    /*++++++++++++++ +++++++++++++++ +++*/

    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <string>

    using namespace std;
    class Array { // klasa array pamieta 10^8 elemntow char oraz
    //tablice "tablica" wskaznikow do slow zbudowana z nich


    char memory [10000000];
    char** tablica;
    string current;
    int n;
    int i; // bierzaca pozycja w tablicy memory
    int length;
    };
    /*++++++++++++++ +++++++++++++++ ++++++*/

    now somwhere in main I declare :

    main(){
    Array A;


    }

    when I run program it crushes.

    but when I do call like that :
    main(){
    Array A = new A();


    }

    everything works fine.
    I use gcc3.4 on windows xp. Is it something linked with the system memory
    allocation ? (in this two case at every time the piece of memory will be
    declared in diffrent memory segment : once at data and second time at stack,
    but have no clue why it behaves like that).


  • Victor Bazarov

    #2
    Re: c++ strange allocation memory behavior

    thomas wrote:
    Hello suppose I have simple class like this :
    /*++++++++++++++ +++++++++++++++ +++*/
    >
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <string>
    >
    using namespace std;
    class Array { // klasa array pamieta 10^8 elemntow char oraz
    //tablice "tablica" wskaznikow do slow zbudowana z
    nich
    >
    char memory [10000000];
    ...
    };
    /*++++++++++++++ +++++++++++++++ ++++++*/
    >
    now somwhere in main I declare :
    >
    main(){
    int main(){
    Array A;
    An automatic object of type 'Array'. Allocated in the automatic
    storage somewhere (usually the CPU _stack_).
    >
    >
    }
    >
    when I run program it crushes.
    >
    but when I do call like that :
    main(){
    int main(){
    Array A = new A();
    You meant, undoubtedly

    Array *A = new A();

    which would meat a dynamic object of type 'Array'. Allocated
    somewhere in the free store.
    >
    >
    }
    >
    everything works fine.
    I use gcc3.4 on windows xp. Is it something linked with the system
    memory allocation ? (in this two case at every time the piece of
    memory will be declared in diffrent memory segment : once at data and
    second time at stack, but have no clue why it behaves like that).
    Automatic storage objects are allocated from a different place than
    the free store objects. The limitations on the automatic storage
    in your system is apparently more severe.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Rolf Magnus

      #3
      Re: c++ strange allocation memory behavior

      Victor Bazarov wrote:
      >but when I do call like that :
      >main(){
      >
      int main(){
      >
      >Array A = new A();
      >
      You meant, undoubtedly
      >
      Array *A = new A();
      Well, I do doubt it ;-)
      I think he (and you) meant:

      Array *A = new Array();

      Comment

      • Bo Persson

        #4
        Re: c++ strange allocation memory behavior

        Victor Bazarov wrote:
        :: thomas wrote:
        :::
        :::
        ::: everything works fine.
        ::: I use gcc3.4 on windows xp. Is it something linked with the system
        ::: memory allocation ? (in this two case at every time the piece of
        ::: memory will be declared in diffrent memory segment : once at data
        ::: and second time at stack, but have no clue why it behaves like
        ::: that).
        ::
        :: Automatic storage objects are allocated from a different place than
        :: the free store objects. The limitations on the automatic storage
        :: in your system is apparently more severe.
        ::

        To thomas:

        The *default settings* for limitations on automatic storage is not the
        proper one for your code. Your options are to either use the free
        store, or to check out the linker settings for requesting a different
        amount of automatic storage (normally, a larger stack size).


        Bo Persson


        Comment

        • James Kanze

          #5
          Re: c++ strange allocation memory behavior

          On Dec 11, 1:00 pm, "Bo Persson" <b...@gmb.dkwro te:
          Victor Bazarov wrote:
          :: thomas wrote:
          >
          :::
          :::
          ::: everything works fine.
          ::: I use gcc3.4 on windows xp. Is it something linked with the system
          ::: memory allocation ? (in this two case at every time the piece of
          ::: memory will be declared in diffrent memory segment : once at data
          ::: and second time at stack, but have no clue why it behaves like
          ::: that).
          ::
          :: Automatic storage objects are allocated from a different place than
          :: the free store objects. The limitations on the automatic storage
          :: in your system is apparently more severe.
          ::
          >
          To thomas:
          >
          The *default settings* for limitations on automatic storage is not the
          proper one for your code. Your options are to either use the free
          store, or to check out the linker settings for requesting a different
          amount of automatic storage (normally, a larger stack size).
          >
          Bo Persson

          Comment

          Working...