aspose file tools*
The moose likes C / C++ and the fly likes simple c programing doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » C / C++
Bookmark "simple c programing doubt" Watch "simple c programing doubt" New topic
Author

simple c programing doubt

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

i knnow this is a core java program but there are some slly doubts of c really bugging me so please tell me why is it happening



now when i try to type more then 10 charecters it displays more then 10 charecters why so??
I was expecting only 10 charecters to be displayed as array size is 10


http://www.lifesbizzare.blogspot.com || OCJP:81%
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11398
    
  16

Because C does not enforce/check your array size. you get 10 bytes of memory for your array, sure, but when you write, it writes until it hits the \0 character. So, when you type more than 10 characters, you are over-writing other memory that isn't yours. This is a common way to cause core dumps in C - you overwrite memory you're not supposed to, which hoses the program.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39384
    
  28
Before I move this to a non-Java forum: I tried it and got this:
campbell@xxxxxxx-laptop:~/CPrograms$ Name
Enter name: Campbell Ritchie

my name is Campbellcampbell@xxxxxxx-laptop:~/CPrograms$ Name
Enter name: Campbell_Ritchie

*** stack smashing detected ***: Name terminated
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(__fortify_fail+0x48)[0x704de8]
/lib/tls/i686/cmov/libc.so.6(__fortify_fail+0x0)[0x704da0]
Name[0x80484f2]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x63ab56]
Name[0x8048401]
======= Memory map: ========
001f0000-001f1000 r-xp 00000000 00:00 0 [vdso]
00624000-00762000 r-xp 00000000 08:07 426522 /lib/tls/i686/cmov/libc-2.10.1.so
00762000-00764000 r--p 0013e000 08:07 426522 /lib/tls/i686/cmov/libc-2.10.1.so
00764000-00765000 rw-p 00140000 08:07 426522 /lib/tls/i686/cmov/libc-2.10.1.so
00765000-00768000 rw-p 00000000 00:00 0
00887000-008a3000 r-xp 00000000 08:07 408806 /lib/libgcc_s.so.1
008a3000-008a4000 r--p 0001b000 08:07 408806 /lib/libgcc_s.so.1
008a4000-008a5000 rw-p 0001c000 08:07 408806 /lib/libgcc_s.so.1
008c9000-008e4000 r-xp 00000000 08:07 411310 /lib/ld-2.10.1.so
008e4000-008e5000 r--p 0001a000 08:07 411310 /lib/ld-2.10.1.so
008e5000-008e6000 rw-p 0001b000 08:07 411310 /lib/ld-2.10.1.so
08048000-08049000 r-xp 00000000 08:06 4146698 /home/campbell/CPrograms/Name
08049000-0804a000 r--p 00000000 08:06 4146698 /home/campbell/CPrograms/Name
0804a000-0804b000 rw-p 00001000 08:06 4146698 /home/campbell/CPrograms/Name
09fdf000-0a000000 rw-p 00000000 00:00 0 [heap]
b7797000-b7798000 rw-p 00000000 00:00 0
b77a8000-b77ac000 rw-p 00000000 00:00 0
bfa56000-bfa6b000 rw-p 00000000 00:00 0 [stack]
my name is Campbell_RitchieAborted
campbell@xxxxxxx-laptop:~/CPrograms$
I had to delete the <conio.h> include directive.
This was on Ubuntu 9.10 with a bash shell.
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

unbuntu???

now then how to input a string then how did c programmers used to create programs prior to discovery of core java then??
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11398
    
  16

ummm...they way you did it. They just had to be careful to define a large enough buffer to handle the expected input.

You could do things like create one large buffer you use for all your inputs, then manually verify it... i.e.

char buffer[1000];

//read name into buffer
//copy first 9 chars into Name string, and set 10th to null
//read address into buffer
//copy first 29 chars into buffer and set 30th to null
//read city into buffer
//copy first...

A common way to fix bugs in C code was to simply double all your declarations, on the assumption that your probably overwriting memory somewhere, so by doubling everything, you will now be less likely to blow away memory that isn't yours.
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055



why aint it dispalying the proper out despite oweriting memory???
and sometimes looping duch programss just to input multiple name creates a strange out put
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39384
    
  28
You are trying to write the memory into the location described by the contents of the 24th element of that char[] array. That is likely to be a bit of memory far from the location of your program, which may give a seg fault. Try name + 23 instead.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: simple c programing doubt