Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

strcmp("string",NULL)

Posted on 2004-09-03
13
Medium Priority
?
591 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 320 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
 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

571 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