string vs const int memory alloc

V

Vulcan Fire

Hi

Why is that

int *p = 12;
core dumps

& not
char *p = "some_str";

Shouldnt the int also be written in the data segment??


Ad Varma
 
L

Lawrence Kirby

Hi

Why is that

int *p = 12;
core dumps

This is not valid C, you cannot implicitly convert an integer to a
pointer, except for the special case of 0 which is a null pointer
constant. Any C compiler should complain about the line above.
& not
char *p = "some_str";

If you had

int *p = (int *)12;

you would be convertinbg the value 12 to a pointer type. C doesn't define
what the result of this will be, it certainly doesn't guarantee that it is
a valid pointer.

In the second case you have a string literal. String literals define (i.e.
create) an object of type array of char. In other words they create
something legitimate for a pointer to point at. Other constants such as 12
or 'X' are simply values are don't create their own objects.
Shouldnt the int also be written in the data segment??

C doesn't have a real concept of "data segment". Since 12, and indeed
(int *)12 are just values they don't exist in objects so storage
type/location/method is not really applicable. A reasonable compiler might
well encode values in the instruction stream where it can. However as far
as C is concerned the only important things are:

1. is the code valid,

2. if so what behaviour is required of it.

The question of how such behaviour is brought about is not important and
could well vary from one compiler to the next.

Lawrence
 
D

Darius

Vulcan said:
Hi

Why is that

int *p = 12;
core dumps

& not
char *p = "some_str";

Shouldnt the int also be written in the data segment??


Ad Varma

when you write int *p=12;
compiler should return a warning because you are tryin to converrt an
integer to pointer without a cast. So if you really want to do this for
some reason you should cast it as int *p=(int *)12; but then if you do
something like *p=<integer>, the behaviour.

but when you write something like char *p="hello hell"
"hello hell" string literal is stored at some static memory(may be DS)
and a valid pointer is returned. thus this is a valid syntax.
 
D

Darius

Vulcan said:
Hi

Why is that

int *p = 12;
core dumps

& not
char *p = "some_str";

Shouldnt the int also be written in the data segment??


Ad Varma

when you write int *p=12;
compiler should return a warning because you are tryin to converrt an
integer to pointer without a cast. So if you really want to do this for
some reason you should cast it as int *p=(int *)12; but then if you do
something like *p=<integer>, the behaviour is undefined.

but when you write something like char *p="hello hell"
"hello hell" string literal is stored at some static memory(may be DS)
and a valid pointer is returned. thus this is a valid syntax.
 
J

Jonathan Bartlett

Vulcan said:
Hi

Why is that

int *p = 12;
core dumps

& not
char *p = "some_str";

Shouldnt the int also be written in the data segment??

Because "some_str" is a string, and strings actually resolve as pointers
(anytime a string literal is used in C, it is either interpretted as an
array or a pointer).

12 is an integer, and always resolves to 12. There are no instances
where 12 will resolve to a pointer to valid memory. Just FYI, it DID
allocate memory in the data segment -- for the pointer, which now has
the value 12, which probably points to inaccessible memory. If you
wanted p to point to a valid memory location, you can do

int i = 12; /* reserve storage for the integer */
int *p = &i; /* get the pointer to said storage */

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top