A question about integers, strings, and a bit more
Author |
Message |
HazySmoke)345
|
Posted: Sun Oct 31, 2010 9:53 am Post subject: A question about integers, strings, and a bit more |
|
|
So last night, I suddenly started to question everything I know about C programming, and I noticed that there were many gaping holes in my knowledge. I would really appreciate it if you can answer any/all of the following questions:
First, when you say something like this
code: | int x = 3;
x = x + 74;
|
Where is the 74 stored? (Don't say 'register', because I can easily add more constants so that I eventually run out of registers) Is it the stack? If it's stored on the stack, how is it deleted when the program finishes? When is it deleted?
Also, what's the difference between the next two lines?
code: | char str1[] = "ready aim fire";
char *str2 = "ready aim fire";
|
I presume that the string is COPIED into str1 when it's created. But what about str2? Is it also copied to some stack storage? If it is, then if I change str2 to point to something else later on in the program, does the original string "ready aim fire" get deleted? How is it deleted? When is it deleted? How does the program KNOW that it should be deleted, even though there is no pointer pointing to it?
If the string "ready aim fire" is NOT copied, then can I change the contents of this supposedly constant string?
Also, why doesn't this give compile errors?
code: | void foo(char *s) {}
int main(){
foo("direct hit!");
}
|
"direct hit!" is a CONSTANT string, but the parameter s is not constant. I tried to modify the contents of s, and the compiler STILL didn't complain. You'd think it should be something like this
code: | void foo(const char *s) {} //CONSTANT string s
int main(){
foo("direct hit!");
}
|
Alas, I don't have a good answer to any of those questions. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
jcollins1991
|
Posted: Sun Oct 31, 2010 11:26 am Post subject: Re: A question about integers, strings, and a bit more |
|
|
The 74 is just converted to binary in the compiled code, it isn't stored anywhere as a variable. The value of x itself may be stored in a register or on the stack, that all depends on how the compiler works and the size of the program. AFAIK any items on the stack are released for use by other parts of the program as soon as they go out of scope, nothing is really "deleted" which is why when you declare variables you should always initialize them so you know exactly whats being stored in the memory that your variables point to.
And to your last question, when you pass variables to a function a copy is made for use within the function, the original copy isn't affected at all by anything you do within the function. |
|
|
|
|
|
mirhagk
|
Posted: Tue Nov 02, 2010 9:54 pm Post subject: RE:A question about integers, strings, and a bit more |
|
|
but the last one is a pointer, so technically it'd have to create a variable somewhere in memory that had that string, and pass the pointer to the function. If that compiles then I imagine that's what it does. (which is interesting, so technically a constant could cause a stack overflow/out of memory if it did do that) |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Wed Nov 03, 2010 4:39 pm Post subject: Re: RE:A question about integers, strings, and a bit more |
|
|
mirhagk @ Tue Nov 02, 2010 9:54 pm wrote: but the last one is a pointer, so technically it'd have to create a variable somewhere in memory that had that string, and pass the pointer to the function. If that compiles then I imagine that's what it does. (which is interesting, so technically a constant could cause a stack overflow/out of memory if it did do that) The string literal is part of the image of the program that is loaded into memory. It is usually stored as read-only. It is possible to cause out-of-memory situations with constant data since that increases the amount of memory required for the program image. |
|
|
|
|
|
SmokeMonster
|
Posted: Sun Nov 14, 2010 9:14 am Post subject: Re: A question about integers, strings, and a bit more |
|
|
HazySmoke)345 @ Sun Oct 31, 2010 9:53 am wrote:
Also, what's the difference between the next two lines?
code: | char str1[] = "ready aim fire";
char *str2 = "ready aim fire";
|
I presume that the string is COPIED into str1 when it's created. But what about str2? Is it also copied to some stack storage? If it is, then if I change str2 to point to something else later on in the program, does the original string "ready aim fire" get deleted? How is it deleted? When is it deleted? How does the program KNOW that it should be deleted, even though there is no pointer pointing to it?
Yes, in case of str1 the string will be loaded into the block of memory for the str1 array. In case of str2; no it's not stored on the stack or the heap, C has a special read-only place in memory where it stores the such string literals. It doesn't get deleted and just stays there for the runtime of your program even if you have nothing pointing to it.
Quote:
Also, why doesn't this give compile errors?
code: | void foo(char *s) {}
int main(){
foo("direct hit!");
}
|
"direct hit!" is a CONSTANT string, but the parameter s is not constant. I tried to modify the contents of s, and the compiler STILL didn't complain. You'd think it should be something like this
code: | void foo(const char *s) {} //CONSTANT string s
int main(){
foo("direct hit!");
}
|
char * s is just a pointer, it can point to anything whether what it's point to is a constant or not. What do you mean by modifying the contents of s? s is just holding the address in memory of the value it is pointing to and that can be changed but you cannot change the contents of the value that s is pointing to.
for example
code: |
void foo(char *s)
{
s = "I'm a new string";
}
|
is valid since we are now just changing the pointer to new spot in memory, it will compile and run without errors
but
code: |
void foo(char *s)
{
*s = 'a';
//or
*(s+1) = 'b'
}
|
is invalid since now we are trying to change the contents of the value that s is pointing to which is a constant and cannot be changed. The compiler would allow this to compile but it will crash at runtime since you are trying to change the value of something that is read only!
Quote:
code: | void foo(const char *s) {} //CONSTANT string s
int main(){
foo("direct hit!");
}
|
This is the exact same thing as saying void foo(char * s) and will have the same effect as that code, the pointer doesn't care whether what it's pointing to is a constant or not since it's just storing the memory address, you don't have to explicity state that the string is a constant. |
|
|
|
|
|
|
|