PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > c++ strange allocation memory behavior
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
c++ strange allocation memory behavior

Réponse
 
LinkBack Outils de la discussion
Vieux 11/12/2007, 02h31   #1
thomas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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).


  Réponse avec citation
Vieux 11/12/2007, 03h34   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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


  Réponse avec citation
Vieux 11/12/2007, 09h50   #3
Rolf Magnus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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();

  Réponse avec citation
Vieux 11/12/2007, 12h00   #4
Bo Persson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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


  Réponse avec citation
Vieux 12/12/2007, 09h41   #5
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: c++ strange allocation memory behavior

On Dec 11, 1:00 pm, "Bo Persson" <b...@gmb.dk> 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


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h15.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15517 seconds with 13 queries