• C

strcmp("string",NULL)

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!
TKDAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sjef BosmanGroupware ConsultantCommented:
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
TKDAuthor Commented:
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
Sjef BosmanGroupware ConsultantCommented:
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
Managing Security Policy in a Changing Environment

The enterprise network environment is evolving rapidly as companies extend their physical data centers to embrace cloud computing and software-defined networking. This new reality means that the challenge of managing the security policy is much more dynamic and complex.

TKDAuthor Commented:
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
Sjef BosmanGroupware ConsultantCommented:
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
alikoankCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TKDAuthor Commented:
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
brettmjohnsonCommented:
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
alikoankCommented:
>>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
TKDAuthor Commented:
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
alikoankCommented:
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
TKDAuthor Commented:
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
Sjef BosmanGroupware ConsultantCommented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.