473,396 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Variable size arrays and pointers

Already in the 1st edition of the "White book",
Kerigham and Ritchie states the "C is a general purpose
language". However, without what is usually called
"assumed size arrays" and built-in complex, C is not
entirely suitable for numerical work. In particular,
in order to have functions which can manipulate
matrices whose dimensions are not determined at compile
time one needs pointers of the form, say,
double (*a)[n]
where n is not a compile time constant but determined
when control passes the pointer declaration. The C89
standard requires that n is a compile time constant.

After the declaration one then fetches matrix elements
with syntax
a[i][j].

In the absence of this feature, the typical
C/C++ solution is to use double pointers

double **b,

an intermediate pointer array,

and in this way we can again access matrix elements
with syntax
b[i][j].

From performance point of view, the latter solution is
a disaster, since it requires 2 defererencing steps
(just think of matrix multiply where we need to loop
over the "slow" index too).

GCC has supported variable-size pointers (and arrays)
for many years. It has been supported in fortran
at least from fortran66.

My question is simply:
Are variable-size pointers allowed in C99, which
would make the language fully suitable also for
numerical work.

Carl-Olof Almbladh

Nov 14 '05 #1
6 3140
My question is simply:
Are variable-size pointers allowed in C99, which
would make the language fully suitable also for
numerical work.

No, but variable sized arrays are.
Nov 14 '05 #2
In <ck**********@news.lth.se> co*@urd.teorfys.lu.se (Carl-Olof Almbladh) writes:
My question is simply:
Are variable-size pointers allowed in C99, which
would make the language fully suitable also for
numerical work.


I've no idea what you mean by "variable-size pointers", but variable
length arrays and pointers to variable length arrays are supported by C99.

And, as the rest of your post makes perfectly clear, this is exactly what
you need.

However, they come too late to make much of a difference: the people
heavily involved in numerical analysis didn't wait for C to acquire this
feature and adopted other languages that already provided decent support
for *efficient* matrix processing.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #3
> After the declaration one then fetches matrix elements
with syntax
a[i][j].

In the absence of this feature, the typical
C/C++ solution is to use double pointers

double **b,

an intermediate pointer array,

and in this way we can again access matrix elements
with syntax
b[i][j].

From performance point of view, the latter solution is
a disaster, since it requires 2 defererencing steps
(just think of matrix multiply where we need to loop
over the "slow" index too).


It's not always true that two dereference steps are required in your latter
case. Consider the following method for allocating a dynamic MxN array:

int ** arr = (int**) malloc(sizeof(int*) * M);
arr[0] = (int*) malloc(sizeof(int) * M * N);
for (i = 1; i < M; i++)
arr[i] = arr[0] + i*M;

Now, all elements are contiguous and you can use arr[i][j] syntax to access
any array element with only one dereference (eg. arr[i*M + j]).
Nov 14 '05 #4
Carl-Olof Almbladh <co*@urd.teorfys.lu.se> wrote:
[snip]
From performance point of view, the latter solution is
a disaster, since it requires 2 defererencing steps
(just think of matrix multiply where we need to loop
over the "slow" index too).


You have measured the difference, haven't you?

My simple tests show that for unoptimized compilation with gcc the
difference between accessing an array-of-double through a "single" or
"double dereference" was about 1% of total program run. I wouldn't call
that a "disaster", rather a measurement error.

Using variable size array, the program ran 20% slower.

I just can't imagine how an additional dereference could make a difference
beside other operations that must be there, such as multiplication.

With -O3 optimization (gcc 3.3.4) the results were somewhat strange.
The tests with "double dereference" ran up to 20% faster. Perhaps that
might be a result of better loop translation (2 smaller loops) than in the
"single dereference" (1 large loop). I haven't time to investigate this.

Tests with variable size array ran about the same as "double dereference".

What are your results?

--
Stan Tobias
sed 's/[A-Z]//g' to email
Nov 14 '05 #5
"Method Man" <a@b.c> wrote:
From performance point of view, the latter solution is
a disaster, since it requires 2 defererencing steps
(just think of matrix multiply where we need to loop
over the "slow" index too).

It's not always true that two dereference steps are required in your latter
case. Consider the following method for allocating a dynamic MxN array:

int ** arr = (int**) malloc(sizeof(int*) * M);
arr[0] = (int*) malloc(sizeof(int) * M * N);


What are those casts for?
for (i = 1; i < M; i++)
arr[i] = arr[0] + i*M;

Now, all elements are contiguous and you can use arr[i][j] syntax to access
any array element with only one dereference (eg. arr[i*M + j]).


There are still two dereferences in the syntax: arr[i][j].
By definition this is: *( *(arr + i) + j).

You have saved on construction costs but not saved on access costs.

arr[i*M + j] would give an int pointer (probably an out of bounds one).
You could write:
int *a = arr[0];
and then have accesses with a single subsequent dereference:
a[i*M + j] ...
but this is a far cry from arr[i][j].
Nov 14 '05 #6
From my original question about variable size arrays
and pointers I have got a clear answer form Dan Pop that they are
included in C99.

There has also been some discussion about the actual permformance
penalties of using double pointers
double **a;
and intermediate pointer arrays
instead of variable-size pointers and arrays
double b[n][m];
double (*a)[m] = b; /* n, m not compile time constants */
for matrix manipulations. (The declaration of a of course
involves no memory allocation but just informs the compliler
how to interpret a[i][j];)

My own experience for a variety of platforms (Sun, Cray, HP ..)
is that it is platform dependent but not at all negligible in most cases.
(In the absence of pointers (*a)[n] one has to work with 1D
arrays and do the indexing by hand.)
I agree with Dan Pop that the extension comes 20 years
too late - most people doing numerical work have not
had time to wait but moved to fortran where "assumed-size arrays"
have been supported for some 40 years.

Carl-Olof Almbladh
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Linny | last post by:
Hi, I need some help in declaring an array of pointers to array of a certain fixed size. I want the pointers to point to arrays of fixed size only (should not work for variable sized arrays of the...
17
by: Davor | last post by:
How to define global variable in main()? I'm asking because I have an array in main, whose size is determined by input, so the definition has to be in main ( or in some other funcion ). And I need...
5
by: dam_fool_2003 | last post by:
Hai, I studied that the array size is fixed. But I come across a word called "variable length array". Is it possible to change the array size? So I tried the following: #include<stdio.h>...
14
by: Christopher Benson-Manica | last post by:
From FAQ 6.13: int arr; int (*a)=arr; /* legal, but useless */ printf( "%d\n", a ); /* error, bounds of a unspecified */ Are there non-contrived situations where an array of unspecified...
6
by: JNY | last post by:
Hello, Is it possible to declare an array with variable indeces? i.e. int x = 4; int myArray; for (j = 0;j < 5;j++) {
10
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
37
by: Erwin Lindemann | last post by:
If a VLA appears within a loop body, it seems the behavior is different with two different compilers I tried. I looked at the standard text, but couldn't find a definite answer there either. ...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
28
by: Trups | last post by:
HI, I want to dynamically allocate array variable of type LPWSTR. Code looks like this... main() { LPWSTR *wstr; int count = Foo (wstr); for (int i = 0; i < count; i++)
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.