• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

simple c programing doubt

 
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic