C
Chris Mantoulidis
I see some really weird output from this program (compiled with GCC
3.3.2 under Linux).
#include <iostream>
using namespace std;
int main() {
char *s;
s = "test1";
cout << "s = " << s << " and &s = " << &s << "\n";
char *s2;
s2 = "foo bar";
cout << "s2 = " << s2 << " and &s2 = " << &s2 << "\n";
cout << "so s = " << s << "\n";
int *i;
*i = 1;
cout << "i = " << *i << " and &i " << &i << "\n"; //could have used
i instead of &i
int j;
j = 4;
cout << "j = " << j << " and &j " << &j << "\n";
return 0;
}
---
BTW: I know I can initialize on definition.
Output of the program is:
s = test1 and &s = 0xbfffe354
s2 = foo bar and &s2 = 0xbfffe350
so s = test1
Segmentation fault (what else, lol)
--
I wrote this program because i wanted to see what happens when I
assign values to pointers when I don't initialize them. If I comment
out the s2 part, then the program works (no segfault).
Why would s2 make this problem?
Oh, as I said, if I remove the s2 part it works fine and j's address
is 4bytes before i (the pointer). So the compiler allocates memory
with decreasing order? Or is this platform dependent?
Thanks in advance,
cmad
3.3.2 under Linux).
#include <iostream>
using namespace std;
int main() {
char *s;
s = "test1";
cout << "s = " << s << " and &s = " << &s << "\n";
char *s2;
s2 = "foo bar";
cout << "s2 = " << s2 << " and &s2 = " << &s2 << "\n";
cout << "so s = " << s << "\n";
int *i;
*i = 1;
cout << "i = " << *i << " and &i " << &i << "\n"; //could have used
i instead of &i
int j;
j = 4;
cout << "j = " << j << " and &j " << &j << "\n";
return 0;
}
---
BTW: I know I can initialize on definition.
Output of the program is:
s = test1 and &s = 0xbfffe354
s2 = foo bar and &s2 = 0xbfffe350
so s = test1
Segmentation fault (what else, lol)
--
I wrote this program because i wanted to see what happens when I
assign values to pointers when I don't initialize them. If I comment
out the s2 part, then the program works (no segfault).
Why would s2 make this problem?
Oh, as I said, if I remove the s2 part it works fine and j's address
is 4bytes before i (the pointer). So the compiler allocates memory
with decreasing order? Or is this platform dependent?
Thanks in advance,
cmad