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 > Strange seg fault
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Strange seg fault

Réponse
 
LinkBack Outils de la discussion
Vieux 06/02/2008, 18h17   #1
ciccio
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Strange seg fault

Hi,

Recently some people I know stumbled upon a strange problem with the
following piece of code. This is reduced as far as possible.

The idea of the code is simple. There are four structs of which one
contains the other three structs. If we allocate a large double array
of that struct and try to initialize it to zero, or try to use one of
them in a function. The program gives a seg fault. If we make the array
slightly smaller, there is no problem. The problem only appears on
x86_64 architectures.

Below I past a copy of the failing code. If DATAMAX is set to 4820 the
program works, if it is set to 4830 it seg faults.

What could the reason be of this? We have calculated the amount of
memory and we have for sure no problem with that. A backtrace also does
not give any deeper insight.

Thanks for the



#define DATAMAX 4830
#define ISOMAX 7

typedef struct
{
short iso;
char observable[15];
short ds_dt;
short ds_du;
double emin;
double emax;
double cos;
double ampli;
double error;
short tch;
} Photo;

typedef struct
{
short iso;
char observable[15];
short ds_dt;
double qsquared;
double s;
short cos_ang;
double t;
double cos;
double ampli;
double error;
short tch;
short beam_ener_input;
double e_beam_ener;
double eps;
short cs_convention;
} Electro;


typedef struct
{
short iso;
char observable[10];
double pk;
double cos;
double ampli;
double ratio;
double error;
} Kaoncap;

typedef struct
{
short iso;
short photo_prod;
short electro_prod;
short kaoncapture;
Photo photo;
Electro elec;
Kaoncap kaoncap;
} Data;

int test(Data blub) {
return 0;
}

int main(int argc, char* argv)
{
// WORKS ALWAYS
Data datapoints[ISOMAX][DATAMAX];

// FAILS WITH DATAMAX = 4830 or larger
// Data datapoints[ISOMAX][DATAMAX] = {{{0}}};

// FAILS WITH DATAMAX = 4830 or lager
// Data datapoints[ISOMAX][DATAMAX];
// test(datapoints[0][0]);

return 0;
}
  Réponse avec citation
Vieux 06/02/2008, 18h50   #2
Alf P. Steinbach
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Strange seg fault

* ciccio:
> Hi,
>
> Recently some people I know stumbled upon a strange problem with the
> following piece of code. This is reduced as far as possible.
>
> The idea of the code is simple. There are four structs of which one
> contains the other three structs. If we allocate a large double array
> of that struct and try to initialize it to zero, or try to use one of
> them in a function. The program gives a seg fault. If we make the array
> slightly smaller, there is no problem. The problem only appears on
> x86_64 architectures.
>
> Below I past a copy of the failing code. If DATAMAX is set to 4820 the
> program works, if it is set to 4830 it seg faults.
>
> What could the reason be of this? We have calculated the amount of
> memory and we have for sure no problem with that.


Are you sure?

Check

sizeof( Data[ISOMAX][DATAMAX] );

No need to calculate when you can check directly.

You're talking automatic local memory here, a.k.a. "stack". It may be
implemented in ways that give you very little elbow room. E.g. onboard
cache, registers.

Try dynamic allocation, which is easily achieved by using std::vector or
e.g. some library matrix class.



> A backtrace also does not give any deeper insight.
>
> #define DATAMAX 4830
> #define ISOMAX 7


In C++ it's a good idea to simply use typed constants, in order to avoid
macros,

size_t const maxData = 4830;



> typedef struct
> {
> short iso;
> char observable[15];
> short ds_dt;
> short ds_du;
> double emin;
> double emax;
> double cos;
> double ampli;
> double error;
> short tch;
> } Photo;


In C++ this is better expressed as

struct Photo
{
// ...
};


[snip]

> int main(int argc, char* argv)
> {
> // WORKS ALWAYS
> Data datapoints[ISOMAX][DATAMAX];


Maybe it's optimized away.


> // FAILS WITH DATAMAX = 4830 or larger
> // Data datapoints[ISOMAX][DATAMAX] = {{{0}}};
>
> // FAILS WITH DATAMAX = 4830 or lager
> // Data datapoints[ISOMAX][DATAMAX];
> // test(datapoints[0][0]);
>
> return 0;
> }



Cheers, & hth.,

- Alf


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  Réponse avec citation
Vieux 06/02/2008, 18h57   #3
Puppet_Sock
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Strange seg fault

On Feb 6, 1:17pm, ciccio <no_valid_em...@spam.com> wrote:
[snip]
> int main(int argc, char* argv)
> {
> // WORKS ALWAYS
> Data datapoints[ISOMAX][DATAMAX];
>
> // FAILS WITH DATAMAX = 4830 or larger
> // Data datapoints[ISOMAX][DATAMAX] = {{{0}}};
>
> // FAILS WITH DATAMAX = 4830 or lager
> // Data datapoints[ISOMAX][DATAMAX];
> // test(datapoints[0][0]);


<JohnCleeseVoice>How much stack?</voice>

How big is your stack? What does your app do, or what
options does your development platform set, when that
happens?

If I did the math right, with DATAMAX of 4830 you've
got something in the range of 8MB. Default stack size
is usually not that large on most compilers.

Try allocating it through new rather than as an auto var.
Socks
  Réponse avec citation
Vieux 07/02/2008, 10h31   #4
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Strange seg fault

On Feb 6, 7:57 pm, Puppet_Sock <puppet_s...@hotmail.com> wrote:
> On Feb 6, 1:17 pm, ciccio <no_valid_em...@spam.com> wrote:
> [snip]


> > int main(int argc, char* argv)
> > {
> > // WORKS ALWAYS
> > Data datapoints[ISOMAX][DATAMAX];


> > // FAILS WITH DATAMAX = 4830 or larger
> > // Data datapoints[ISOMAX][DATAMAX] = {{{0}}};


> > // FAILS WITH DATAMAX = 4830 or lager
> > // Data datapoints[ISOMAX][DATAMAX];
> > // test(datapoints[0][0]);


> <JohnCleeseVoice>How much stack?</voice>


> How big is your stack? What does your app do, or what
> options does your development platform set, when that
> happens?


> If I did the math right, with DATAMAX of 4830 you've
> got something in the range of 8MB. Default stack size
> is usually not that large on most compilers.


The maximum stack size can often be set by a command at runtime.
On my system, it's 8MB by default, but I can always do "ulimit
-s unlimited", and use all of the available memory.

Not that that's necessarily a good idea. The reason why the
stack size is limited is so that an endless recursion will crash
in a reasonable time, rather than running for days eating up
machine resources. In this case, he should simple use an
std::vector, and be done with it.

> Try allocating it through new rather than as an auto var.


Yes, but not manually, please.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

  Réponse avec citation
Vieux 08/02/2008, 09h49   #5
ciccio
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Strange seg fault

Hi all,

Thanks for the info, this was indeed the case. I forgot about the stack.
  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 13h26.


É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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18604 seconds with 13 queries