|
plowstar wrote: Except this, any better idea??
Nope. Variable-argument lists must have a terminator.
"Take only what you need and leave the land as you found it." - Native American Proverb
|
|
|
|
|
How do you interpret this:
while (*p++);
|
|
|
|
|
LABEL:
if(*p)
{
++p;
goto LABEL;
}
Think about it!
INTP
Every thing is relative...
|
|
|
|
|
I actually thought about it, and your interpretation doesnt seem to match what is working.
This piece of code is from:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/obtaining_a_file_name_from_a_file_handle.asp
where p initially points to a:
A:\\0C:\\0D:\\0\0
where \0 represents a single NULL character.
|
|
|
|
|
I am surprised I even looked at that code.
What I showed you was a litteral interpretation of the code you presented. I could even present it in assembly if required (which I will not do).
Let's break it down:
1) Copy the current value into a temporary variable.
2) Increment the current value.
3) Check if the temporary value has a value other than zero. If it does not then stop looping.
That is how it works!
INTP
Every thing is relative...
|
|
|
|
|
With your solution p will point to the first NULL from the beginning, which is not really correct. it will point to the char after the first null. try to trace it and see. but be careful with the string you use, or your program might crash. for eg, use "a\0bb\0ccc\0\0dddd"
I can understand, the code is very deceptive. only a few get it right the first time, and I am not one of them either
thanks!
|
|
|
|
|
Lets make it more accurate then:
343: char* p = "123";
0043CA51 mov dword ptr [ebp-14h],offset string "123" (004df014)
344: while( *p++ ) {};
0043CA58 mov eax,dword ptr [ebp-14h]
0043CA5B movsx ecx,byte ptr [eax]
0043CA5E mov edx,dword ptr [ebp-14h]
0043CA61 add edx,1
0043CA64 mov dword ptr [ebp-14h],edx
0043CA67 test ecx,ecx
0043CA69 je main+4Dh (0043ca6d)
0043CA6B jmp main+38h (0043ca58)
345: return 0;
0043CA6D xor eax,eax
0043CA6F jmp __tryend$_main$1+2Ch (0043cac0)
START_LOOP:
value_type = *p;
++p;
if( value_type == 0 )
goto END_LOOP;
goto START_LOOP;
END_LOOP:
INTP
Every thing is relative...
|
|
|
|
|
Looks like someone is trying to write hard to read code, that does nothing.
I believe that * gets called before ++, which would mean that a pointer is dereferened, and incremented, then the new value is returned. This code will run until that value is zero, and it's basically the same as
*p = 0;
but it will take a little longer.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
that someone is none other than a micrsoft sample code.
This piece of code is from:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/obtaining_a_file_name_from_a_file_handle.asp
where p initially points to a:
A:\\0C:\\0D:\\0\0
where \0 represents a single NULL character.
I dont understand what you maen by *p = 0, but this piece of code doesnt modify any data, only pointer advancement. and how that works is still mysterious to me.
|
|
|
|
|
It actually does the ++ first, which means that it increments the pointer until it points to NULL.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Christian Graus wrote: It actually does the ++ first, which means that it increments the pointer until it points to NULL.
Welcome to the gang! you are not alone
PS: check out other posts
thanks!
|
|
|
|
|
execution will take place from right to left
first the address store in p gets incremented then the derefrence of the pointer(new address stored in p) will take place.
Finally the condition will be checked by while loop.If it is non zero statements enclosed by while loop braces will be exceuted otherwise not.
|
|
|
|
|
first Compare value of *P & then inc P , *p++ used no *(++p)
Iman Ghasrfakhri
|
|
|
|
|
execution will take place from right to left
first the address store in p gets incremented then the derefrence of the pointer(new address stored in p) will take place.
Finally the condition will be checked by while loop.If it is non zero statements enclosed by while loop braces will be exceuted otherwise not.
There was a mistake in my previous post.I have rectified it
//first the derefrence of the pointer will take place and then according to that value conditon is checked
//check the condition in while
if(*p)
{
p++;
// statements inside while loop are executed
}
else //when while loop fails
{p++;}
//statements after while loop
-- modified at 6:12 Thursday 27th October, 2005
|
|
|
|
|
The statement searches for the first byte after a zero terminated string.
It's useful when you're parsing a multistring, e.g. when you have a CFileDialog and the user has selected several files.
The response comes in a string where the files are separated with a single NULL char and the file list end with a double NULL char.
The statement mentioned would in such case find the next file name, or point to the second NULL char at the end of the list.
--
Roger
It's supposed to be hard, otherwise anybody could do it!
|
|
|
|
|
If p initially points to a character string then after while(*p++); p points to the 0 character that terminates the string.
So it is semantically the same as
p = p + _tcslen(p);
|
|
|
|
|
Carsten Leue wrote: p = p + _tcslen(p);
Welcome to the gang! you too got it incorrect
it, actually, is equivalent to :
p= p + _tcslen(p) + 1
thanks!
|
|
|
|
|
It's a bummer! You are correct.
The loop ends if p points to the end of the string but p is still incremented.
Carsten
|
|
|
|
|
step 1. obtain value of object pointed to by "p"
step 2. increment "p"
step 3. if value evaluates to "true" then execute statement and continue with step 1
step 4. stop
|
|
|
|
|
equivalent code:
while (true)
{
bool done = *p == 0;
++p;
if (done) break;
}
|
|
|
|
|
Roger, Sunit, Ahz- you guys are awesome!
Does this example fit an obfuscated C contest?
thanks!
|
|
|
|
|
Lambu Jindu wrote: Does this example fit an obfuscated C contest?
no
|
|
|
|
|
please help if you can.
void i_make_an_object
{
object* object_ptr = new some_derived_object_class(some_parameter);
. . .
}
In the code above i have two things that need memory, the pointer object_ptr ,and the memory for the new object.
This things stored in registers,are they stored on stack(in the activation record i_make_an_object)?
dizzzzy
|
|
|
|
|
I'm not sure I understand the question. You're creating an object *, is this a managed C++ question, or vanilla C++ ?
In C++, when you call new, the memory for the new object is stored on the heap. The pointer itself, the variable which stores the memory address of this new object, is on the stack, I believe.
Here[^] is an article I found with google on the topic.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
can i ask you some questions that i have?
can i add two numbers and store the result without loading the operands into the cpu?
can a computer tell to the disk drive to move a standard block of bytes (say 512) into memory without the CPU handling each byte individually?
can i define 1024 registers?
dizzzzy
|
|
|
|