473,383 Members | 1,821 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,383 software developers and data experts.

address beyond array declaration

Hi,

What is the need for the inaccessible pointer address beyond the end of an array?

Eg. could

for(ip = &array[0]; ip < &array[arraySize]; ip++)....

not be rewritten as

arraySize--;
for(ip = &array[0]; ip <= & array[arraySize]; ip++)....
Douglas
Nov 14 '05 #1
6 1393
"Douglas" <mm***@yahoo.co.uk> wrote in message
news:4c**************************@posting.google.c om...
Hi,

What is the need for the inaccessible pointer address beyond the end of an array?

It's sometimes convenient when iterating through an array.
But be sure not to try to dereference it.

Eg. could

for(ip = &array[0]; ip < &array[arraySize]; ip++)....

not be rewritten as

arraySize--;
for(ip = &array[0]; ip <= & array[arraySize]; ip++)....

Yes, it could also be written e.g.:

for(ip = &array[0]; ip <= & array[arraySize - 1]; ip++)....

However the first form above is the most 'idomatic'
(i.e. most quickly recognized by most C coders).

Use whichever form most clearly expresses your intent.
I don't like your second form, because if possible,
I'd declare 'arraySize' as 'const' (typically it would
be a function parameter), in which case the expression
'arraySize--' would not be allowed.

Personally, if I'm using an array's size as my
'boundary condition' (as opposed to using an array
element value, such as a string terminator), I'll use
indices rather than pointers, e.g.:

size_t i = 0
for(i = 0; i < arraySize; ++i)
/* etc */

-Mike
Nov 14 '05 #2
Douglas wrote:
Hi,

What is the need for the inaccessible pointer address beyond the end of an array?

Eg. could

for(ip = &array[0]; ip < &array[arraySize]; ip++)....

not be rewritten as

arraySize--;
for(ip = &array[0]; ip <= & array[arraySize]; ip++)....


Yes, you could write this and it would work. But it
still uses "one past the end" pointer: What is the value
of `ip' when the test fails?

--
Er*********@sun.com

Nov 14 '05 #3
Douglas wrote:
...
What is the need for the inaccessible pointer address beyond the end of an array?

Eg. could

for(ip = &array[0]; ip < &array[arraySize]; ip++)....

not be rewritten as

arraySize--;
for(ip = &array[0]; ip <= & array[arraySize]; ip++)....
...


Firstly, not in case when 'arraySize' is initially zero.

Secondly, your version still produces a pointer that points beyond the
end of an array.

--
Best regards,
Andrey Tarasevich

Nov 14 '05 #4
JV

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:_e*****************@newsread2.news.atl.earthl ink.net...
"Douglas" <mm***@yahoo.co.uk> wrote in message
news:4c**************************@posting.google.c om...
Hi,

What is the need for the inaccessible pointer address beyond the end of
an array?

It's sometimes convenient when iterating through an array.
But be sure not to try to dereference it.

BTW why the later sizeof (a) causes segmentation fault in the code below? I
guessed that if would have resulted still 5*sizeof (int). Now I just found
out that there really is difference between arrays and pointers that
matters:). Althought in practice I use arrays if the length is fixed and
just declare a pointer if I the number of elements varies runtime.
-Jyrki
int a[5];
int main(void){
printf("%i\n",sizeof(int));

printf("%i\n",sizeof(a));
realloc(a,10*sizeof(int));
printf("%i\n",sizeof(a));
}

-jyrki

Nov 14 '05 #5
"JV" <n.*@n.com.invalid> wrote:
BTW why the later sizeof (a) causes segmentation fault in the code below?
It doesn't. That is, that may be the line that triggers the segfault,
but the actual cause is...
int a[5];
int main(void){
printf("%i\n",sizeof(int));

printf("%i\n",sizeof(a));
realloc(a,10*sizeof(int));
....here. You cannot use realloc() on memory you didn't get from *alloc()
in the first place. In this case, you cannot use realloc() on a, which
is a static file-scope array.
This call invokes undefined behaviour; after it is executed, _nothing_
can be relied on. In theory, your program is now allowed to send love
letters to your supervisor. In practice, you've probably corrupted your
allocation arena...
printf("%i\n",sizeof(a));


....leading to a segfault next time you refer to this object, or use
another allocation function. But this line _in itself_ is not the
problem.

Richard
Nov 14 '05 #6
"JV" <n.*@n.com.invalid> wrote:
<snip>
BTW why the later sizeof (a) causes segmentation fault in the code below? I
guessed that if would have resulted still 5*sizeof (int). Now I just found
out that there really is difference between arrays and pointers that
matters:). <snip>
int a[5];
int main(void){
printf("%i\n",sizeof(int));

printf("%i\n",sizeof(a));
realloc(a,10*sizeof(int));
printf("%i\n",sizeof(a));
}


There's one more /very/ important difference: any attempt to
reallocate an array results in undefined behaviour, of which
the segfault you observed is only one incarnation.

BTW: your code fails to #include two necessary standard headers,
and the invocation of realloc would still be horribly wrong, even
if the first argument were a pointer previously returned by one
of the *alloc functions. Plus, omitting the return statement
in main is only sanctioned by ISO C99.

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #7

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

Similar topics

8
by: User | last post by:
Hi, This is very basic, It may be a repost, if so I'm sorry. The problem is that this declaration : Private strMyArray(100) As String will create an array of string with a length of 101,...
1
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of...
27
by: Adam Warner | last post by:
Hi all, In the code snippet below I successfully determine the address of val1:* struct o val1=l_SYM_2B(&a).o; print_aesthetic(&val1); The structure o is heavyweight. I understand...
9
by: Luke Wu | last post by:
Hello, I'm having some problems understanding 2 dimensional arrays. My problem relates to the following code: #include <stdio.h> #define M 3 #define N 3
3
by: dbru | last post by:
I need to pass an address of a Managed float array to a DLL. The following doesn't seem to work extern static float GetXXX( StringBuilder HWND, long nWhat, ref float lparam );
12
by: FI | last post by:
Hello All, I am relatively new to C programming and I am struck with a problem in dynamic memory allocation. I would like to know if it is ok to pass the 'memory address' returned by...
19
by: DarelRex | last post by:
Is it possible to pass a 2-D, statically defined array? Here's a 1-D example that won't work: void foo() { int myArray ; bar(myArray); } void bar(int *arr) {
17
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...
16
by: somenath | last post by:
Hi All. I would like to know the following information. 1)Is there any difference between the address and integer ? For example suppose int x = 500; And int y =10; Suppose address of y is...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.