linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* is it a buf or what?
@ 2002-06-08 10:33 Mohammed Khalid Ansari
  2002-06-08 12:03 ` Glynn Clements
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Mohammed Khalid Ansari @ 2002-06-08 10:33 UTC (permalink / raw)
  To: linux c programming mailing list


Hi,

I had written a small program as follows...

#include <stdio.h>

int main()
{
	char	*ptr;
	
	strcpy (ptr, "khalid");
	printf ("%s\n", ptr);
	return 0;
}

As you can see, I haven't allocated the space for the pointer ptr. When I 
ran the program, instead of giving me the segmentation fault, it just ran 
printing "khalid". Is it a bug of gcc or the kernel.

-- 

**************************************************************************

Mohammed Khalid Ansari                    Tel (res) : 0091-022-3051360
Assistant Manager II                          (off) : 0091-022-2024641
National Centre for Software Technology   Fax       : 0091-022-2049573 
8th flr,Air India Build. Nariman Point,   E-Mail    : khalid@ncst.ernet.in 	
Mumbai 400021.

Homepage : http://soochak.ncst.ernet.in/~khalid			  	  

**************************************************************************


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: is it a buf or what?
  2002-06-08 10:33 is it a buf or what? Mohammed Khalid Ansari
@ 2002-06-08 12:03 ` Glynn Clements
  2002-06-08 16:40 ` Elias Athanasopoulos
  2002-06-08 17:24 ` Marius Nita
  2 siblings, 0 replies; 4+ messages in thread
From: Glynn Clements @ 2002-06-08 12:03 UTC (permalink / raw)
  To: Mohammed Khalid Ansari; +Cc: linux c programming mailing list


Mohammed Khalid Ansari wrote:

> I had written a small program as follows...
> 
> #include <stdio.h>
> 
> int main()
> {
> 	char	*ptr;
> 	
> 	strcpy (ptr, "khalid");
> 	printf ("%s\n", ptr);
> 	return 0;
> }
> 
> As you can see, I haven't allocated the space for the pointer ptr. When I 
> ran the program, instead of giving me the segmentation fault, it just ran 
> printing "khalid". Is it a bug of gcc or the kernel.

No.

An automatic variables which isn't explicitly initialised will have a
random value. It seems that, in this case, "ptr" just happens to point
to writable memory.

This is an example of a program whose behaviour is "undefined". That
means that anything might happen; it might even work.

-- 
Glynn Clements <glynn.clements@virgin.net>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: is it a buf or what?
  2002-06-08 10:33 is it a buf or what? Mohammed Khalid Ansari
  2002-06-08 12:03 ` Glynn Clements
@ 2002-06-08 16:40 ` Elias Athanasopoulos
  2002-06-08 17:24 ` Marius Nita
  2 siblings, 0 replies; 4+ messages in thread
From: Elias Athanasopoulos @ 2002-06-08 16:40 UTC (permalink / raw)
  To: Mohammed Khalid Ansari; +Cc: linux c programming mailing list

On Sat, Jun 08, 2002 at 04:03:35PM +0530, Mohammed Khalid Ansari wrote:

> As you can see, I haven't allocated the space for the pointer ptr. When I 
> ran the program, instead of giving me the segmentation fault, it just ran 
> printing "khalid". Is it a bug of gcc or the kernel.

You are just lucky. If you run your program with efence or another mem
debugging tool, you'll get a segfault for sure.

Elias

-- 
http://gnewtellium.sourceforge.net			MP3 is not a crime.	

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: is it a buf or what?
  2002-06-08 10:33 is it a buf or what? Mohammed Khalid Ansari
  2002-06-08 12:03 ` Glynn Clements
  2002-06-08 16:40 ` Elias Athanasopoulos
@ 2002-06-08 17:24 ` Marius Nita
  2 siblings, 0 replies; 4+ messages in thread
From: Marius Nita @ 2002-06-08 17:24 UTC (permalink / raw)
  To: Mohammed Khalid Ansari; +Cc: linux-c-programming

On Sat, Jun 08, 2002 at 04:03:35PM +0530, Mohammed Khalid Ansari wrote:
> 
> Hi,
> 
> I had written a small program as follows...
> 
> #include <stdio.h>
> 
> int main()
> {
> 	char	*ptr;
> 	
> 	strcpy (ptr, "khalid");

Try this with a larger string, say 400 bytes. It will most likely segfault.
Since your string is small, there's a high probability that ptr will be
pointing to a small chunk of writable memory, in which your 7 bytes happen to
fit.

> 	printf ("%s\n", ptr);
> 	return 0;
> }
> 
> As you can see, I haven't allocated the space for the pointer ptr. When I 
> ran the program, instead of giving me the segmentation fault, it just ran 
> printing "khalid". Is it a bug of gcc or the kernel.

Neither. This is perfectly normal behavior.

> -- 
> 
> **************************************************************************
> 
> Mohammed Khalid Ansari                    Tel (res) : 0091-022-3051360
> Assistant Manager II                          (off) : 0091-022-2024641
> National Centre for Software Technology   Fax       : 0091-022-2049573 
> 8th flr,Air India Build. Nariman Point,   E-Mail    : khalid@ncst.ernet.in 	
> Mumbai 400021.
> 
> Homepage : http://soochak.ncst.ernet.in/~khalid			  	  
> 
> **************************************************************************
> 
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-06-08 17:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-06-08 10:33 is it a buf or what? Mohammed Khalid Ansari
2002-06-08 12:03 ` Glynn Clements
2002-06-08 16:40 ` Elias Athanasopoulos
2002-06-08 17:24 ` Marius Nita

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).