How does a computer differentiate '' (null character) from “unsigned int = 0”

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












If in a given situation, you have an array of chars (ending of course with the null character) and just after that, in the immediate next position in memory, you want to store 0 as an unsigned int, how does the computer differentiate between these 2?










share|improve this question









New contributor




Angelixus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • You're asking about typical computers about which the answers are completely right. However, there used to be some architectures which use tagged memory to distinguish between data types.
    – grawity
    15 mins ago















up vote
2
down vote

favorite












If in a given situation, you have an array of chars (ending of course with the null character) and just after that, in the immediate next position in memory, you want to store 0 as an unsigned int, how does the computer differentiate between these 2?










share|improve this question









New contributor




Angelixus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • You're asking about typical computers about which the answers are completely right. However, there used to be some architectures which use tagged memory to distinguish between data types.
    – grawity
    15 mins ago













up vote
2
down vote

favorite









up vote
2
down vote

favorite











If in a given situation, you have an array of chars (ending of course with the null character) and just after that, in the immediate next position in memory, you want to store 0 as an unsigned int, how does the computer differentiate between these 2?










share|improve this question









New contributor




Angelixus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











If in a given situation, you have an array of chars (ending of course with the null character) and just after that, in the immediate next position in memory, you want to store 0 as an unsigned int, how does the computer differentiate between these 2?







memory






share|improve this question









New contributor




Angelixus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Angelixus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 8 mins ago









Toto

3,00371024




3,00371024






New contributor




Angelixus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 hours ago









Angelixus

111




111




New contributor




Angelixus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Angelixus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Angelixus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • You're asking about typical computers about which the answers are completely right. However, there used to be some architectures which use tagged memory to distinguish between data types.
    – grawity
    15 mins ago

















  • You're asking about typical computers about which the answers are completely right. However, there used to be some architectures which use tagged memory to distinguish between data types.
    – grawity
    15 mins ago
















You're asking about typical computers about which the answers are completely right. However, there used to be some architectures which use tagged memory to distinguish between data types.
– grawity
15 mins ago





You're asking about typical computers about which the answers are completely right. However, there used to be some architectures which use tagged memory to distinguish between data types.
– grawity
15 mins ago











2 Answers
2






active

oldest

votes

















up vote
2
down vote













It doesn't.



The string terminator is a byte containing all 0 bits.



The unsigned int is two or four bytes (depending on your environment) each containing all 0 bits.



The two items are stored at different addresses. Your compiled code performs operations suitable for strings on the former location, and operations suitable for unsigned binary numbers on the latter. (Unless you have either a bug in your code, or some dangerously clever code!)



But all of these bytes look the same to the CPU. Data in memory doesn't have any type associated with it. That's an abstraction that exists only in the source code and means something only to the compiler.






share|improve this answer
















  • 1




    That's why developers have to be careful with strings. If you have, say, 100 consecutive bytes, you can fit at most 99 1-byte characters in there plus the terminator in last byte. If you write a 100-byte string in there, program won't be able to figure out that the string ends there and will continue reading consecutive bytes until a coincidental zero byte. If the string is more than 100 bytes long, it will overwrite some adjacent data. High-level programming languages (Java, C#, JS etc.) take care of this themselves, but in low-level langs such as C, C++, assembly it's dev's responsobility.
    – gronostaj
    1 hour ago

















up vote
2
down vote













In short there is no difference (except that an int is 2 or 4 bytes wide and a char just 1).



The thing is that all modern libaries either use the null terminator technique or store the length of a string. And in both cases the program/computer knows it reached the end of a string when it either read a null character or it has read as many characters as the size tells it to.



Issues with this start when the null terminator is missing or the length is wrong as then the program starts reading from memory it isn't supposed to.






share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "3"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    Angelixus is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1362864%2fhow-does-a-computer-differentiate-0-null-character-from-unsigned-int-0%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    It doesn't.



    The string terminator is a byte containing all 0 bits.



    The unsigned int is two or four bytes (depending on your environment) each containing all 0 bits.



    The two items are stored at different addresses. Your compiled code performs operations suitable for strings on the former location, and operations suitable for unsigned binary numbers on the latter. (Unless you have either a bug in your code, or some dangerously clever code!)



    But all of these bytes look the same to the CPU. Data in memory doesn't have any type associated with it. That's an abstraction that exists only in the source code and means something only to the compiler.






    share|improve this answer
















    • 1




      That's why developers have to be careful with strings. If you have, say, 100 consecutive bytes, you can fit at most 99 1-byte characters in there plus the terminator in last byte. If you write a 100-byte string in there, program won't be able to figure out that the string ends there and will continue reading consecutive bytes until a coincidental zero byte. If the string is more than 100 bytes long, it will overwrite some adjacent data. High-level programming languages (Java, C#, JS etc.) take care of this themselves, but in low-level langs such as C, C++, assembly it's dev's responsobility.
      – gronostaj
      1 hour ago














    up vote
    2
    down vote













    It doesn't.



    The string terminator is a byte containing all 0 bits.



    The unsigned int is two or four bytes (depending on your environment) each containing all 0 bits.



    The two items are stored at different addresses. Your compiled code performs operations suitable for strings on the former location, and operations suitable for unsigned binary numbers on the latter. (Unless you have either a bug in your code, or some dangerously clever code!)



    But all of these bytes look the same to the CPU. Data in memory doesn't have any type associated with it. That's an abstraction that exists only in the source code and means something only to the compiler.






    share|improve this answer
















    • 1




      That's why developers have to be careful with strings. If you have, say, 100 consecutive bytes, you can fit at most 99 1-byte characters in there plus the terminator in last byte. If you write a 100-byte string in there, program won't be able to figure out that the string ends there and will continue reading consecutive bytes until a coincidental zero byte. If the string is more than 100 bytes long, it will overwrite some adjacent data. High-level programming languages (Java, C#, JS etc.) take care of this themselves, but in low-level langs such as C, C++, assembly it's dev's responsobility.
      – gronostaj
      1 hour ago












    up vote
    2
    down vote










    up vote
    2
    down vote









    It doesn't.



    The string terminator is a byte containing all 0 bits.



    The unsigned int is two or four bytes (depending on your environment) each containing all 0 bits.



    The two items are stored at different addresses. Your compiled code performs operations suitable for strings on the former location, and operations suitable for unsigned binary numbers on the latter. (Unless you have either a bug in your code, or some dangerously clever code!)



    But all of these bytes look the same to the CPU. Data in memory doesn't have any type associated with it. That's an abstraction that exists only in the source code and means something only to the compiler.






    share|improve this answer












    It doesn't.



    The string terminator is a byte containing all 0 bits.



    The unsigned int is two or four bytes (depending on your environment) each containing all 0 bits.



    The two items are stored at different addresses. Your compiled code performs operations suitable for strings on the former location, and operations suitable for unsigned binary numbers on the latter. (Unless you have either a bug in your code, or some dangerously clever code!)



    But all of these bytes look the same to the CPU. Data in memory doesn't have any type associated with it. That's an abstraction that exists only in the source code and means something only to the compiler.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 2 hours ago









    Jamie Hanrahan

    15.8k33573




    15.8k33573







    • 1




      That's why developers have to be careful with strings. If you have, say, 100 consecutive bytes, you can fit at most 99 1-byte characters in there plus the terminator in last byte. If you write a 100-byte string in there, program won't be able to figure out that the string ends there and will continue reading consecutive bytes until a coincidental zero byte. If the string is more than 100 bytes long, it will overwrite some adjacent data. High-level programming languages (Java, C#, JS etc.) take care of this themselves, but in low-level langs such as C, C++, assembly it's dev's responsobility.
      – gronostaj
      1 hour ago












    • 1




      That's why developers have to be careful with strings. If you have, say, 100 consecutive bytes, you can fit at most 99 1-byte characters in there plus the terminator in last byte. If you write a 100-byte string in there, program won't be able to figure out that the string ends there and will continue reading consecutive bytes until a coincidental zero byte. If the string is more than 100 bytes long, it will overwrite some adjacent data. High-level programming languages (Java, C#, JS etc.) take care of this themselves, but in low-level langs such as C, C++, assembly it's dev's responsobility.
      – gronostaj
      1 hour ago







    1




    1




    That's why developers have to be careful with strings. If you have, say, 100 consecutive bytes, you can fit at most 99 1-byte characters in there plus the terminator in last byte. If you write a 100-byte string in there, program won't be able to figure out that the string ends there and will continue reading consecutive bytes until a coincidental zero byte. If the string is more than 100 bytes long, it will overwrite some adjacent data. High-level programming languages (Java, C#, JS etc.) take care of this themselves, but in low-level langs such as C, C++, assembly it's dev's responsobility.
    – gronostaj
    1 hour ago




    That's why developers have to be careful with strings. If you have, say, 100 consecutive bytes, you can fit at most 99 1-byte characters in there plus the terminator in last byte. If you write a 100-byte string in there, program won't be able to figure out that the string ends there and will continue reading consecutive bytes until a coincidental zero byte. If the string is more than 100 bytes long, it will overwrite some adjacent data. High-level programming languages (Java, C#, JS etc.) take care of this themselves, but in low-level langs such as C, C++, assembly it's dev's responsobility.
    – gronostaj
    1 hour ago












    up vote
    2
    down vote













    In short there is no difference (except that an int is 2 or 4 bytes wide and a char just 1).



    The thing is that all modern libaries either use the null terminator technique or store the length of a string. And in both cases the program/computer knows it reached the end of a string when it either read a null character or it has read as many characters as the size tells it to.



    Issues with this start when the null terminator is missing or the length is wrong as then the program starts reading from memory it isn't supposed to.






    share|improve this answer
























      up vote
      2
      down vote













      In short there is no difference (except that an int is 2 or 4 bytes wide and a char just 1).



      The thing is that all modern libaries either use the null terminator technique or store the length of a string. And in both cases the program/computer knows it reached the end of a string when it either read a null character or it has read as many characters as the size tells it to.



      Issues with this start when the null terminator is missing or the length is wrong as then the program starts reading from memory it isn't supposed to.






      share|improve this answer






















        up vote
        2
        down vote










        up vote
        2
        down vote









        In short there is no difference (except that an int is 2 or 4 bytes wide and a char just 1).



        The thing is that all modern libaries either use the null terminator technique or store the length of a string. And in both cases the program/computer knows it reached the end of a string when it either read a null character or it has read as many characters as the size tells it to.



        Issues with this start when the null terminator is missing or the length is wrong as then the program starts reading from memory it isn't supposed to.






        share|improve this answer












        In short there is no difference (except that an int is 2 or 4 bytes wide and a char just 1).



        The thing is that all modern libaries either use the null terminator technique or store the length of a string. And in both cases the program/computer knows it reached the end of a string when it either read a null character or it has read as many characters as the size tells it to.



        Issues with this start when the null terminator is missing or the length is wrong as then the program starts reading from memory it isn't supposed to.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 1 hour ago









        BrainStone

        206220




        206220




















            Angelixus is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            Angelixus is a new contributor. Be nice, and check out our Code of Conduct.












            Angelixus is a new contributor. Be nice, and check out our Code of Conduct.











            Angelixus is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1362864%2fhow-does-a-computer-differentiate-0-null-character-from-unsigned-int-0%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery