> TRACK_DATA * track[MAX_TRACK_INROOM]; > > Now I think the above may be where my problem is -- should > this be w/out a *?
As Edwin pointed out in his reply to your first post, yes. > What does the * indicate here? I notice that > if I take it out, I have to call the variables in the track_data structure > with . instead of ->, so I'm assuming the * means that instead of > TRACK_DATA initializing a new array, it also makes it a pointer? Close. It's not a pointer to an array. It makes it an array of pointers. Here's the best tutorial on reading C declarations I could find real fast: http://infoweb.magi.com/~anderson/cwrule/cwrule.html Pointers are not places to store data. They just tell you where the data is. You have to take care of allocating the storage for it yourself. Pointers are mainly used for dynamic memory management, when you don't know how much memory you're going to need up front. In your case, you do know exactly how much memory you want, so there's no need to go through the hassle of dynamic memory allocation/pointers. You want a plain array of structures (arrays DO allocate storage for you). Take off the * in the declaration, and use the . syntax for accessing the members of your structures. Here's a little tutorial on pointers: http://home.netcom.com/~tjensen/ptr/cpoint.htm > ch->in_room->track = $4 {0x0, 0x0, 0x0, 0x0, 0x0, 0x0} Here's the contents of your array: 6 pointers, all with the value 0 (NULL). Now, since your program doesn't have access to memory address 0, when you try to do "ch->in_room->track[i]->name" (go to memory address 0, treat it like a track_data structure, and write the name), the OS notices you trying to write memory that you were never given access to and signals you to tell you about the segment violation (segment is a memory management term), and then kills your process when you don't catch the signal. Dennis On Wed, 13 Mar 2002, Cameron Barry wrote: > Hey Dennis, > > Thank you very much for your input. I really don't know more than a > dog about pointers. My C book sucks too, no practical application. > *sigh* And I paid 28 dollars too! > > I apologize for not including all the important info -- this was kind of > a continuation from my last post and I assumed (first mistake) that > it would have been clear... > > ch->in_room = $1 (struct room-index_data *) 0x404b0970 > i = 0 > ch->in_room->track[i] = $3 (struct track_data *) 0x0 > ch->in_room->track = $4 {0x0, 0x0, 0x0, 0x0, 0x0, 0x0} > > Let me elaborate. > > In merc.h, I have: > > typedef struct track_data TRACK_DATA; > > #define MAX_TRACK_INROOM 6 > > struct track_data > { > char * name; > int door; > int timer; > }; > > under room_index_data, I have: > > TRACK_DATA * track[MAX_TRACK_INROOM]; > > Now I think the above may be where my problem is -- should > this be w/out a *? What does the * indicate here? I notice that > if I take it out, I have to call the variables in the track_data structure > with . instead of ->, so I'm assuming the * means that instead of > TRACK_DATA initializing a new array, it also makes it a pointer? > Plah. Fuck if I know.. Anyway, I appreciate your patience, and > your assistance. I will understand this some day. > > -Cameron > > ----- Original Message ----- > From: "Dennis" <[EMAIL PROTECTED]> > To: "Cameron Barry" <[EMAIL PROTECTED]> > Cc: <[email protected]> > Sent: Wednesday, March 13, 2002 12:46 PM > Subject: Re: Question 2: > > > > The first step is to understand what the error is. A SIGSEGV (Segment > > Violation signal) is sent by the OS when your program attempts to access > > memory that it does not have access to. The way this happens in C is > > when you attempt to use a pointer that points to some memory address > > outside your address space. > > > > GDB told you what line the error was on. Now you just have to figure out > what > > part of the line created the error. > > > > > ch->in_room->track[i]->name = ch->name; /* <--- THIS LINE */ > > > > >From here, you should use gdb to track it down further. "print > ch->name", > > "print ch->in_room", "print i", "print ch->in_room->track[i]" (I suspect > > this one will print out a bogus address), "print > ch->in_room->track[i]->name" > > (I suspect this one will give you an error that gdb can't reference > > the memory). > > > > Doing this will tell you which bogus pointer you are dereferencing. > > >From there you progress to the next step: Figuring out why it's bogus. > > > > My guess: You never set it to anything non-bogus, so by default it ends > > up bogus. > > > > I'm going to go out on a limb here and try to guess what your error is > > without seeing the rest of your code (from what I gather about your > > pointer experience in these 2 emails). I'm going to guess that in > > struct room_index_data, you still have track declared as > > "TRACK_DATA * track[7];", and nowhere in your code do you have anything > > even slightly resembling the following: > > for ( i = 0; i < 7; i++ ) > > room->track[i] = malloc ( sizeof ( TRACK_DATA ) ); > > I'm going to assume that you took Bobby's advice from the last question, > > and did exactly what he suggested, and no more. That's where the problem > > came in. His answer only fixed the syntax problem in your code. It > didn't > > fix the problem with design. To use that solution, much more is required, > > such as malloc()ing and free()ing the memory in the appropriate places. > > > > For what you are trying to do, Edwin's solution to your previous post > > solves the syntax error, and also fixes the design problem. > > > > Now, there's also the problem with "... = ch->name" that was already > > pointed out in another post. You'll need to work on that too. > > > > > I don't really understand -- if both are set to char *, why can't I set > one > > > to the > > > other? How would I go about accomplishing this? Will I have the same > problem > > > with the integers door and timer? > > > > > > The char * has nothing to do with your problem. It's the track[i]->name > > that is causing it. Since you are doing the same thing with door and > > timer, the same problem will pop up there too. > > > > > > Dennis > > > > > > > > On Wed, 13 Mar 2002, Cameron Barry wrote: > > > > > Another silly question (thanks Bobby for solving my last one!!)... > > > > > > I'm getting a Seg Fault in the following code: > > > > > > if (!IS_NPC(ch)) > > > { > > > for (i=ch->in_room->track_num; i <= MAX_TRACK_INROOM; i++) > > > { > > > if (i == ch->in_room->track_num) > > > { > > > ch->in_room->track[i]->name = ch->name; /* <--- THIS LINE */ > > > ch->in_room->track[i]->dir = door; > > > ch->in_room->track[i]->timer = timer; > > > > > > if (ch->in_room->track_num >= MAX_TRACK_INROOM) > > > { > > > ch->in_room->track_num = 0; > > > } > > > else > > > { > > > ch->in_room->track_num += 1; > > > } > > > } > > > } > > > } > > > > > > According to GDB, ch->in_room->track[i]->name = ch->name; is the line > > > causing the fault. > > > > > > I don't really understand -- if both are set to char *, why can't I set > one > > > to the > > > other? How would I go about accomplishing this? Will I have the same > problem > > > with the integers door and timer? > > > > > > Thanks! > > > -Cameron > > > > > > > > > > > > > > > > > > > > > -- > > ROM mailing list > > [email protected] > > http://www.rom.org/cgi-bin/mailman/listinfo/rom > > > > >

