How does a computer differentiate '' (null character) from “unsigned int = 0â€
Clash 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?
memory
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.
add a comment |Â
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?
memory
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
add a comment |Â
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?
memory
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
memory
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.
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
add a comment |Â
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
add a comment |Â
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.
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
add a comment |Â
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.
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered 1 hour ago
BrainStone
206220
206220
add a comment |Â
add a comment |Â
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.
Angelixus is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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