|
-
Jun 13th, 2003, 11:55 PM
#1
Thread Starter
Hyperactive Member
p && *p !='$' I don't understand why p is so important to this test!<Resolved>
Hello everyone, I was looking at some code, and this throws me off, i have no idea why if I delete p and just leave *p!='$'; it will crash the whole program. here's the code:
Code:
void tokenize(char **name, int &n, char * buffer)
{
char *p = strtok(buffer, "\n");
for(n = 0; p && *p !='$'; n++)
{
name[n] = p;
p = strtok(NULL, "\n");
}
}
here's some info about the values being passed:
Code:
const int NAME_LENGTH = 20;
const int MAX_NUM_NAMES = 25;
const int BUFFER_LENGTH = MAX_NUM_NAMES* (NAME_LENGTH + 1);
void input(char * buffer);
void tokenize(char **name, int & numNames, char* buffer);
void print(char **name, int numNames);
void sort(char ** name, int numNames);
int main(void)
{
char * name[MAX_NUM_NAMES];
char buffer[BUFFER_LENGTH+1];
int numNames;
input(buffer);
tokenize(name,numNames,buffer);
print(name,numNames);
sort(name,numNames);
print(name,numNames);
return 0;
}
If someone could explain to me what exactly p && *p !='$'; evalutes that would be great!
also, is it a good idea to use char ** or is there a better way to appraoch this? This isn't my code, its code out of a book and i've never seen it put this way thats why i'm so concerned!
-Thanks
Last edited by voidflux; Jun 16th, 2003 at 01:27 AM.
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Jun 14th, 2003, 12:28 PM
#2
Code:
if (p && *p != '$') { // ....
the first part is equivalent to if (p != NULL). this is necessary because if p is a bad pointer, then you don't want to touch it, because you'll end up overwriting something else, resulting in a crash at best.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Jun 14th, 2003, 05:43 PM
#3
Frenzied Member
Correct. p && *p != '$' first checks that p is not NULL. If it IS NULL, the && MUST be false, and so the second statement (*p) is never executed.
Z.
-
Jun 16th, 2003, 01:24 AM
#4
Thread Starter
Hyperactive Member
Thanks!
Thanks for the replies!
I figured thats what it did, it just suprised me that the program would crash for not checking a NULL pointer. Because if i allocate memory using the new keyword and forget to check if *p = NULL the program will still run fine. Thats what threw me off! thanks again!
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Jun 16th, 2003, 04:17 AM
#5
That's only because failures of new are rare. strtok however is guaranteed to return NULL sooner or later, so failing to check there is guaranteed to crash the app.
You must know that the app doesn't crash simply because you forget to check the pointer, the app crashes because you access a NULL pointer, which you must not do. The check only prevents you from doing so, but in cases where you're unlikely to do so the app might run 10000 times before it crashes (because, due to strange circumstances involving UFOs and unknown quantum phenomena, the operation that usually succeeds decided to fail).
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|