Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ SMAUG ➜ SMAUG coding ➜ Where should I declare an instance of a struct?

Where should I declare an instance of a struct?

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Golg   (8 posts)  Bio
Date Sat 05 Mar 2005 10:54 PM (UTC)
Message
I created a new struct in mud.h to hold globally available data that should be the same for all characters, so it must be outside of pc_data. Upon MUD startup, an instance of it should be declared and memory allocated for it.

The problem is, I'm not sure where I should declare it, and I don't seem to be able to find where other structs are created. If you could help me, I'd really appreciate it...this whole thing is giving me a headache.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Sat 05 Mar 2005 11:09 PM (UTC)
Message
Do you intend for this data to be specific to each character, or do you intend it to be global across the whole MUD?

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Golg   (8 posts)  Bio
Date Reply #2 on Sat 05 Mar 2005 11:39 PM (UTC)
Message
Global across the entire MUD...I know how to put things in pc_data or char_data already.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #3 on Sat 05 Mar 2005 11:43 PM (UTC)
Message
OK. You'll want to create a global variable of that type, which you can do in comm.c for example. Look at the declarations for other global variables, such as first_char, and make one like that. You don't need to make it a pointer, you can just declare one on the stack, I suppose. Then, in mud.h, you'll want the same thing with extern in front - that means that it's not a declaration per se but a sort of forward declaration, meaning: "this symbol exists, but is somewhere else".

Let me know if that made sense. :)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Golg   (8 posts)  Bio
Date Reply #4 on Sat 05 Mar 2005 11:45 PM (UTC)
Message
Yes, that makes sense, thanks :P
Top

Posted by Golg   (8 posts)  Bio
Date Reply #5 on Sun 06 Mar 2005 12:38 AM (UTC)
Message
Oh yeah. I also want a variable in each instance of the struct to be initialized when the MUD is started, and I need to allocate memory for the pointer to the struct...do you know where I should do these two things?

Thanks.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #6 on Sun 06 Mar 2005 05:06 AM (UTC)
Message
Quote:
each instance of the struct
I thought the whole point was to have just one instance of the struct, and have it be global...
Quote:
allocate memory for the pointer to the struct
Inside db.c, in one of the load functions, most likely. Or, I suppose, inside the main function. It depends on what you want to do with your struct and what information it needs to have lying around.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Golg   (8 posts)  Bio
Date Reply #7 on Sun 06 Mar 2005 05:49 AM (UTC)
Message
I'll have a struct array that'll be accessed from a new function in act_info.c and act_comm.c. It'll be for a channel history command, and it will hold a double pointer (I'm not sure what the term is...char **history) to hold the messages and an int holding the index representing the last message gotten. It will only hold something around 10 messages, so the messages will be recycled.
Top

Posted by Greven   Canada  (835 posts)  Bio
Date Reply #8 on Sun 06 Mar 2005 06:45 AM (UTC)
Message
You could fairly easily incorporate that into sys_data and have it created and initialized with the rest of sys_data.

Nobody ever expects the spanish inquisition!

darkwarriors.net:4848
http://darkwarriors.net
Top

Posted by Golg   (8 posts)  Bio
Date Reply #9 on Sun 06 Mar 2005 07:03 AM (UTC)
Message
Oh, thanks for pointing out system_data. I didn't realize that it existed. Now, I need to initialize index to 0 and allocate memory for everything when the mud starts up...where's the best place for that?
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #10 on Sun 06 Mar 2005 08:27 AM (UTC)
Message
If you're putting it into sys_data, find out where the rest of sys_data is initialized and do it in the same place.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Golg   (8 posts)  Bio
Date Reply #11 on Mon 07 Mar 2005 01:21 AM (UTC)
Message
Thanks...I don't think my mind was adhering to common sense at the time of my last post.

Now, a problem that's suddenly popping up.

This line works:
sysdata.channel_history[chanhist_num]->message[sysdata.channel_history[chanhist_num]->lm_index] = "test";
But this segfaults:
strcpy(sysdata.channel_history[chanhist_num]->message[sysdata.channel_history[chanhist_num]->lm_index], argument);

Any idea why? I tried strncpy, but it still explodes. argument is a char * that holds the message sent by a MUD command like "chat blah".
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #12 on Mon 07 Mar 2005 06:26 AM (UTC)
Message
Quote:
sysdata.channel_history[chanhist_num]->message[sysdata.channel_history[chanhist_num]->lm_index] = "test";
This line is assigning the pointer value of the constant string 'test' to that long pointer.
Quote:
strcpy(sysdata.channel_history[chanhist_num]->message[sysdata.channel_history[chanhist_num]->lm_index], argument)
This on the other hand is trying to copy into the memory located at that long pointer, and chances are you have not allocated any memory there.

One works at not the other because in the first case, you are assigning a pointer to constant memory (strings like that are held in the executable as plain-text constants), but in the second case, you're trying to copy into memory that does not exist.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Golg   (8 posts)  Bio
Date Reply #13 on Mon 07 Mar 2005 03:31 PM (UTC)
Message
Ah, thank you for pointing that out...I was thinking along the wrong lines again. I allocated memory for each string in boot_db.c and then set each string to NULL. Oops.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #14 on Mon 07 Mar 2005 05:32 PM (UTC)
Message
Well, this may seem like nitpicking but a precise vocabulary sometimes helps clear things up in your head (it does for me, in any case.) You're not actually setting the string to null, but the string pointer. A string is a sequence of bytes, terminated by a null byte. So setting a string to null means (literally) pretty much that you have set the first byte to null, although most people do not use the term this way. The string pointer is the bit that needs to point towards a block of memory somewhere.

Forgive me if this feels obvious and you already knew it, but I figured it might be useful if you didn't.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


28,861 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.