[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Is Linux Unix?



Ryo Furue wrote:
Roel Schroeven <rschroev_nospam_ml@fastmail.fm> wrote in message news:<2oUpO-4mC-5@gated-at.bofh.it>...

Jeronimo Pellegrini wrote:

[...]

Actually, the first version works -- and both C and C++ (tested here with
gcc -- not sure it it became a standard or not)) will dynamically allocate
memory for you. Try this:

       int size1, size2, i, j;
       cin >> size1;
       cin >> size2;
       double vec[size1][size2];

And enter something like "1000" and "1000" when asked for the array dimensions.
It compiles, runs, and does not segfault! :-)

Your program segfaults on cygwin (using gcc 3.3.1) when using 1000 and 1000. On my sarge installation (gcc 3.3.4) it seems to work when using 1000 and 1000, but segfaults when using 1000 and 10000. Either you were lucky, or it's a feature that's introduced in more recent versions of gcc.


There is a posibility that you run out of stackspace.  I guess these
dynamically sized arrays are allocated on the stack.  You might want to
try setting the stacksize to "unlimited".

Sounds plausible. double vec[size1][size2] with size1=1000 and size2=10000 allocates 80 MB and with size2=1000 still allocates 8 MB, which I imagine can be quite hard on the stack.

I've found that gcc has implemented this at least for seven years or so.
If I remember correctly I found a news article in the google groups dated
in 1996 which mentions this feature of gcc.

Also, I found that the C99 standard incorporated this feature:

   http://home.tiscalinet.ch/t_wolf/tw/c/c9x_changes.html

Cool, I didn't know that.

It's interesting that some of the new features are (or seem to me to be)
mainly for numerical computations, such as the "restrict" keyword and
the builtin complex numbers.

Possibly, but isn't this feature's usefullness in numerical computations quite limited if you're restricted to the size of the stack?

I suspect they are influenced by Fortran.
And it's amusing to see that C99 has *at last* caught up with Fortran 77
(partly with Fortran 90/95) in terms of dynamically sized multidimensional
arrays, complex arithmetics, and "restrict"ed pointers, although C99 lags
far behind Fortran 90/95 in terms of intrinsic array operations. ;-)

Yeah well, it's not a coincidence that C is often called 'portable assembler'.

--
"Codito ergo sum"
Roel Schroeven



Reply to: