Solved

strcmp("string",NULL)

Posted on 2004-09-03
13
571 Views
Last Modified: 2008-03-10
Hi.Experts:
Program:
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::;
int average(char *first, char *second, long ii, double mm, ...)
{
   int count = 0, sum = 0, i;
   char *buffer;
   va_list marker;
   va_start(marker, mm);
   while (strcmp(buffer = va_arg(marker, char *), NULL))
      printf("[%s]\n", buffer);
   va_end(marker); /* Reset variable arguments. */
   return (sum ? (sum / count) : 0);
}

void main()
{
   clrscr();
   printf("average=%d\n", average("avg", "DDD", 123L, 23.0, "10", "20", "39", NULL));
}
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::;
In turbo C/C++,the program could execute completely.
In VC 6.0,aftering executing,the program will show  access exception  error.
I find the program 's problem is on:
==> while (strcmp(buffer = va_arg(marker, char *), NULL))
But i don't know why???
Why 's difference between in real mode and in protect mode???
Thanks!
0
Comment
Question by:TKD
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 11970889
NULL is NOT a string, but zero! If you want to compare a string to an empty string, use "". If you want to verify that a pointer is non-zero (i.e. points to a valid location (?)), then don't use strcmp, but use ptr!=NULL
0
 

Author Comment

by:TKD
ID: 11972475
I know what you say.
But i would like to know why the program exist a access exception in VC 6.0.
I execute the progrma in turbo C/C++,and there isn't any exception.
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 11972510
You might just be lucky to get it running at all! The program is just plain wrong.

Most likely explanation: the address 0 will be readable using one runtime system and illegal using the other. Depends on the code that is generated. Suggestion: compare the generated assembly code in both cases.
0
 

Author Comment

by:TKD
ID: 11972545
Oh....
Isn't there any different between access address 0 in real mode and in protect mode?
I find that there isn't any exception in real mode.
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 11972717
Could be, I don't know. Some parts of the program's data memory could be in a protected (R/O) segment. I also know of environments where the lower memory block was excluded (addresse 0-255 or even more), to generate just the error you are getting.
0
 
LVL 4

Accepted Solution

by:
alikoank earned 80 total points
ID: 11976648
well, in C language all strings are arrays represented by their first bytes, and when you call strcmp("str1", "str2") you are actually passing pointers to strings "str1" and "str2" these strings have a null character at their ends to distinguish the end of string hence the name NULL TERMINATED STRING.

you can pass NULL as an argument to all string related routines, it is legal in compilers perspective, you are just passing an addres, albeit it is zero.

data at address zero (read this as NULL) is inaccessible on windows platform, so you got an exception, whereas DOS programs have no such limitation.

As far as I remember memory location zero has a string like "Turbo C/C++ ...." by default on programs compiled with Borland C products. so if you pass NULL as an argument to a string function you are practically passing this Borland copyright note string to your function.

0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:TKD
ID: 11978502
Windows platform don't  permit to access data at address zero.
Dos programs don't have such limitation.
Where could I get  those information???
Thanks!
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 11978598
The difference is probably in the implementation of the standard
library routines between Turbo-C and VC++.  The Borland library
might check for NULL parameters and exit gracefully, whereas the
Microsoft library probably considers it a programming error, and the
seg-fault will draw immediate attention to the problem.

0
 
LVL 4

Expert Comment

by:alikoank
ID: 11979663
>>Windows platform don't  permit to access data at address zero.
>>Dos programs don't have such limitation.
>Where could I get  those information???

I should say restriction is a better choice of word than limitation, on my previous comment.
You can find these kind of information on platform SDK documentations.
0
 

Author Comment

by:TKD
ID: 11984128
Nn...
I could find these kind of information on platform SDK documents.
But I don't know how to use keyword to search??? or where to get those information???
Could someone hint me???
Thanks~
0
 
LVL 4

Expert Comment

by:alikoank
ID: 11984366
if you want to go deep into windows system programming aspects

you can take a look at win32 programming FAQ at
http://www.iseran.com/Win32/FAQ/faq.htm

or buy a book on win32 system programming
0
 

Author Comment

by:TKD
ID: 11984400
Thanks for alikoank,but I think there isn't any thing about the topic.
I want to know what 's different between access memory in real mode and in protect mode.
I wouldn't like to know how to window programming.
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 11985293
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now