Advertisement
What's this do? I assume it crashes the app, if not the whole system, but knowing very little about memory architecture I could see it doing something weird like wipe my BIOS. So I'm afraid to compile it and find out. Oh, and tribe doesn't like my indents, sorry.
void main()
{
for (int x=0; 1; x++)
*x=0;
}
void main()
{
for (int x=0; 1; x++)
*x=0;
}
Advertisement
Advertisement
-
Re: silly pointer question
Wed, February 18, 2004 - 3:08 PMOr can I even use the reference operator with a variable not explicitly defined as a pointer? Obviously I've not yet done any serious C coding... -
-
Re: silly pointer question
Wed, February 18, 2004 - 3:40 PMYou can't use an int as a pointer. To work, it'd have to be something like:
for (int x=0; 1; x++)
{
char *loc = (char*)x;
*loc=0;
}
As to what it's do, depends on the operating system. Might crash or hang Win 95/98/Me but NT variants and UNIX/Linux variants wil protect OS memory. -
-
Re: silly pointer question
Wed, February 18, 2004 - 7:47 PMwhy wouldn't that give a syntax error or a trivial reassignment depending upon compiler? you are just dereferencing the memory location of variable "x" right?
and even if it doesn't, I could see:
&x = 0;
which would should take the memory space for "x" and set it to NULL while it should still be storing a value, and that I could see would give a segfault. Kind of curious, its stuff like this that makes C/C++ mystifying, so any input would be much appreciated. -
-
Re: silly pointer question
Wed, February 18, 2004 - 8:00 PMI got an illegal indirection error from Visual Studio, wouldn't even compile.
The "&x = 0" results in "left operand must be l-value" error. l-value refers to the left side of an equation. Not sure if that's ANSII C++ or a Microsoft improvisation.
Maybe because you're trying to alter the location of a real variable whose space was allocated by the compiler/loader and
not just change the pointer... -
-
Re: silly pointer question
Thu, February 19, 2004 - 12:12 AMwell, as I was walking home, I realized the real villain was :
for(int x=0; 1; x++)
didn't realize what that "1" would do for a little bit. Now I see more of the problem.
I think the l-value is ANSI, and it makes sense, you aren't allowed to reassign registers to fixed variables. So all is right with the world. still though, I am curious, why would :
for(int x=0; 1; i++)
*x=0;
cause any problem? aside from the infinite loop that would kick in that is? -
-
Re: silly pointer question
Thu, February 19, 2004 - 6:02 AM>for(int x=0; 1; i++)
> *x=0;
>
>cause any problem? aside from the infinite loop that would >kick in that is?
Because you can only use the indirection operator "*" on pointer data types. You can change the location that a pointer references but you can't change the location that a non-pointer references.
Don -
-
Re: silly pointer question
Thu, February 19, 2004 - 9:16 AMGah, and I thought I was starting to grok pointers.
At least my question turned out to be nontrivial... -
-
Re: silly pointer question
Thu, February 19, 2004 - 9:21 AMlook up Kernighan and Ritchie online, it should be free. if I remember correctly, they have some horrific examples of pointer expressions that if you can figure out help in making sense of them. -
-
This is the maximum depth. Additional responses will not be threaded.
Re: silly pointer question
Thu, February 19, 2004 - 9:25 AMAnother thing could is to take an Assembly Language Programming class. Getting down to the raw bits and bytes and working with different reference modes is a big help in groking what's going on.
And the debugger is your friend. -
-
Re: silly pointer question
Thu, February 19, 2004 - 9:37 AMBTW, I was curious whether a different error would be thrown at address 0x0 than, say, at 0x5000 because NT variants tend to use the first chunk (1024 or 2048 bytes, I think) of a process's virtual address space as kind of a canary in a coal mine: they're never allocated legitimately and ANY attempt to read/write there will be an error automatically.
But the error looks the same... -
-
Re: silly pointer question
Thu, February 19, 2004 - 10:18 AMThat was actually what I was getting at with my question, wondering what the memory address values actually correspond to, practically speaking. Whether the addresses are absolute for the machine or relative to each process, that sort of thing. (Though the pointer education is handy too, of course.) Now what's this, you're saying my OS uses an entire megabyte per process on nothing at all? Why is that? -
-
Re: silly pointer question
Thu, February 19, 2004 - 10:34 AMIt's OS specific. Not that familiar with UNIX but this link leads to info about how some Windows variants map out your virtual address space. Looks like it's the first 64k that you can't touch but that's virtual memory, not your machine's physical memory going to waste:
tinyurl.com/2wm3y
If you're interested, this one looks at how the loader and Windows run executables:
tinyurl.com/at4w
-
-
-
-
-
-
-
-
-
-
-