473,468 Members | 1,472 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem with use of pointer

Hello

Have a look at this little piece of code:

signed char sca[] = {2,-1,4,3,5,3,-8,1,9,2,3,6,-1,0,12,2};
signed char sc = *(sca+1);
short s = *((short*)(sca+sc)+4);

My question is: does the code invoke undefined behaviour, because it
tries to convert the pointer which points outside the array, or maybe
there's everything fine taking into account that just before
dereferencing, the value of pointer is set appropriately inside of
array?

Thanks for your help.
braton

May 16 '07 #1
16 1283
br****@gmail.com said:
Hello

Have a look at this little piece of code:

signed char sca[] = {2,-1,4,3,5,3,-8,1,9,2,3,6,-1,0,12,2};
signed char sc = *(sca+1);
short s = *((short*)(sca+sc)+4);

My question is: does the code invoke undefined behaviour, because it
tries to convert the pointer which points outside the array, or maybe
there's everything fine taking into account that just before
dereferencing, the value of pointer is set appropriately inside of
array?
Let's start off by assuming that sca is at address ZOG:0002

(The ZOG part is just to remind us that pointers need not be wholly
numeric values.)

sca + 1 is ZOG:0003, which is fine.

sc = *(sca + 1); is fine, too, and loads -1 into sc.

(sca + sc) is ZOG:0001, which points outside the object, and that
immediately means undefined behaviour. So does the cast to short *,
because there is no guarantee that ZOG:0001 is a correctly-aligned
address for a short int. Adding 4 to it /also/ invokes undefined
behaviour if sizeof(short) is big enough, and dereffing the resulting
pointer is just heaping undefined behaviour up into a big wobbly pile.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 16 '07 #2
Hello
>
Have a look at this little piece of code:
signed char sca[] = {2,-1,4,3,5,3,-8,1,9,2,3,6,-1,0,12,2};
signed char sc = *(sca+1);
short s = *((short*)(sca+sc)+4);
My question is: does the code invoke undefined behaviour, because it
tries to convert the pointer which points outside the array, or maybe
there's everything fine taking into account that just before
dereferencing, the value of pointer is set appropriately inside of
array?

Let's start off by assuming that sca is at address ZOG:0002

(The ZOG part is just to remind us that pointers need not be wholly
numeric values.)

sca + 1 is ZOG:0003, which is fine.

sc = *(sca + 1); is fine, too, and loads -1 into sc.

(sca + sc) is ZOG:0001, which points outside the object, and that
immediately means undefined behaviour. So does the cast to short *,
because there is no guarantee that ZOG:0001 is a correctly-aligned
address for a short int. Adding 4 to it /also/ invokes undefined
behaviour if sizeof(short) is big enough, and dereffing the resulting
pointer is just heaping undefined behaviour up into a big wobbly pile.
I forgot mention that sizeof(short)==2 so if addition of 4 would be
possible then pointer would points to 1 in array, but I see it won't
reach that far.
Thank you for clarification!
braton

May 16 '07 #3
br****@gmail.com wrote:
>>>signed char sca[] = {2,-1,4,3,5,3,-8,1,9,2,3,6,-1,0,12,2};
signed char sc = *(sca+1);
short s = *((short*)(sca+sc)+4);
>>>My question is: does the code invoke undefined behaviour,

(sca + sc) is ZOG:0001, which points outside the object, and that
immediately means undefined behaviour. So does the cast to short *,
because there is no guarantee that ZOG:0001 is a correctly-aligned
address for a short int. Adding 4 to it /also/ invokes undefined
behaviour if sizeof(short) is big enough, and dereffing the resulting
pointer is just heaping undefined behaviour up into a big wobbly pile.

I forgot mention that sizeof(short)==2 so if addition of 4 would be
possible then pointer would points to 1 in array, but I see it won't
reach that far.
Keep in mind that if you ask if behavior is defined in this newsgroup,
the assumed context is definition by the C Standard, which doesn't
specify sizeof(short). If you want to know if the behavior of your code
is defined for a particular implementation, then you need to ask
somewhere that addresses that implementation.

--
Thad
May 17 '07 #4

Richard Heathfield wrote:
br****@gmail.com said:
Hello

Have a look at this little piece of code:

signed char sca[] = {2,-1,4,3,5,3,-8,1,9,2,3,6,-1,0,12,2};
signed char sc = *(sca+1);
short s = *((short*)(sca+sc)+4);

My question is: does the code invoke undefined behaviour, because it
tries to convert the pointer which points outside the array, or maybe
there's everything fine taking into account that just before
dereferencing, the value of pointer is set appropriately inside of
array?

Let's start off by assuming that sca is at address ZOG:0002

(The ZOG part is just to remind us that pointers need not be wholly
numeric values.)
Are there any examples of implementations where addresses are not
numeric values? I understand you point but just curious to know if
such an address really exists.

May 17 '07 #5
p_***********@yahoo.co.in said:
>
Richard Heathfield wrote:
>>
Let's start off by assuming that sca is at address ZOG:0002

(The ZOG part is just to remind us that pointers need not be wholly
numeric values.)

Are there any examples of implementations where addresses are not
numeric values? I understand you point but just curious to know if
such an address really exists.
My claim was they are not "wholly numeric" - although wholly non-numeric
values are not forbidden by the Standard, they don't sound terribly
useful, do they?

And yes, there are examples of implementations where addresses are not
wholly numeric. The following code fragment:

sprintf(s, "%p", (void *)ptr); /* get text rep of ptr */
printf("%.2s\n", s); /* print first two characters only */

when put into suitable clothes and executed under MS-DOS, might well
give you an output such as "SS", "DS", or something similar.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 17 '07 #6
p_***********@yahoo.co.in wrote:
Richard Heathfield wrote:
>[...]
Let's start off by assuming that sca is at address ZOG:0002

(The ZOG part is just to remind us that pointers need not be wholly
numeric values.)

Are there any examples of implementations where addresses are not
numeric values? I understand you point but just curious to know if
such an address really exists.
This is a variant of Question 5.17 in the comp.lang.c
Frequently Asked Questions (FAQ) list at <http://c-faq.com/>.

--
Eric Sosman
es*****@acm-dot-org.invalid
May 17 '07 #7
br****@gmail.com wrote:
>
Hello

Have a look at this little piece of code:

signed char sca[] = {2,-1,4,3,5,3,-8,1,9,2,3,6,-1,0,12,2};
signed char sc = *(sca+1);
short s = *((short*)(sca+sc)+4);

My question is: does the code invoke undefined behaviour,
Yes.
because it
tries to convert the pointer which points outside the array, or maybe
there's everything fine taking into account that just before
dereferencing, the value of pointer is set appropriately inside of
sc is equal to -1, which makes (sca+sc) undefined.

printf("sc is %d\n", sc);

N869
6.5.6 Additive operators
Constraints
[#8]
If both the
pointer operand and the result point to elements of the same
array object, or one past the last element of the array
object, the evaluation shall not produce an overflow;
otherwise, the behavior is undefined.

--
pete
May 17 '07 #8
In article <46**********@mindspring.com>, pete <pf*****@mindspring.comwrote:
>short s = *((short*)(sca+sc)+4);
>sc is equal to -1, which makes (sca+sc) undefined.
An example of the non-associativity of addition in C. You don't need
pointers for it: if INT_MAX is 32767 then

(32767 + 1) + (-1)

is undefined, though

32767 + (1 + (-1))

is 32767.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
May 17 '07 #9
Richard Heathfield <rj*@see.sig.invalidwrites:
p_***********@yahoo.co.in said:
>Richard Heathfield wrote:
>>>
Let's start off by assuming that sca is at address ZOG:0002

(The ZOG part is just to remind us that pointers need not be wholly
numeric values.)

Are there any examples of implementations where addresses are not
numeric values? I understand you point but just curious to know if
such an address really exists.

My claim was they are not "wholly numeric" - although wholly non-numeric
values are not forbidden by the Standard, they don't sound terribly
useful, do they?
Well, sort of. The representation of any pointer value (like the
representation of anything other than a bit field) must be composed of
bytes, and bytes must be composed of bits. So there's always going to
be a way to represent a pointer value as a number, though perhaps not
uniquely, and the resulting number might not be meaningful. (And it
might be bigger than any numeric type supported by the
implementation.)

The more relevant question, I think, is whether pointers can always be
viewed as numbers with a straightforward linear relationship between
the numeric values and memory addresses. The answer to that is no
(though such a relationship does exist on many systems, perhaps most
of them).
And yes, there are examples of implementations where addresses are not
wholly numeric. The following code fragment:

sprintf(s, "%p", (void *)ptr); /* get text rep of ptr */
printf("%.2s\n", s); /* print first two characters only */

when put into suitable clothes and executed under MS-DOS, might well
give you an output such as "SS", "DS", or something similar.
The conversion performed by "%p" is implementation-defined; in this
case, the implementation chose to use things like "SS" and "DS" rather
than numbers because that's what makes the most sense on that system.

Pointers are numeric in the sense that *everything* is numeric,
because everything is made of bits. But in the most general case,
pointers are numeric *only* in that sense.

Richard's basic point is entirely correct, as it usually is; I'm
merely quibbling about semantics.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 17 '07 #10
In article <11**********************@e65g2000hsc.googlegroups .com>
<p_***********@yahoo.co.inwrote:
>Are there any examples of implementations where addresses are not
numeric values? I understand you point but just curious to know if
such an address really exists.
Perhaps you mean "such a machine". They do exist -- even the x86
*can* work this way, although it "never" (?) does in practice --
but they were much more common in the past. I have seen mention
of a machine that used "floating-point pointers" (I have no idea
how this worked), and machines that used segment tables were
pretty common in the 1970s.

See also <http://web.torek.net/torek/c/numbers2.html>.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
May 18 '07 #11
Richard Tobin wrote:
>
In article <46**********@mindspring.com>, pete <pf*****@mindspring.comwrote:
short s = *((short*)(sca+sc)+4);
sc is equal to -1, which makes (sca+sc) undefined.

An example of the non-associativity of addition in C. You don't need
pointers for it: if INT_MAX is 32767 then

(32767 + 1) + (-1)

is undefined, though

32767 + (1 + (-1))

is 32767.
On my machine where FLT_ROUNDS equals 1,
if x and y are set equal to (4 * DBL_EPSILON / 3)
and z is set equal to 2.0, then
((x + y) + z) is less than (x + (y + z))
and
((x + y) + z) equals (y + z)

/* BEGIN epsilon.c */

#include <stdio.h>
#include <float.h>

int main(void)
{
double x, y, z;

puts("/* BEGIN ouptput from epsilon.c */");
printf("\nFLT_ROUNDS is %d\n\n", FLT_ROUNDS);
x = y = 4 * DBL_EPSILON / 3;
puts("x = y = 4 * DBL_EPSILON / 3;");
z = 2.0;
puts("z = 2.0;\n");
if ( (x + y) + z == x + (y + z) ) {
puts("(x + y) + z == x + (y + z)");
}
if ( (x + y) + z x + (y + z) ) {
puts("(x + y) + z x + (y + z)");
}
if ( (x + y) + z < x + (y + z) ) {
puts("(x + y) + z < x + (y + z)");
}
if ( (x + y) + z == y + z) {
puts("(x + y) + z == y + z");
}
puts("\n/* END ouptput from epsilon.c */");
return 0;
}

/* END epsilon.c */
--
pete
May 19 '07 #12
Keith Thompson wrote:
Pointers are numeric in the sense that *everything* is numeric,
because everything is made of bits. But in the most general case,
pointers are numeric *only* in that sense.
Pointers are more numeric than that.

Pointers are scalar types,
which makes pointers more numeric than nonscalar types.

There are arithmetic operators and other operators
that can be used with pointers and arithmetic types,
but not with nonscalar types such as stuctures and arrays.

--
pete
May 19 '07 #13
p_***********@yahoo.co.in wrote:
>
.... snip ...
>
Are there any examples of implementations where addresses are not
numeric values? I understand you point but just curious to know if
such an address really exists.
Yes.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 19 '07 #14
pete <pf*****@mindspring.comwrites:
Keith Thompson wrote:
>Pointers are numeric in the sense that *everything* is numeric,
because everything is made of bits. But in the most general case,
pointers are numeric *only* in that sense.

Pointers are more numeric than that.

Pointers are scalar types,
which makes pointers more numeric than nonscalar types.

There are arithmetic operators and other operators
that can be used with pointers and arithmetic types,
but not with nonscalar types such as stuctures and arrays.
Hmm. That's one way to look at it, I suppose.

The way I look at it is that numeric types are a subset of scalar
types, which in turn are a subset of object types. Pointers are
scalar, but not numeric.

<OT>In Perl, for example, strings are "scalar", but they're certainly
not numeric.</OT>

On the other hand, the standard's definition of "alignment" is vaguely
troubling (C99 3.2):

alignment

requirement that objects of a particular type be located on
storage boundaries with addresses that are particular multiples of
a byte address

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 19 '07 #15
Keith Thompson wrote:
>
pete <pf*****@mindspring.comwrites:
Keith Thompson wrote:
Pointers are numeric in the sense that *everything* is numeric,
because everything is made of bits. But in the most general case,
pointers are numeric *only* in that sense.
Pointers are more numeric than that.

Pointers are scalar types,
which makes pointers more numeric than nonscalar types.

There are arithmetic operators and other operators
that can be used with pointers and arithmetic types,
but not with nonscalar types such as stuctures and arrays.

Hmm. That's one way to look at it, I suppose.

The way I look at it is that numeric types are a subset of scalar
types, which in turn are a subset of object types. Pointers are
scalar, but not numeric.
"numeric types" isn't a technical term in C.
The standard describes scalars as being compsed of pointers
and "arithmetic types".

--
pete
May 20 '07 #16
pete <pf*****@mindspring.comwrites:
Keith Thompson wrote:
[...]
>The way I look at it is that numeric types are a subset of scalar
types, which in turn are a subset of object types. Pointers are
scalar, but not numeric.

"numeric types" isn't a technical term in C.
The standard describes scalars as being compsed of pointers
and "arithmetic types".
Agreed, but I didn't introduce the word "numeric" to the discussion.
I think of "numeric types" as being a synonym for "arithmetic types".
Pointers are not numeric because they're not numbers. They're not
more or less numeric than anything else; they're just not numbers.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 20 '07 #17

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

Similar topics

4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
28
by: Davy | last post by:
Hi all, I found char x={"my"}; can be compiled. But char x; x={"my"}; can not be compiled.
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
37
by: Patrik Huber | last post by:
Hello! I got the following Code in Assembler (NASM), which prints out "5" in realmode: mov ax, 0xB800 mov es, ax mov byte , '5' I want to do the same in gcc now, but I'm stuck. GCC...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
7
by: Marcelo | last post by:
Hi everybody, I don't understand why I am having a problem in this code. The problem is that my pointer *phist in main method, it is declared. Then I send the pointer to my method, and this...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
3
by: iskeletor | last post by:
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #define STUDENT_NUMBER 68 #define ARRAY_LENGTH 10 struct node{ char Name,Surname; int data,no;
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
marktang
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,...
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
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.