Sujet : Re: Segments
De : antispam (at) *nospam* fricas.org (Waldek Hebisch)
Groupes : comp.archDate : 17. Jan 2025, 03:22:54
Autres entêtes
Organisation : To protect and to server
Message-ID : <vmcets$5vp2$1@paganini.bofh.team>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14
User-Agent : tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-9-amd64 (x86_64))
David Brown <
david.brown@hesbynett.no> wrote:
On 16/01/2025 17:46, Waldek Hebisch wrote:
David Brown <david.brown@hesbynett.no> wrote:
On 16/01/2025 13:35, Michael S wrote:
On Thu, 16 Jan 2025 12:36:45 +0100
David Brown <david.brown@hesbynett.no> wrote:
>
On 15/01/2025 21:59, Thomas Koenig wrote:
Michael S <already5chosen@yahoo.com> schrieb:
On Wed, 15 Jan 2025 18:00:34 -0000 (UTC)
Thomas Koenig <tkoenig@netcologne.de> wrote:
As you can guess, in kernel drivers VLA are unwelcome.
>
I can imagine that they are - but I really don't understand why. I've
never understood why people think there is something "dangerous" about
VLAs, or why they think using heap allocations is somehow "safer".
VLA normally allocate on the stack. Which at first glance look
great. But once one realize how small are stacks in modern
systems (compared to whole memory), this no longer looks good.
Basically, to use VLA one needs rather small bound on maximal
size of array.
Sure.
Given such bound always allocating maximal
size is simpler. Without _small_ bound on size heap is
safer, as it is desined to handle also big allocations.
You don't allocate anything in a VLA without knowing the bounds and
being sure it is appropriate to put on the stack. You don't allocate
anything on the heap without knowing the bounds and being sure it is
appropriate. There's no fundamental difference - it's just the cut-off
point that is different.
Well, AFAICS VLA-s may get allocated on function entry. In such
case caller have to check for allocation size, which spreads
allocation related code between caller and called function.
In case of 'malloc' one can simply check return value. In fact,
in many programs simple wrapper that exits in case of allocation
failure is enough (if application can not do its work without
memory and there is no memory, then there is no point in continuing
execution).
The stack on Linux is 10 MB by default, and 1 MB by default on Windows.
That's a /lot/ if you are working with fairly small but non-constant
sizes. So if you are working with a selection of short-lived
medium-sized bits of data - say, parts of strings for some formatting
work - putting them on the stack is safe and can be significantly faster
than using the heap.
IME this is relatively rare case. For formatting frequently a single
result buffer (possibly expanded when needed) with other pieces of
data added there gave me good performance. Intermediate strings
appeared as return values of called functions. Without reoganizing
code this does not respect stack discipline. Once reorganized
I get best results without materializing intermediate strings.
Using VLAs (or the older but related technique, alloca) means you don't
waste space. Maybe you are working with file paths, and want to support
up to 4096 characters per path - but in reality most paths are less than
100 characters. With fixed size arrays, allocating 16 of these and
initialising them will use up your entire level 1 cache - with VLAs, it
will use only a tiny fraction.
One case initialize only used part. Or simply used uninitialized
arrays (that is what I do normally). It rather hard to give
meaningful initialization in case where size of payload varies.
These things can make a big difference
to code that aims to be fast.
Fixed size arrays are certainly easier to analyse and are often a good
choice, but VLA's definitely have their advantages in some situations,
and they are perfectly safe and reliable if you use them appropriately
and correctly.
In the past I was a fan of VLA and stack allocation in general.
But I saw enough bug reports due to programs exceeding their
stack limits that I changed my view.
Other people might have bad uses of VLAs - it doesn't mean /you/ have to
use them badly too!
Well, for me typical cases is for work arrays where needed size
may vary widely. Using 'malloc' is simpler is such use given
small stack limit. With small stack limit VLA would be a
micro-optimization, not worth extra effort.
<snip>
Far and away my most common use of VLAs is, however, not variable length
at all. It's more like :
const int no_of_whatsits = 20;
const int size_of_whatsit = 4;
uint8_t whatsits_data[no_of_whatsits * size_of_whatsit];
Technically in C, that is a VLA because the size expression is not a
constant expression according to the rules of the language. But of
course it is a size that is known at compile-time, and the compiler
generates exactly the same code as if it was a constant expression.
OK, that is useful case (but in spirt this is not VLA).
-- Waldek Hebisch
Date | Sujet | # | | Auteur |
1 Oct 24 | Re: Whether something is RISC or not (Re: PDP-8 theology, not Concertina II Progress) | 387 | | MitchAlsup1 |
1 Oct 24 |  Re: Whether something is RISC or not (Re: PDP-8 theology, not Concertina II Progress) | 386 | | Thomas Koenig |
1 Oct 24 |   Re: Whether something is RISC or not (Re: PDP-8 theology, not Concertina II Progress) | 379 | | MitchAlsup1 |
2 Oct 24 |    Re: Whether something is RISC or not (Re: PDP-8 theology, not Concertina II Progress) | 377 | | Brett |
3 Oct 24 |     Re: Whether something is RISC or not (Re: PDP-8 theology, not Concertina II Progress) | 376 | | Lawrence D'Oliveiro |
3 Oct 24 |      Re: Whether something is RISC or not (Re: PDP-8 theology, not Concertina II Progress) | 1 | | Brett |
3 Oct 24 |      Re: Whether something is RISC or not (Re: PDP-8 theology, not Concertina II Progress) | 1 | | Anton Ertl |
3 Oct 24 |      Re: Whether something is RISC or not (Re: PDP-8 theology, not Concertina II Progress) | 373 | | David Brown |
3 Oct 24 |       Byte ordering (was: Whether something is RISC or not) | 372 | | Anton Ertl |
3 Oct 24 |        Re: Byte ordering (was: Whether something is RISC or not) | 1 | | David Brown |
3 Oct 24 |        Re: Byte ordering (was: Whether something is RISC or not) | 369 | | Lawrence D'Oliveiro |
4 Oct 24 |         Re: Byte ordering | 1 | | Lynn Wheeler |
4 Oct 24 |         Re: Byte ordering (was: Whether something is RISC or not) | 365 | | David Brown |
4 Oct 24 |          Re: Byte ordering (was: Whether something is RISC or not) | 364 | | Anton Ertl |
4 Oct 24 |           Re: Byte ordering | 5 | | BGB |
5 Oct 24 |            Re: Byte ordering | 4 | | MitchAlsup1 |
5 Oct 24 |             Re: Byte ordering | 2 | | BGB |
5 Oct 24 |              Re: Byte ordering | 1 | | Lawrence D'Oliveiro |
5 Oct 24 |             Re: Byte ordering | 1 | | Lawrence D'Oliveiro |
5 Oct 24 |           Re: Byte ordering (was: Whether something is RISC or not) | 13 | | Lawrence D'Oliveiro |
5 Oct 24 |            Re: Byte ordering (was: Whether something is RISC or not) | 12 | | Brett |
5 Oct 24 |             Re: Byte ordering (was: Whether something is RISC or not) | 11 | | Anton Ertl |
5 Oct 24 |              Re: Byte ordering (was: Whether something is RISC or not) | 10 | | Michael S |
6 Oct 24 |               Re: Byte ordering | 1 | | Terje Mathisen |
6 Oct 24 |               Re: Byte ordering (was: Whether something is RISC or not) | 8 | | Brett |
7 Oct 24 |                Re: Byte ordering (was: Whether something is RISC or not) | 7 | | Lawrence D'Oliveiro |
7 Oct 24 |                 Re: Byte ordering (was: Whether something is RISC or not) | 6 | | Brett |
7 Oct 24 |                  Re: Byte ordering (was: Whether something is RISC or not) | 5 | | Michael S |
7 Oct 24 |                   Re: Byte ordering | 2 | | Stefan Monnier |
7 Oct 24 |                    Re: Byte ordering | 1 | | Michael S |
7 Oct 24 |                   Re: Byte ordering (was: Whether something is RISC or not) | 2 | | Lawrence D'Oliveiro |
8 Oct 24 |                    Re: Byte ordering | 1 | | Terje Mathisen |
6 Oct 24 |           Re: Byte ordering | 345 | | David Brown |
6 Oct 24 |            Re: Byte ordering | 344 | | Anton Ertl |
6 Oct 24 |             Re: Byte ordering | 189 | | John Dallman |
7 Oct 24 |              Re: Byte ordering | 20 | | Lawrence D'Oliveiro |
8 Oct 24 |               Re: Byte ordering | 19 | | John Dallman |
9 Oct 24 |                VMS/NT memory management (was: Byte ordering) | 1 | | Stefan Monnier |
15 Oct 24 |                Re: Byte ordering | 2 | | Lawrence D'Oliveiro |
15 Oct 24 |                 Re: Byte ordering | 1 | | MitchAlsup1 |
15 Oct 24 |                Re: Byte ordering | 15 | | Lawrence D'Oliveiro |
15 Oct 24 |                 Re: Byte ordering | 3 | | Michael S |
15 Oct 24 |                  Re: Byte ordering | 1 | | John Dallman |
18 Oct 24 |                  Re: Byte ordering | 1 | | Lawrence D'Oliveiro |
15 Oct 24 |                 Re: Byte ordering | 9 | | John Dallman |
16 Oct 24 |                  Re: Byte ordering | 7 | | George Neuner |
16 Oct 24 |                   Re: Byte ordering | 6 | | Terje Mathisen |
16 Oct 24 |                    Re: Byte ordering | 5 | | David Brown |
17 Oct 24 |                     Re: Byte ordering | 2 | | George Neuner |
17 Oct 24 |                      Re: Byte ordering | 1 | | David Brown |
17 Oct 24 |                     Re: clouds, not Byte ordering | 2 | | John Levine |
17 Oct 24 |                      Re: clouds, not Byte ordering | 1 | | David Brown |
18 Oct 24 |                  Re: Byte ordering | 1 | | Lawrence D'Oliveiro |
16 Oct 24 |                 Re: Byte ordering | 2 | | Paul A. Clayton |
18 Oct 24 |                  Re: Microkernels & Capabilities (was Re: Byte ordering) | 1 | | Lawrence D'Oliveiro |
7 Oct 24 |              80286 protected mode | 168 | | Anton Ertl |
7 Oct 24 |               Re: 80286 protected mode | 5 | | Lars Poulsen |
7 Oct 24 |                Re: 80286 protected mode | 4 | | Terje Mathisen |
7 Oct 24 |                 Re: 80286 protected mode | 1 | | Michael S |
7 Oct 24 |                 Re: 80286 protected mode | 2 | | Lawrence D'Oliveiro |
8 Oct 24 |                  Re: 80286 protected mode | 1 | | Terje Mathisen |
7 Oct 24 |               Re: 80286 protected mode | 3 | | Brett |
7 Oct 24 |                Re: 80286 protected mode | 2 | | Michael S |
7 Oct 24 |                 Re: 80286 protected mode | 1 | | Brett |
7 Oct 24 |               Re: 80286 protected mode | 1 | | Lawrence D'Oliveiro |
8 Oct 24 |               Re: 80286 protected mode | 152 | | MitchAlsup1 |
8 Oct 24 |                Re: 80286 protected mode | 4 | | Lawrence D'Oliveiro |
8 Oct 24 |                 Re: 80286 protected mode | 3 | | MitchAlsup1 |
9 Oct 24 |                  Re: 80286 protected mode | 1 | | David Brown |
15 Oct 24 |                  Re: 80286 protected mode | 1 | | Lawrence D'Oliveiro |
8 Oct 24 |                Re: 80286 protected mode | 147 | | Anton Ertl |
8 Oct 24 |                 Re: 80286 protected mode | 1 | | Robert Finch |
9 Oct 24 |                 Re: 80286 protected mode | 145 | | David Brown |
9 Oct 24 |                  Re: 80286 protected mode | 79 | | MitchAlsup1 |
9 Oct 24 |                   Re: 80286 protected mode | 78 | | David Brown |
9 Oct 24 |                    Re: 80286 protected mode | 77 | | Stephen Fuld |
10 Oct 24 |                     Re: 80286 protected mode | 2 | | MitchAlsup1 |
10 Oct 24 |                      Re: 80286 protected mode | 1 | | David Brown |
10 Oct 24 |                     Re: 80286 protected mode | 1 | | David Brown |
11 Oct 24 |                     Re: 80286 protected mode | 73 | | Tim Rentsch |
15 Oct 24 |                      Re: 80286 protected mode | 72 | | Stefan Monnier |
15 Oct 24 |                       Re: 80286 protected mode | 30 | | MitchAlsup1 |
16 Oct 24 |                        Re: 80286 protected mode | 25 | | MitchAlsup1 |
16 Oct 24 |                         Re: C and turtles, 80286 protected mode | 13 | | John Levine |
16 Oct 24 |                          Re: C and turtles, 80286 protected mode | 7 | | MitchAlsup1 |
16 Oct 24 |                           Re: C and turtles, 80286 protected mode | 6 | | John Levine |
17 Oct 24 |                            Re: C and turtles, 80286 protected mode | 5 | | Thomas Koenig |
20 Oct 24 |                             Re: C and turtles, 80286 protected mode | 4 | | Lawrence D'Oliveiro |
20 Oct 24 |                              Re: C and turtles, 80286 protected mode | 3 | | George Neuner |
22 Oct 24 |                               Re: C and turtles, 80286 protected mode | 2 | | Tim Rentsch |
22 Oct 24 |                                Re: C and turtles, 80286 protected mode | 1 | | George Neuner |
16 Oct 24 |                          Re: C and turtles, 80286 protected mode | 1 | | David Brown |
16 Oct 24 |                          Re: C and turtles, 80286 protected mode | 4 | | Paul A. Clayton |
17 Oct 24 |                           Re: C and turtles, 80286 protected mode | 1 | | David Brown |
20 Oct 24 |                           Re: C and turtles, 80286 protected mode | 2 | | Lawrence D'Oliveiro |
20 Oct 24 |                            Re: C and turtles, 80286 protected mode | 1 | | Paul A. Clayton |
16 Oct 24 |                         Re: 80286 protected mode | 7 | | Thomas Koenig |
16 Oct 24 |                          Re: 80286 protected mode | 2 | | MitchAlsup1 |
17 Oct 24 |                           Re: 80286 protected mode | 1 | | Tim Rentsch |
17 Oct 24 |                          Re: 80286 protected mode | 4 | | Tim Rentsch |
17 Oct 24 |                           Re: fine points of dynamic memory allocation, not 80286 protected mode | 3 | | John Levine |
17 Oct 24 |                         Re: 80286 protected mode | 3 | | George Neuner |
17 Oct 24 |                         Re: 80286 protected mode | 1 | | Tim Rentsch |
16 Oct 24 |                        Re: 80286 protected mode | 3 | | David Brown |
17 Oct 24 |                        Re: 80286 protected mode | 1 | | Tim Rentsch |
16 Oct 24 |                       Re: 80286 protected mode | 41 | | David Brown |
9 Oct 24 |                  Re: 80286 protected mode | 51 | | Thomas Koenig |
13 Oct 24 |                  Re: 80286 protected mode | 14 | | Anton Ertl |
8 Oct 24 |               Re: 80286 protected mode | 6 | | John Levine |
3 Jan 25 |             Re: Byte ordering | 154 | | Waldek Hebisch |
6 Oct 24 |         Re: Byte ordering (was: Whether something is RISC or not) | 2 | | Michael S |
3 Oct 24 |        Re: Byte ordering (was: Whether something is RISC or not) | 1 | | John Dallman |
2 Oct 24 |    Re: Whether something is RISC or not (Re: PDP-8 theology, not Concertina II Progress) | 1 | | Thomas Koenig |
2 Oct 24 |   Re: Whether something is RISC or not (Re: PDP-8 theology, not Concertina II Progress) | 5 | | David Schultz |
3 Oct 24 |   Re: Whether something is RISC or not (Re: PDP-8 theology, not Concertina II Progress) | 1 | | Lawrence D'Oliveiro |