converting an char pointer to an integer pointer 
Author Message
 converting an char pointer to an integer pointer

Hello all

I assume that the following

char *p;
/* p is assigned memory here */

*(int *) p = 42;

should cause UB, but I cannot find a reference in the c99
standard (dont have c89) which covers the conversion of
a pointer to an integer type being converted to a pointer
to another integer type of higher rank.

The above code generates a trap on the m68k architecture.

The reference that I have found in the standard is
6.3.1.3 #3 and 6.2.5 #6, which says that a trap *can* be
raised by the implementation if the value cannot be
represented, and that differently ranked integer types
can have different alignments.

anything in the standard (either one) specifically
prohibits the above piece of code ?

(btw: I did not write the offending code, I was merely
given the piece of code and asked what was wrong with
it. I am currently trying to get everyone I work
with to adhere to c89)

goose,
   trying to rid the world of UB



Tue, 22 Nov 2005 16:42:07 GMT  
 converting an char pointer to an integer pointer

Quote:
> Hello all

> I assume that the following

> char *p;
> /* p is assigned memory here */

How?? Is it

char o;
char *p = &o;
??
Or rather:
char *p = malloc(CERTAIN_NUMBER_OF_BYTE_BIGGER_THAN_SIZEOF_INT);
???

Quote:
> *(int *) p = 42;

The pointer p is converted to a int pointer, that is
dereferenced and the pointed to object is assigned a integer value.

The conversion does not invoke UB, but if the pointed object (memory) is
not capable of storing a int (say because it is to short or is not
correctly aligned) then dereferencing should be UB.

Malloc OTOH guarantees that the returned address is always aligned for
accessing any kind of object, and if the requested memory was big enough,
there shouldn't be a problem.

Quote:

> should cause UB, but I cannot find a reference in the c99
> standard (dont have c89) which covers the conversion of
> a pointer to an integer type being converted to a pointer
> to another integer type of higher rank.

> The above code generates a trap on the m68k architecture.

> The reference that I have found in the standard is
> 6.3.1.3 #3 and 6.2.5 #6, which says that a trap *can* be
> raised by the implementation if the value cannot be
> represented, and that differently ranked integer types
> can have different alignments.

--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond


Tue, 22 Nov 2005 17:33:43 GMT  
 converting an char pointer to an integer pointer

Quote:

> Hello all

> I assume that the following

> char *p;
> /* p is assigned memory here */

> *(int *) p = 42;

> should cause UB, but I cannot find a reference in the c99
> standard (dont have c89) which covers the conversion of
> a pointer to an integer type being converted to a pointer
> to another integer type of higher rank.

It depends on how "p is assigned memory".  If p points to sufficient
memory, correctly aligned, for storage of an int, then the behaviour is
defined.  Eg;

char *p = malloc(sizeof(int) + 10);

*(int *)p = 42;

... is fine.  char * is required to have the same representation as
void *, and void * must be able to represent all pointer values.  In
fact, in the days before void * existed, char * _was_ the generic
pointer type.

However, a version which does *not* have defined behaviour is:

char *p;
char x[10];

p = x;

*(int *)p = 42;

...because x (and thus p) is not necessarily correctly aligned for
storing an int, and may...

Quote:
> The above code generates a trap on the m68k architecture.

...yes, do that.

In terms of an explicit reference, pointer conversions say that the
result "...may not be correctly aligned for storage of the target
type..", so if you don't have any other guarantee about alignment (such
as that provided by malloc), then you can't assume anything about it.

        - Kevin.



Tue, 22 Nov 2005 17:50:48 GMT  
 converting an char pointer to an integer pointer

Quote:
> char *p;
> /* p is assigned memory here */

> *(int *) p = 42;

> should cause UB, but I cannot find a reference in the c99

It causes a UB because 'p' is not initialized, and that you attempt to
dereference it.

Quote:
> The above code generates a trap on the m68k architecture.

That's a nice UB. Some architecture are mute, that makes a UB so a dangerous
bug.

--
-ed- emdel at noos.fr
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-library: http://www.dinkumware.com/htm_cl/index.html
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
                          -- Jerry Coffin in a.l.c.c++



Thu, 24 Nov 2005 22:29:06 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. converting char to pointer char

2. Comparison with pointer to pointer to char, and char

3. how to enter integers into char *pointer

4. Converting a char array of integers into integers

5. pointer to an array OF pointers to integer data types

6. convert char to short int via pointer?...

7. Converting char pointers

8. Converting segment:offset to a C char pointer

9. Converting BSTR to char pointer

10. Converting from long to char pointer

11. char pointers vs. int pointers

12. Pointers to Pointers to Char in C

 

 
Powered by phpBB® Forum Software