473,788 Members | 2,706 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pointer int/char ...

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(with out 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.....
Nov 14 '05 #1
22 2700
bvb <bm******@faste m.com> scribbled the following:
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(with out 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.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"How come even in my fantasies everyone is a jerk?"
- Daria Morgendorfer
Nov 14 '05 #2
In <d5************ **************@ posting.google. com> bm******@fastem .com (bvb) writes:
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(with out 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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #3
In <cc**********@o ravannahka.hels inki.fi> Joona I Palaste <pa*****@cc.hel sinki.fi> writes:
bvb <bm******@faste m.com> scribbled the following:
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.


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #4
On Fri, 8 Jul 2004, bvb wrote:
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(with out 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.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@ whitehouse.gov
Nov 14 '05 #5
bvb <bm******@faste m.com> spoke thus:

<mode="nitpick" >
3 int main() int main( void ) /* better */ 4 {
5 char *a="linux"; const char *a="linux"; /* a good idea */ 6 char *b="linux"; /* ditto */ 7 char *c="linux"; /* ditto */ 8 int *i=4;
13
14 printf("\n i=%d",*i); /* some implementations require a newline at the end
of output, no reason not to include one */ 15
16 return 0;
17 }


--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #6
Joona I Palaste wrote:
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
Nov 14 '05 #7
j

"Darrell Grainger" <da*****@NOMORE SPAMcs.utoronto .ca.com> wrote in message
news:Pi******** *************** ********@drj.pf ...
On Fri, 8 Jul 2004, bvb wrote:
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(with out 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;


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.

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.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@ whitehouse.gov

Nov 14 '05 #8
On Fri, 9 Jul 2004 16:21:18 GMT, Default User
<fi********@boe ing.com.invalid > wrote in comp.lang.c:
Joona I Palaste wrote:
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.


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.
then by indirecting through that pointer.


This is correct.


Indeed.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #9
"Default User" <fi********@boe ing.com.invalid > wrote in message
news:40******** *******@boeing. com.invalid...
Joona I Palaste wrote:

[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...
then by indirecting through that pointer.


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.

--
Peter
Nov 14 '05 #10

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

Similar topics

10
2067
by: Chris Mantoulidis | last post by:
I see some really weird output from this program (compiled with GCC 3.3.2 under Linux). #include <iostream> using namespace std; int main() { char *s; s = "test1"; cout << "s = " << s << " and &s = " << &s << "\n";
4
16461
by: Bryan Parkoff | last post by:
I want to allocate pointer array into memory so pointer array contains ten pointers. It would be 4 bytes per pointer to be total 40 bytes. Looks like below for example. unsigned char* A = new unsigned char ; It has only one pointer contains 1,000 bytes. How can I do this to create pointer list like below. unsigned char** B = new (unsigned char*) ; // Pointer List contains
35
2906
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL */ memset(TextureImage,0,sizeof(void *)*1); /* Line 2*/ According to my knowledge in the first line
16
2310
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the subject pointer does not refer to an object suitably aligned in storage. It is guaranteed that a pointer to an object may be converted to a pointer to an object whose type requires less or equally strict storage alignment and back again without change;...
204
13122
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
5
5347
by: max | last post by:
Dear all, I did the following analysis to conclude that the following pointer types are not compatible. Please let me know If my analysis and interpretation of the C standard are correct: const char * : "pointer to const-qualified char". char *: "pointer to char". Are these pointed-to types compatibles?
15
3775
by: khan | last post by:
Hi, I read that pointer representation can non-zero bit pattern, machine specific.Compiler when comes accross value '0' in pointer context, converts it to machine specific null pointer bit-pattern. My question is if a program refers to that specific value which is used by the machine to refer to null pointer, how compiler treats that?.
5
1664
by: mdh | last post by:
Hi all, I have gone through the FAQ and done searches in the Comp.Lang and if I have missed it please let me have the ref. (Question generated in part by p119). Given char a;
10
3446
by: Ahmad Humayun | last post by:
Whats the difference between: char str1 = "wxyz"; char* str2 = "abcd"; I can do this: str2 = str1 but I can't do this: str1 = str2
8
1866
by: tfelb | last post by:
Hey group! I have 2 questions. I saw functions with char *dst = (char *)src. In that case if I remember what I've learned I assign (an) (the) address of src to dst. Right? But I can assign an address with the address operator & too? char *dst = &src. What's the difference between *dst = (char *)src and *dst = &src and
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10370
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10113
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7519
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5402
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5538
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.