Results 1 to 8 of 8

Thread: First Question

  1. #1
    Join Date
    Dec 2007
    Posts
    198

    First Question

    I just got done with the Structures VTM which I liked a lot. But I was left a bit unclear on something about pointers and strings.

    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    
    struct monstersRat
    {	
    	int HP;
    	int ATK;
    	char *name; //Why is a pointer necessary here?  
    };                        //I came from VB and never had to use pointers
                               //Was this done automatically?
    
    
    
    int main()
    {
    	monstersRat sRat;
    	monstersRat gRat;
    	
    
    	sRat.name = "Small Rat";
    	sRat.HP = 5;
    	sRat.ATK = 1;
    
    	gRat.name = "Giant Rat";
    	gRat.HP = 15;
    	gRat.ATK = 3;
    	
    	cout << sRat.name << endl;
    	cout << sRat.HP<<endl ;
    	cout << sRat.ATK<<endl ;
    
    	cout <<endl<<endl<<endl;
    
    	cout <<gRat.name <<endl;
    	cout <<gRat.HP <<endl;
    	cout <<gRat.ATK <<endl;
    
    
    
    
    }

    Hypothetically lets say this is Everquest or WOW. Would these monsters be loaded a) when you entered the Zone? Or b) would all of the monsters be loaded when you entered the game?

    Also, should I be deleting the .names immediately or would it be when the player left the zone, or left the game? In this case do I even have to delete the .names since I'm not using an input function?

    Thanks for any help

  2. #2
    Join Date
    Mar 2008
    Posts
    394
    What do you mean by loaded exactly?

    And a pointer is necessary there to make a string. char name; is a single character, char *name; is a pointer to the start of a character string (with unspecified length).

    I guess you can delete the names whenever you're sure you won't need them again, but if it's a small program you can just do it all at the end of the program.

    Someone else might have a better answer, but it's a start! :P

  3. #3
    Join Date
    Dec 2007
    Posts
    198
    Hey thanks for the response.

    I think your question answered mine haha
    Probably getting ahead of myself but...

    You enter the level and all the structs have already been compiled and registered even though you haven't entered the rest of the levels yet. If that doesn't make sense it really isn't that big of a deal now.

    And that actually helped a lot with the string pointer thanks.

  4. #4
    Join Date
    Sep 2008
    Posts
    21
    The character information, buildings, terrain, triggers, etc are all loaded either at the beginning of the instance (loading level) or are loaded dynamically as you approach areas of the map (harder). It is a more complex process than what you are doing right now. Normally there is a wrapper that allows map data etc to be programmed in a scripting language. The engine will handle all the add/remove parts to/from memory. I'm not sure if EQ and WOW do it exactly this way, I have some experience in the different Torque engines, and the A6 engine and they do it this way.

  5. #5
    Join Date
    Dec 2007
    Posts
    198
    Ahh thank you sir!

  6. #6
    Join Date
    Jun 2003
    Location
    Trier, Germany
    Posts
    1,350
    to clarify the issue with strings a little more:
    vb manages to hide away some of the technicalities of programming. this is a good thing, since you generally don't want to worry about how a string is handled by the computer on the lowest level. still, it's a good a idea to step back from time to time and take a look at how things actually work down there.

    now, the main difference between strings and simple datatypes (as integers, floating point numbers or bool) is that the latter ones all have distinct ranges. a single-precision float always needs 32 bits of memory, a long int might take 64 bits and a single unsigned char 8 bits. with strings, you don't know how much memory they will occupy. you could store a single letter as well as the whole bible in a string. therefore, a string is usually implemented as a more complex object, storing not only the data itself, but also information about where the data is saved and how much memory it occupies (i.e. how long the string is).

    this is the way you'ld want it to be in most cases. unless you are working in an area where you cannot bother constructing such a complex string. think of embedded systems for cars or a device driver in an os-kernel. maybe constructing such complex string objects causes an unacceptable overhead for your application and you just need something more lightweight. c-strings entering the stage...

    the c-style char* strings are the most simple type of string. the pointer points to the beginning of the string data, but does not carry any additional information as we had for the more complex (vb-)strings. the rule is simply, everything from the pointer on belongs to the string, until you encounter a byte that's set to 0 ('\0' in c). of course, we lost a *lot* of comfort here, but well, we managed to store a string with a total overhead of only 40 bits (32 bits for the pointer and 8 bits for the terminating '\0').

    strings are a fascinating issue and you would be amazed on what neat tricks people come up with to handle them efficiently. if you have some spare time, try to implement some of the functionalities that vb-strings offer out of the box using spare c-strings. this is not only an excellent training, but also gives some insight on how strings really work...

  7. #7
    Join Date
    Dec 2007
    Posts
    198
    hey comic, thanks for that.

    Why did we lose a lot of comfort after using c-style *char? If I had to guess (using the definition "everything from the pointer on until \0") does it have to do with memory that is allocated improperly?

  8. #8
    Join Date
    Jun 2003
    Location
    Trier, Germany
    Posts
    1,350
    exactly. if we forget the closing '\0' or overwrite it by accident, we're screwed. not quite as bad but still annoying: if the string contains embedded '\0's.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •