From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1164f8,8083a431078ea083 X-Google-Attributes: gid1164f8,public From: Ron Natalie Subject: Re: Segmentation Violation Date: 1996/11/06 Message-ID: <3280E47C.B0E@sensor.com>#1/1 X-Deja-AN: 194927965 references: <55qgbj$tdi@pulp.ucs.ualberta.ca> content-type: text/plain; charset=us-ascii organization: Sensor Systems mime-version: 1.0 newsgroups: comp.sys.dec x-mailer: Mozilla 3.0Gold (X11; I; HP-UX A.09.05 9000/755) Nader Behzad wrote: > char *tempstring; > > tempstring = (char *)Malloc(256*sizeof(char)); > I don't know what "Malloc" is, but if it is the same as "malloc" there is no guarantee as to the contents. Also, I'm not clear why you bother to set tempstring to anything here as you just clobber it two lines later (leaking 256 bytes in the process). Also, "sizeof (char)" is ALWAYS 1 (since sizeof is defined to return things in units of char's). > char_count = strlen(angle_file); > tempstring = strrchr(angle_file, '/'); > if (tempstring != NULL) { > tempstring++; > } > > i = (char_count - strlen(tempstring)); /* <-------- A */ > strncpy(temp, angle_file, i); > temp[i] = '\0'; > > "tempstring" is NULL and this causes the program to crash at point A. > The strlen result for NULL should be 0 right?? No it should not be. strlen(NULL) is illegal. There is a difference between a pointer to a zero-length string and a pointer value of zero. Strlen is not guaranteed to do anything if the argument is not a pointer to a null terminated. string. > I don't have this problem under SUN_OS or Solaris platforms. You're extremely lucky. Some platforms encourage people to write non portable and bogus code such as this by putting a zero at location zero, but you can not and should not rely on this behavior. Getting a violation or possibly an arbitrary value return from strlen are also possible. -Ron