Basic question about pointers

A

Aarti

I have a very elementary question about pointers. Please pardon me
for my ignorance of C

int main()
{
int* i;
*i = 1 //at times this may give me a core dump.
const char* str = "test"; //This seems to be a valid construct.
}

Now I understand that when we declare int* i, we just have a pointer
that may point to any place. If we try to store something in it, we
may get a segmentation fault. But why does const char* str = "test"
work? Do we not need to allocate memory for str first? Can someone
please shed some light on this?

Thanks in advance
 
C

Chris Johnson

I have a very elementary question about pointers. Please pardon me
for my ignorance of C

int main()
{
int* i;
*i = 1 //at times this may give me a core dump.
const char* str = "test"; //This seems to be a valid construct.
}

Now I understand that when we declare int* i, we just have a pointer
that may point to any place. If we try to store something in it, we
may get a segmentation fault. But why does const char* str = "test"
work? Do we not need to allocate memory for str first? Can someone
please shed some light on this?

Thanks in advance

http://c-faq.com/decl/strlitinit.html
 
B

Barry Schwarz

I have a very elementary question about pointers. Please pardon me
for my ignorance of C

int main()
{
int* i;
*i = 1 //at times this may give me a core dump.
const char* str = "test"; //This seems to be a valid construct.
}

Now I understand that when we declare int* i, we just have a pointer
that may point to any place. If we try to store something in it, we
may get a segmentation fault. But why does const char* str = "test"

Whether you get a segmentation fault or not, the effort to evaluate
any variable with an indeterminate value invokes undefined behavior.
work? Do we not need to allocate memory for str first? Can someone
please shed some light on this?

"test" is a string literal. When your compiler constructs your
program, that literal is part of the object file. The pointer str is
initialized with the address of the first 't' in that literal.

While not technically precise, you can think of it conceptually as the
compiler allocating five bytes for the literal; initializing those
five bytes with the values 't', 'e', 's', 't', and '\0'; and
initializing str to point to the start of this five byte area.

To answer your question - when initializing a pointer with a string
literal, you do not need to allocate memory for the pointer because
the compiler did it for you.


Remove del for email
 
S

santosh

Aarti said:
I have a very elementary question about pointers. Please pardon me
for my ignorance of C

int main()
{
int* i;
*i = 1 //at times this may give me a core dump.
const char* str = "test"; //This seems to be a valid construct.
}

Now I understand that when we declare int* i, we just have a pointer
that may point to any place. If we try to store something in it, we
may get a segmentation fault.

Not necessarily. Segmentation faults do occur under modern hardware
and operating systems, but not on all systems. Under DOS, for example,
a segmentation fault will not occur.

But in all cases, attempting to deference an indeterminate pointer
causes Undefined behaviour, as per the C Standard. Also the outcome of
storing an arbitrary numeric value into a pointer, (as you've done
above), is implementation defined. It may be perfectly sensible in one
situation, but fatal in another. It is however, not portable.
But why does const char* str = "test"
work? Do we not need to allocate memory for str first? Can someone
please shed some light on this?


That's because when the implementation compiled your program, it
automatically and anonymously allocated storage space for the string
and initialised str to point to the first element of it. This is a
part of the "high level" nature of languages like C. In assembler, for
example, you'd have to manually set aside storage for "test" and
explicitly initialise the pointer, (or register), with the start
address of this piece of storage.
 
M

Mohan

Aarti said:
I have a very elementary question about pointers. Please pardon me
for my ignorance of C

int main()
{
int* i;
*i = 1 //at times this may give me a core dump.
const char* str = "test"; //This seems to be a valid construct.
}

Now I understand that when we declare int* i, we just have a pointer
that may point to any place. If we try to store something in it, we
may get a segmentation fault. But why does const char* str = "test"
work?

"test" is a valid string literal, that means str will be pointing to a
valid pointer.
Where as 'i' is uninitialized and dereferencing 'i' causes undefined
behavior.
Do we not need to allocate memory for str first? Can someone
please shed some light on this?
Yes, 'str' in this case is pointing to a valid (allocated) memory.

Mohan
 
C

Christopher Benson-Manica

"test" is a valid string literal, that means str will be pointing to a
valid pointer.

str points to the first character of an array of characters. It does
not point to a pointer.
 
P

pete

Christopher said:
str points to the first character of an array of characters. It does
not point to a pointer.

You can also say that str is a pointer to a string.
 
B

Barry Schwarz

Not necessarily. Segmentation faults do occur under modern hardware
and operating systems, but not on all systems. Under DOS, for example,
a segmentation fault will not occur.

But in all cases, attempting to deference an indeterminate pointer
causes Undefined behaviour, as per the C Standard. Also the outcome of
storing an arbitrary numeric value into a pointer, (as you've done
above), is implementation defined. It may be perfectly sensible in one
situation, but fatal in another. It is however, not portable.

He was not attempting to store a numeric value into the pointer, but
into the object the pointer should have been pointing to.
That's because when the implementation compiled your program, it
automatically and anonymously allocated storage space for the string
and initialised str to point to the first element of it. This is a
part of the "high level" nature of languages like C. In assembler, for
example, you'd have to manually set aside storage for "test" and
explicitly initialise the pointer, (or register), with the start
address of this piece of storage.


Remove del for email
 
B

Barry Schwarz

"test" is a valid string literal, that means str will be pointing to a
valid pointer.

str will be pointing to a valid char.
Where as 'i' is uninitialized and dereferencing 'i' causes undefined
behavior.

Yes, 'str' in this case is pointing to a valid (allocated) memory.

Mohan


Remove del for email
 
U

Urs Beeli

On Mon, 16 Jul 2007 10:42:13 -0000 Mohan wrote:

[Quoting Mohan as my server no longer carries the OP]

I think you are confusing things due to different notations.

You have:
int* i;
*i = 1;

This is not correct, as i is not properly initialised so dereferencing it on
the second line causes undefined behaviour.

What you have above, is *NOT* the same as:
int* i = 1;
Here you create a pointer to an int and make it point to the address "1",
not defining the content of *i.

Had you written:
char* str;
*str = "test";
you would have had the same problem (plus an additional one), you would
have dereferenced the uninitialized pointer to char str. (And even had it
been initialised, it would be a syntax error to assign as string literal to
it).

Writing:
char* str;
str = "test";
would be correct and do the same thing as your example, but then
int* i;
i = 1;
would also be valid (and do the same as int *i=1;) but not do what you
tried.

The fact that the legal construct 'int *i=1;' contains the sequence '*i=1;'
does not mean that i is dereferenced, the * belongs to the declaration of i.

I imagine that this might have confused you.

Cheers
/urs
 
C

Chris Dollin

Urs said:
I think you are confusing things due to different notations.

You have:
int* i;
*i = 1;

This is not correct, as i is not properly initialised so dereferencing it on
the second line causes undefined behaviour.

What you have above, is *NOT* the same as:
int* i = 1;
Here you create a pointer to an int and make it point to the address "1",
not defining the content of *i.

Here you have a constraint violation. `1` is not a legal value for a
pointer. (If you cast the integer to a pointer, only the implementation
knows what will happen. I think it has to tell you, though.)
 
U

Urs Beeli

Here you have a constraint violation. `1` is not a legal value for a
pointer. (If you cast the integer to a pointer, only the implementation
knows what will happen. I think it has to tell you, though.)

Fair enough, I missed that in my zeal to unconfuse the OP :)

Cheers
/urs
 
B

Bryan Harris

int main()
{
int* i;
*i = 1 //at times this may give me a core dump.
const char* str = "test"; //This seems to be a valid construct.

}

Shouldn't there be a semicolon on line 4?

1: int main()
2: {
3: int* i;
4: *i = 1; //at times this may give me a core dump.
5: const char* str = "test"; //This seems to be a valid construct.
6:
7: }
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top