I noticed that when f2c converts a fortran subroutine to a c function
it decrements the pointer sent as an argument to the function and then
uses 1 based addressing system. If am I not wrong, one can increment
a pointer to one past the end of the array but not reference it. But
does a similar rule hold for one before the beggining of an array?
I am currently using an array larger than the required size by one and
sending array+1 as to the function just to be safe i.e., instead of
double a[]={1.,2.,3.};
f(a);
I am doing
double a[]={/*some junk*/-1.,1.,2.,3.};
f(a+1);
but is it really necessary? 2 2292
gc wrote: I noticed that when f2c converts a fortran subroutine to a c function it decrements the pointer sent as an argument to the function and then uses 1 based addressing system.
Naughty f2c.
If am I not wrong, one can increment a pointer to one past the end of the array but not reference it.
Right.
But does a similar rule hold for one before the beggining of an array?
No. The behaviour is undefined.
I am currently using an array larger than the required size by one and sending array+1 as to the function just to be safe i.e.,
A very sensible precaution.
instead of
double a[]={1.,2.,3.}; f(a);
I am doing double a[]={/*some junk*/-1.,1.,2.,3.}; f(a+1);
but is it really necessary?
It makes the behaviour of your program well-defined. Is that really
necessary? I think so, yes.
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton gu**********@yahoo.com (gc) wrote in message news:<79**************************@posting.google. com>... I noticed that when f2c converts a fortran subroutine to a c function it decrements the pointer sent as an argument to the function and then uses 1 based addressing system. If am I not wrong, one can increment a pointer to one past the end of the array but not reference it.
Correct. The reason it's allowed is that it's useful and easy to
implement for compiler writers. You only need to make sure that adding
1 to the pointer makes sense which it does if e.g. there is a single
extra byte after the last element in the array.
But does a similar rule hold for one before the beggining of an array?
No, it's UB. The reason is that, although IMO it would be useful just
like the after-pointer, it could be difficult to support: Let T be a
very large type of size 1GB say. Let p be a T pointer: T * p; Then
p-1 would point 1G bytes before p which could be troublesome. This in
contrast to p+1 which just points to one byte after the last byte of
*p.
Note though that for small types there would be no problem allowing
p-1, just like p+1 is allowed. I actually suggested this here http://groups.google.com/groups?hl=e...Dcomp.lang.c.*
but basically no one else thought it was a good idea. The common
opinion was that before pointers aren't as useful as after pointers
and that just what would be allowed, i.e. what would constitute a
small type, would be messy.
I am currently using an array larger than the required size by one and sending array+1 as to the function just to be safe i.e., instead of
double a[]={1.,2.,3.}; f(a);
I am doing double a[]={/*some junk*/-1.,1.,2.,3.}; f(a+1);
but is it really necessary?
If you don't want UB it's necessary. For reasons discussed above
it's likely that your program would work fine also without the fix.
However, in your case it's an easy fix so why not do it? If the fix
would be hard or wasteful you should take into account the likelihood
of introducing bugs with the fix, memory waste etc. and weight that
against the likelihood of running the program somewhere where the
before pointer would break things.
Daniel Vallstrom This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Brian |
last post by:
I'm diddlying with a script, and found some behavior I don't understand.
Take this snippet:
for ($i = 0; $i <= count($m); $i++) {
array_shift($m);
reset($m);
}
|
by: Stormkid |
last post by:
Hi Group I'm trying to figure out a way that I can take two (two
dimensional) arrays and avShed and shed, and subtract the matching elements
in shed from avShed I've pasted the arrays blow from a...
|
by: lawrence |
last post by:
I wanted to test xml_parse_into_struct() so I took the example off of
www.php.net and put this code up on a site:
<?php
$simple = <<<END
<item>
|
by: vcardillo |
last post by:
Hello all,
Okay, I am having some troubles. What I am doing here is dealing with
an employee hierarchy that is stored in an array. It looks like this:
$employees = array( "user_id" => array(...
|
by: Sam Collett |
last post by:
How do I remove an item with a specified value from an array?
i.e. array values 1,2,2,5,7,12,15,21
remove 2 from array would return
1,5,7,12,15,21
(12 and 21 are NOT removed, duplicates are...
|
by: Mike S. Nowostawsky |
last post by:
I tried using the "toUpperCase()" property to change the value of an array
entity to uppercase BUT it tells me that the property is invalid. It seems
that an array is not considered an object when...
|
by: jr |
last post by:
Sorry for this very dumb question, but I've clearly got a long way to go!
Can someone please help me pass an array into a function. Here's a starting
point.
void TheMainFunc()
{
// Body of...
|
by: Leszek |
last post by:
Hi.
Is it possible in javascript to operate on an array without knowing how mamy
elements it has?
What i want to do is sending an array to a script, and this script should
add all values from...
|
by: Jim Carlock |
last post by:
Looking for suggestions on how to handle bad words that might
get passed in through $_GET variables.
My first thoughts included using str_replace() to strip out such
content, but then one ends...
|
by: =?Utf-8?B?U2hhcm9u?= |
last post by:
Hi Gurus,
I need to transfer a jagged array of byte by reference to unmanaged
function, The unmanaged code should changed the values of the array, and when
the unmanaged function returns I need...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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,...
|
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...
|
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...
|
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |