c++ strange allocation memory behavior

T

thomas

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).
 
V

Victor Bazarov

thomas said:
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(){

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
 
B

Bo Persson

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
 
J

James Kanze

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top