Thread: question about variables

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    33

    question about variables

    hi

    if we have this code

    char x[10];

    just this code nothing else, can we know what there might be inside the elements of the array?

    if you print an element for example printf("%c",x[5]) it will give something to the screen

    i ve heard it's independent but how can C just print this stuff? where does C get this stuff from?

    thanks in advance

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jackhasf View Post
    i ve heard it's independent but how can C just print this stuff? where does C get this stuff from?
    Because x is assigned 10 bytes of real physical memory. There cannot be "nothing" there, but you have not done anything with it, so it contains whatever it was used for last by some other program -- possibly -- or it is just in a random state.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    thanks MK27

    i have one more question

    what's the probability of this variable to contain a normal string of 3 characters?

    i mean if we would make printf("%s",x) what's the probability of this to get us a normal string that has 3 characters+1('\0') to the screen for example "abc"?

    is this even possible? just curious

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    printable/256 + printable/256 + printable/256 + 1/256

    Where printable is the total number of printable character values for your current character set.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If it's a global variable (outside of any functions), then all the elements will be initialized to '\0', so it will be an empty string.
    If it's a local variable, it could have any value.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by quzah View Post
    printable/256 + printable/256 + printable/256 + 1/256

    Where printable is the total number of printable character values for your current character set.


    Quzah.
    You mean

    printable/256 * printable/256 * printable/256 * 1/256

    where printable would usually be 255 (not including 0). For "abc" it would be 1/(256^4)

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by MK27 View Post
    Because x is assigned 10 bytes of real physical memory. There cannot be "nothing" there, but you have not done anything with it, so it contains whatever it was used for last by some other program -- possibly -- or it is just in a random state.
    Wouldn't be reading what was used for last by some other program kind of a security issue?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by C_ntua View Post
    where printable would usually be 255 (not including 0).
    There's a reason they have a function called isprint. For example: \b\b\b\0

    I always get probability wrong (probably because it's boring).


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by C_ntua View Post
    Wouldn't be reading what was used for last by some other program kind of a security issue?
    That's why good programs don't store things like plain text passwords in memory, they store them encrypted.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by cpjust View Post
    That's why good programs don't store things like plain text passwords in memory, they store them encrypted.
    Wouldn't it be simpler if the OS erased the stack after the program finished or crashed? As a safety measure lets say. Or is it kind of "not necessary" safety measurement. You could do it yourself if the program normally returns.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by C_ntua View Post
    Wouldn't it be simpler if the OS erased the stack after the program finished or crashed? As a safety measure lets say. Or is it kind of "not necessary" safety measurement. You could do it yourself if the program normally returns.
    People with root/Administrator access can force a memory dump whenever they want and then they'd be able to see anything that's not encrypted in the dump file. But yeah, I guess that might make things a tiny bit more secure.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  4. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  5. question about declaring global variables
    By Dag_ in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2005, 06:03 AM