Sign in to follow this  

Advanced c++ question

This topic is 3660 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello: I am working on a engine and I encountered these in the source code: if (const char *slash = dStrchr(this->name, '/')) { char root[512]; dStrncpy(root, this->name, slash-this->name); root[slash-this->name] = 0; mRoot = StringTable->insert(root); } What does slash-this->name mean? It seems very wierd but it compiles. Second, void CodeBlock::clearAllBreaks() { if(!lineBreakPairs) return; for(U32 i = 0; i < lineBreakPairCount; i++) { U32 *p = lineBreakPairs + i * 2; code[p[1]] = p[0] & 0xFF; } } Does P[1] points to the next 32 bits after *p ? And anyone can guess what line break pairs mean? Here code block is simply a block of instructions for a virtual machine. Help will be really appraciated!!!!!!

Share this post


Link to post
Share on other sites
Quote:
Original post by ynkm169
Hello:

I am working on the Torque engine and I encountered these in the source code:

if (const char *slash = dStrchr(this->name, '/'))
{
char root[512];
dStrncpy(root, this->name, slash-this->name);
root[slash-this->name] = 0;
mRoot = StringTable->insert(root);
}

What does slash-this->name mean? It seems very wierd but it compiles.


.... Ewwwwwwwwwwwwwwwwwwwww. Oh well, might as well give you an education.

It's simply (slash) - (this->name).

'slash' is the const char* defined above, i.e. the result of the dStrchr() call.
'this->name' is the 'name' member of the current object (i.e. the code is in some member function; we are accessing the data of the object upon which the function was called). Since it compiles, 'this->name' must be either an integer (such that a pointer - one that is "offset" from 'slash' - is returned), or another char* (such that an integer - the "distance" between the two pointers) is returned. Based on the names, it would appear to be the latter.

What we are doing, therefore, is finding the '/' within the name using dStrchr(), and then the distance between the beginning of the name and the slash - i.e. the length of the text before the slash. The dStrncpy function then copies that many characters (starting at the beginning of the name) into the buffer. The resulting text is null-terminated, and fed to the string table.

(I have no idea why they're using a buffer. It would be simpler - assuming you can call any of this arcane hackery "simple" - to put the null terminator directly into the 'name' (replacing the slash), feed to the string table, and then re-write the slash.

Please don't ever think of writing things like this yourself. C++ has a real string type, called std::string. It is your friend and wants to make your life easier. This is an offer you really can't refuse.

Quote:

Second,

void CodeBlock::clearAllBreaks()
{
if(!lineBreakPairs)
return;
for(U32 i = 0; i < lineBreakPairCount; i++)
{
U32 *p = lineBreakPairs + i * 2;
code[p[1]] = p[0] & 0xFF;
}
}
Does P[1] points to the next 32 bits after *p ?


p[1] *is* the next U32-sized chunk of memory after *p. 'p' points somewhere; '*p' is the pointed-at thing, which is identical to 'p[0]'. 'p[1]' is identical to '*(p+1)', and is the thing next to the pointed-at thing.

Quote:

And anyone can guess what line break pairs mean? Here code block is simply a block of instructions for a virtual machine.


It's a variable. I may know a good thing or two about coding but I'm not psychic (well, not that psychic). Go find the declaration (and/or the rest of the code which uses it).

Share this post


Link to post
Share on other sites

This topic is 3660 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this