Don't know what was said in the lecture, but I guess it was an example for something that shouldn't be done. You can't use free with a pointer to the middle of an allocated area, it causes segmantation fault (as you noticed). Ido
> -----Original Message----- > From: Haifux - Haifa Linux Club > [mailto:[EMAIL PROTECTED] Behalf Of Tzahi Fadida > Sent: Wednesday, March 09, 2005 2:49 AM > To: 'Orna Agmon' > Cc: [email protected] > Subject: [Haifux] c with a spoon and pointer arithmetic > > > As we talked about in the c with a spoon lecture, I tried the > pointer arithmetic and then free and at least for me it didn't > work. > gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release) > > [EMAIL PROTECTED]:~]$ gcc nis2.c > [EMAIL PROTECTED]:~]$ ./a.out > char y[0] a > char y[1] b > char y[2] c > char y[3] d > Segmentation fault (core dumped) > > #include <stdio.h> > #include <stdlib.h> > main(){ > char *y = (char *)malloc(4*sizeof(char)); > char *x = y + 2; > y[0]='a'; > y[1]='b'; > y[2]='c'; > y[3]='d'; > printf("char y[0] %c\n", y[0]); > printf("char y[1] %c\n", y[1]); > printf("char y[2] %c\n", y[2]); > printf("char y[3] %c\n", y[3]); > > free(x); > > printf("char y[0] %c\n", y[0]); > printf("char y[1] %c\n", y[1]); > // printf("char y[2] %c\n", y[2]); > // printf("char y[3] %c\n", y[3]); > > exit(EXIT_SUCCESS); > } > > Regards, > tzahi. > > WARNING TO SPAMMERS: see at > http://members.lycos.co.uk/my2nis/spamwarning.html > > > > -------------------------------------------------------------- > ------------ > Haifa Linux Club Mailing List (http://www.haifux.org) > To unsub send an empty message to [EMAIL PROTECTED] > > > -------------------------------------------------------------------------- Haifa Linux Club Mailing List (http://www.haifux.org) To unsub send an empty message to [EMAIL PROTECTED]
