dvu@pyr.gatech.EDU (Dinh Vu) (02/02/89)
I am learning C, and having difficulty with pointers, and structures. The small program below compiled fine, but it gave core dump when I run it. Would someone give me some light on this matter. #include <stdio.h> struct abc { int x; int y; }; main() { struct abc *var; var->x = 5; var->y = 10; } ====================================================================== Why have sex today, when you can have it tomorrow! -a horny procrastinator Dinh Vu Georgia Insitute of Technology, Atlanta Georgia, 30332 ...!{akgua,allegra,amd,hplabs,ihnp4,seismo,ut-ngp}!gatech!gitpyr!dvu ======================================================================
daves@hpopd.HP.COM (Dave Straker) (02/03/89)
You have a pointer to a structure, but you have not attached it to any structure, thus when you assign things, it is probably trying to put these things into memory which isn't yours. Dave
matthew@sunpix.UUCP ( Sun NCAA) (02/03/89)
In article <7208@pyr.gatech.EDU>, dvu@pyr.gatech.EDU (Dinh Vu) writes:
@ I am learning C, and having difficulty with pointers, and
@ structures. The small program below compiled fine, but
@ it gave core dump when I run it. Would someone give me
@ some light on this matter.
@
@
@ #include <stdio.h>
@
@ struct abc {
@ int x;
@ int y;
@ };
@
@ main()
@ {
@ struct abc *var;
@
@ var->x = 5;
@ var->y = 10;
@ }
@
@
@
@ Dinh Vu
Okay, I think I've got your problem figured out. What I see is a structure
named 'abc'. Next I see the declaration of a pointer to point to a structure of
type 'abc'. Next I see two structure pointer operations trying to initialize
elements of a structure, but no were do I see the definition of the structure
being pointed to.
Basically what I'm saying, is that your structure pointer is not being
initialized to point to a structure of type 'abc'.
--
Matthew Lee Stier (919) 469-8300|
Sun Microsystems --- RTP, NC 27560| "Wisconsin Escapee"
uucp: {sun, rti}!sunpix!matthew |
jair@ficc.uu.net (jair bobys) (02/04/89)
In article <7208@pyr.gatech.EDU>, dvu@pyr.gatech.EDU (Dinh Vu) writes: > I am learning C, and having difficulty with pointers, and structures > [some text deleted] > main() > { > struct abc *var; > > var->x = 5; > var->y = 10; > } The problem is that you never allocated a structure for your pointer to point to. Try: main() { struct abc *var, place; var = place; var->x = 5; var->y = 10; } You were trying to assign values to nonexistent locations, hence the core dump. -- +------------------------------------------------------------------------------+ |"One ring to bring them all | Jair Bobys (713) 274-5201 | | and in the darkness bind them." | uunet!ficc!jair jair@ficc.uu.net| +------------------------------------------------------------------------------+
mlandau@bbn.com (Matt Landau) (02/04/89)
In comp.lang.c (<2990@ficc.uu.net>), jair@ficc.uu.net (jair bobys) writes: >Try: > main() > { > struct abc *var, place; > > var = place; Yeah, but it might work even better if you were to try: var = &place; /* :-) */ -- Matt Landau Oblivion gallops closer, mlandau@bbn.com favoring the spur, sparing the rein.
savage@tramp.Colorado.EDU (SAVAGE CHARLES L) (02/07/89)
In article <7208@pyr.gatech.EDU> dvu@pyr.gatech.EDU (Dinh Vu) writes: >The small program below compiled fine, but >it gave core dump when I run it. Would someone give me >some light on this matter. > > #include <stdio.h> > > struct abc { > int x; > int y; > }; > > main() > { > struct abc *var; --------------->var = (abc *) malloc ((unsigned) sizeof(struct abc)); > var->x = 5; > var->y = 10; > } > Sombody has probably already fixed this error, but you must alocate memory for the pointer to point too. If not, all the pointer has is a random address, and when you assign a value to the pointer it overwrites some memory in storage that may be used by your source code or may not be yours at all. Savage. D D D
throopw@xyzzy.UUCP (Wayne A. Throop) (02/09/89)
> dvu@pyr.gatech.EDU (Dinh Vu) > The small program below compiled fine, but > it gave core dump when I run it. Would someone give me > some light on this matter. In general, you should run "lint" on your programs. It will catch many simple errors, such as the one here. In this instance, lint has this to say: (12) warning: var may be used before set (14) warning: main() returns random value to invocation environment about the appended program Dinh was wondering about. It is perhaps a little cryptic, but it covers the essentials: the struct pointer was used before it was set to anything, and the program will potentially return an undefined value to its invoker. #include <stdio.h> struct abc { int x; int y; }; main() { struct abc *var; var->x = 5; var->y = 10; } -- "Cogito, ergo spud." I think, therefore a yam. -- Wayne Throop <the-known-world>!mcnc!rti!xyzzy!throopw