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 > Runtime Stack allocation
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Runtime Stack allocation

Réponse
 
LinkBack Outils de la discussion
Vieux 23/02/2008, 18h53   #1
Adam
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Runtime Stack allocation

Is there a way in C++ to allocate a variable amount of stack space at
run-time?

void complex_maths_operation(const int num_rows, double* vector)
{
double temp[num_rows];
....
}

The obvious answer is to use the heap... but if this isn't an option.



  Réponse avec citation
Vieux 23/02/2008, 19h00   #2
Rolf Magnus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Runtime Stack allocation

Adam wrote:

> Is there a way in C++ to allocate a variable amount of stack space at
> run-time?


Well, there is the class std::stack, but I doubt that you are referring to
that. However, other than that, C++ doesn't define anything called "stack".

  Réponse avec citation
Vieux 23/02/2008, 20h16   #3
Jeff Schwab
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Runtime Stack allocation

Adam wrote:
> Is there a way in C++ to allocate a variable amount of stack space at
> run-time?
>
> void complex_maths_operation(const int num_rows, double* vector)
> {
> double temp[num_rows];
> ....
> }
>
> The obvious answer is to use the heap... but if this isn't an option.


Recurse. Your base case can depend on a run-time variable.
  Réponse avec citation
Vieux 24/02/2008, 07h14   #4
Andrey Tarasevich
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Runtime Stack allocation

Adam wrote:
> Is there a way in C++ to allocate a variable amount of stack space at
> run-time?
>
> void complex_maths_operation(const int num_rows, double* vector)
> {
> double temp[num_rows];
> ....
> }
>
> The obvious answer is to use the heap... but if this isn't an option.
> ...


There's no C++-standard way to do that.

If you don't mind going outside the limits of C++ standard, look up the 'alloca'
function. It does exactly what you want but has some limitations, which might be
(or might not be) critical in your case.

--
Best regards,
Andrey Tarasevich
  Réponse avec citation
Vieux 24/02/2008, 15h49   #5
Adam
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Runtime Stack allocation

Thanks Andrey,

Yes, 'alloca' style allocation is what I was looking for.
This sort of behavor is supported by Ada and I've become fond of it.

Adam

"Andrey Tarasevich" <andreytarasevich@hotmail.com> wrote in message
news:OPqdnWZLEKaxhFzanZ2dnUVZ_ubinZ2d@comcast.com. ..
> Adam wrote:
>> Is there a way in C++ to allocate a variable amount of stack space at
>> run-time?
>>
>> void complex_maths_operation(const int num_rows, double* vector)
>> {
>> double temp[num_rows];
>> ....
>> }
>>
>> The obvious answer is to use the heap... but if this isn't an option.
>> ...

>
> There's no C++-standard way to do that.
>
> If you don't mind going outside the limits of C++ standard, look up the
> 'alloca' function. It does exactly what you want but has some limitations,
> which might be (or might not be) critical in your case.
>
> --
> Best regards,
> Andrey Tarasevich


  Réponse avec citation
Vieux 25/02/2008, 11h46   #6
Matthias Buelow
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Runtime Stack allocation

Adam wrote:

> Yes, 'alloca' style allocation is what I was looking for.
> This sort of behavor is supported by Ada and I've become fond of it.


Don't allocate too much with alloca(), on most systems, there's a lot
less stack space than heap space available with default settings.

  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 18h20.


É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,10748 seconds with 14 queries