c++ strange allocation memory behavior

Discussion in 'C++' started by thomas, Dec 11, 2007.

  1. thomas

    thomas Guest

    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).
     
    thomas, Dec 11, 2007
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Dec 11, 2007
    #2
    1. Advertising

  3. thomas

    Rolf Magnus Guest

    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();
     
    Rolf Magnus, Dec 11, 2007
    #3
  4. thomas

    Bo Persson Guest

    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
     
    Bo Persson, Dec 11, 2007
    #4
  5. thomas

    James Kanze Guest

    On Dec 11, 1:00 pm, "Bo Persson" <> wrote:
    > 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
     
    James Kanze, Dec 12, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    721
    Eric Sosman
    Mar 22, 2005
  2. Rodrigo Dominguez

    memory allocation and freeing memory

    Rodrigo Dominguez, Jun 13, 2005, in forum: C Programming
    Replies:
    11
    Views:
    616
    Jean-Claude Arbaut
    Jun 15, 2005
  3. Ken
    Replies:
    24
    Views:
    3,903
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    1,007
    chris
    Oct 28, 2005
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,208
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page