pointer int/char ...

B

bvb

Hi,
while declaring/defining pointer to int/char, What is doing the
compiler exactly . i'm able to get output(String) for all pointer to
char definition(without line # 8&14). But when i include line #8 &
line #14 Its only printing lines 10 & 11 & immediatly its giving seg
fault. Please give suggestion to understand the internal things abt
declaration/definition of pointer varibles.

1#include<stdio.h>
2
3 int main()
4 {
5 char *a="linux";
6 char *b="linux";
7 char *c="linux";
8 int *i=4;
9
10 printf("\n a = %s",a);
11 printf("\n b = %s",b);
12 printf("\n c = %s",c);
13
14 printf("\n i=%d",*i);
15
16 return 0;
17 }

NOTE:
Also i'm getting some strange result when just doing pointer to
interger declaration. (Sometimes i'm able to assign/print pointer to
interger variable values EVEN WITHOUT ALLOCATIG MEMORY)
OS : Linux 2.4.20-8 i686 i686 i386 GNU/Linux
Compiler : gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

ThankX in Advance.......

Urs
Saravanan.....
 
J

Joona I Palaste

bvb said:
Hi,
while declaring/defining pointer to int/char, What is doing the
compiler exactly . i'm able to get output(String) for all pointer to
char definition(without line # 8&14). But when i include line #8 &
line #14 Its only printing lines 10 & 11 & immediatly its giving seg
fault. Please give suggestion to understand the internal things abt
declaration/definition of pointer varibles.

You are causing undefined behaviour, first by assigning an integer
value to a pointer, then by indirecting through that pointer. Your
implementation probably implements this by trying to access address 4
in the virtual memory table, which very likely is in a part of memory
that does not belong to your processs, therefore causing a
segmentation fault.
What is it exactly that you are trying to do? Perhaps you want this:
int i = 4;
int *ip = &i;
1#include<stdio.h>
2
3 int main()
4 {
5 char *a="linux";
6 char *b="linux";
7 char *c="linux";
8 int *i=4;
9
10 printf("\n a = %s",a);
11 printf("\n b = %s",b);
12 printf("\n c = %s",c);
13
14 printf("\n i=%d",*i);
15
16 return 0;
17 }
NOTE:
Also i'm getting some strange result when just doing pointer to
interger declaration. (Sometimes i'm able to assign/print pointer to
interger variable values EVEN WITHOUT ALLOCATIG MEMORY)

This is just sheer dumb luck. Undefined behaviour means that anything
can happen: your program might even appear to work.
OS : Linux 2.4.20-8 i686 i686 i386 GNU/Linux
Compiler : gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

This is not relevant to comp.lang.c.
ThankX in Advance.......
Urs
Saravanan.....

I didn't know you were a bear.
 
D

Dan Pop

In said:
Hi,
while declaring/defining pointer to int/char, What is doing the
compiler exactly . i'm able to get output(String) for all pointer to
char definition(without line # 8&14). But when i include line #8 &
line #14 Its only printing lines 10 & 11 & immediatly its giving seg
fault. Please give suggestion to understand the internal things abt
declaration/definition of pointer varibles.

1#include<stdio.h>
2
3 int main()
4 {
5 char *a="linux";
6 char *b="linux";
7 char *c="linux";
8 int *i=4;

Does your compiler silently accept the above line?!? I can't believe
that!
9
10 printf("\n a = %s",a);
11 printf("\n b = %s",b);
12 printf("\n c = %s",c);
13
14 printf("\n i=%d",*i);
15
16 return 0;
17 }

NOTE:
Also i'm getting some strange result when just doing pointer to
interger declaration. (Sometimes i'm able to assign/print pointer to
interger variable values EVEN WITHOUT ALLOCATIG MEMORY)
OS : Linux 2.4.20-8 i686 i686 i386 GNU/Linux
Compiler : gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

Then, your compiler is issuing the following diagnostic:

test.c:8: warning: initialization makes pointer from integer without a cast

You got exactly what you deserved for ignoring it. If the compiler is
telling you that your code is incorrect, what's the point in executing it,
anyway, without fixing it?

Anyway, the FAQ is explaining how to initialise pointers properly.

Dan
 
D

Dan Pop

In said:
This is not relevant to comp.lang.c.

When the OP has no clue about the source of the problem, it is a *good*
idea to mention such details. For all he knows, the code might be correct
but is hitting a bug in the implementation. And we have seen such cases
in the past, related to bugs in Microsoft compilers.

It is sheer stupidity to discourage people from providing such
information.

Dan
 
D

Darrell Grainger

Hi,
while declaring/defining pointer to int/char, What is doing the
compiler exactly . i'm able to get output(String) for all pointer to
char definition(without line # 8&14). But when i include line #8 &
line #14 Its only printing lines 10 & 11 & immediatly its giving seg
fault. Please give suggestion to understand the internal things abt
declaration/definition of pointer varibles.

1#include<stdio.h>
2
3 int main()
4 {
5 char *a="linux";
6 char *b="linux";
7 char *c="linux";

Line 7 the expression "linux" evaluates to the memory address that the
string "linux" will reside at. So if this string ends up at memory
location 1000, this is the same as:

char *c = (char *)1000;
or
char *c;
c = (char *)1000;
8 int *i=4;

Line 8 is the same as:

int *i;
i = 4;

Thus, you have set the pointer to reference memory address 4.
9
10 printf("\n a = %s",a);
11 printf("\n b = %s",b);
12 printf("\n c = %s",c);
13
14 printf("\n i=%d",*i);

This is telling the compiler to printf the integer at memory location 4.
That is, of i == 4 then *i is the integer at memory location 4. Most
likely, the operating system has sensitive information at memory location
4 so it is blocking your program from peeking at this memory location.
15
16 return 0;
17 }

NOTE:
Also i'm getting some strange result when just doing pointer to
interger declaration. (Sometimes i'm able to assign/print pointer to
interger variable values EVEN WITHOUT ALLOCATIG MEMORY)

This is called undefined behaviour. There are things in C where the
behaviour is undefined. This means that it could work on your computer but
not on might. It could work on Tuesdays at 4pm but never on Wednesdays at
3:17am. You just never know. Because of the uncertainty of undefined
behaviour you should avoid it at all costs.
 
D

Default User

Joona said:
You are causing undefined behaviour, first by assigning an integer
value to a pointer,

Chapter and verse on this, please. My understanding is that it's
implementation-defined.
then by indirecting through that pointer.

This is correct.




Brian Rodenborn
 
J

j

Darrell Grainger said:
Line 7 the expression "linux" evaluates to the memory address that the
string "linux" will reside at. So if this string ends up at memory
location 1000, this is the same as:

char *c = (char *)1000;
or
char *c;
c = (char *)1000;


Line 8 is the same as:

int *i;
i = 4;

You are mistaken. The value of ``i'' is initially indeterminate.
You then assign the integer ``4'' to ``i''.

int *i = 4;

Here, ``i'' has an initial value which is not indeterminate.

The two are not the same.
 
J

Jack Klein

Chapter and verse on this, please. My understanding is that it's
implementation-defined.

With a cast, the result is implementation-defined. As written by the
op, which I have replaced since you snipped it:
8 int *i=4;

....it is a constraint violation (6.5.4 paragraph 3 and 6.16.1
paragraph 1). So the compiler is required to emit a diagnostic.

Curiously, the standard neither forbids an implementation from
producing an executable in the presence of a constraint violation, so
long as it issues a diagnostic, nor does it state specifically that
executing a program that contained a constraint violation is undefined
behavior. But it surely is.
This is correct.

Indeed.
 
P

Peter Nilsson

Default User said:
Joona said:
[re: int *i=4;]

You are causing undefined behaviour, first by assigning an integer
value to a pointer,

Chapter and verse on this, please. My understanding is that it's
implementation-defined.

The assignment of an uncasted integer to a pointer violates the constraint 6.5.16.1p1.
[Whether it's undefined behaviour is apparently a separate issue.]

The conversion of an int to a pointer via cast is implementation-defined, but subject to
various problems...
This is correct.

Only if the pointer is a not suitably aligned, is a trap representation, or does not point
to an entity of the referenced type. [cf 6.3.2.3p5]

In the code above, there is no guarantee that the exceptions are excluded.
 
R

Richard Bos

Jack Klein said:
Curiously, the standard neither forbids an implementation from
producing an executable in the presence of a constraint violation, so
long as it issues a diagnostic, nor does it state specifically that
executing a program that contained a constraint violation is undefined
behavior.

Erm, yes, it does, in 4#2:

# 2 If a ‘‘shall’’ or ‘‘shall not’’ requirement that appears outside of
# a constraint is violated, the behavior is undefined.

Richard
 
C

Chris Torek

Erm, yes, it does, in 4#2:

# 2 If a 0x910x91shall0x920x92 or 0x910x91shall not0x920x92 requirement

[incidentally, please do something about these invalid characters,
if you can]
that appears outside of a constraint is violated, the behavior is undefined.

But Jack Klein is talking about a shall or shall-not phrase that
appears *inside* a constraint (and hence, as he noted, requires
a diagnostic).

I do not find it particularly curious (that "anything further,
deponent sayeth not") since the diagnostic gives the implementation
license to stop translation. If that implementation goes on and
produces an executable, and the user runs it, so what? The Standard
has required a diagnostic, and permitted the implementor to stop;
that the implementor and user have colluded to continue anyway is
their problem. :)
 
R

Richard Bos

Chris Torek said:
Erm, yes, it does, in 4#2:

# 2 If a 0x910x91shall0x920x92 or 0x910x91shall not0x920x92 requirement

[incidentally, please do something about these invalid characters,
if you can]

Hey, that's not Free Agent's usual behaviour... wonder what went wrong.
But Jack Klein is talking about a shall or shall-not phrase that
appears *inside* a constraint (and hence, as he noted, requires
a diagnostic).

Erm. I blame the turpentine (I'm painting my house).

Richard
 
D

Dan Pop

In said:
Curiously, the standard neither forbids an implementation from
producing an executable in the presence of a constraint violation, so
long as it issues a diagnostic, nor does it state specifically that
executing a program that contained a constraint violation is undefined
behavior. But it surely is.

The subsection ordering inside a section of the standard is not chosen
at random. The first section is dealing with syntax. Violate it
and the rest of the section no longer applies. The next sections
are constraints. Violate them and the semantic sections that follow
no longer apply.

So, it's undefined behaviour by lack of definition of behaviour, a
possibility explicitly allowed by the standard.

Dan
 
M

Mark McIntyre

# 2 If a 0x910x91shall0x920x92 or 0x910x91shall not0x920x92 requirement

[incidentally, please do something about these invalid characters,
if you can]

Hey, that's not Free Agent's usual behaviour... wonder what went wrong.[/QUOTE]

I didn't see them here until Chris quoted you so I siuspect its his server
rather than your client.
 
B

Ben Pfaff

Mark McIntyre said:
# 2 If a 0x910x91shall0x920x92 or 0x910x91shall not0x920x92 requirement

[incidentally, please do something about these invalid characters,
if you can]

Hey, that's not Free Agent's usual behaviour... wonder what went wrong.

I didn't see them here until Chris quoted you so I siuspect its his server
rather than your client. [/QUOTE]

If you're running Microsoft Windows you won't see them. This is
the Microsoft dumbquotes problem, in which '\x221' and '\x222'
get substituted for ` and ' respectively.
 
D

Dan Pop

In said:
Mark McIntyre said:
On Mon, 12 Jul 2004 11:46:00 GMT, in comp.lang.c ,
# 2 If a 0x910x91shall0x920x92 or 0x910x91shall not0x920x92 requirement

[incidentally, please do something about these invalid characters,
if you can]

Hey, that's not Free Agent's usual behaviour... wonder what went wrong.

I didn't see them here until Chris quoted you so I siuspect its his server
rather than your client.

If you're running Microsoft Windows you won't see them. This is
the Microsoft dumbquotes problem, in which '\x221' and '\x222'
get substituted for ` and ' respectively.

You must be talking about '\221' and '\222'. Not exactly the same
thing...

Dan
 
D

Dan Pop

In said:
Chris Torek said:
# 2 If a 0x910x91shall0x920x92 or 0x910x91shall not0x920x92 requirement

[incidentally, please do something about these invalid characters,
if you can]

Hey, that's not Free Agent's usual behaviour... wonder what went wrong.

I didn't see them here until Chris quoted you so I siuspect its his server
rather than your client.

Typical Mark McIntyre logic: if I don't see it, it does NOT exist.

I didn't see them either, but then, I didn't expect to: my news client
simply ignores arbitrary non-printable character codes. In the ISO 8859
8-bit extensions to ASCII, the range 0x80..0x9F is reserved for
additional control characters. As usual, Microsoft knows better.

Dan
 
M

Mark McIntyre

If you're running Microsoft Windows you won't see them. This is
the Microsoft dumbquotes problem, in which '\x221' and '\x222'
get substituted for ` and ' respectively.

I don't follow at all - there's nothing there either in the post or in teh
raw message, not \x221 or 0x91 or anything - but its OT so I'm not sure I
really care...
 
B

Ben Pfaff

Mark McIntyre said:
I don't follow at all - there's nothing there either in the post or in teh
raw message, not \x221 or 0x91 or anything - but its OT so I'm not sure I
really care...

Er, I mean \221 and \222 not the \x versions. But those
characters are definitely in article
<[email protected]> as it showed up at my
newsswerver.
 

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
474,145
Messages
2,570,824
Members
47,371
Latest member
Brkaa

Latest Threads

Top