Solved

strcmp("string",NULL)

Posted on 2004-09-03
13
570 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops 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.

757 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

22 Experts available now in Live!

Get 1:1 Help Now