0

I was working on an assignment for class, and I think I got the program working properly, but now I would like to make some modifications to it just to better understand assert. The code is below -

#include <iostream>
#include <stdlib.h>
#include <assert.h>
using namespace std;

// Sample program that shows how command line arg works, in Unix g++
// Note argc and argv
// Also shows use of system call, that can launch any program
// system launches 'ls' to display files in the dir

void runAssert(int);

int main(int argc, char *argv[])
{

  cout << "Number of inputs: " << argc << endl;
  cout << "1st argument: " << argv[0] << endl;
  system ("ls");
  cout << "hello world" << endl;

  runAssert(argc);

  return 0;

}

void runAssert(int argc)
{
    assert(argc > 4);
}

So the program is supposed to keep track of the arguments passed into main through command line. The professor specified that it should take 4 arguments. This code works, as far as I can tell, but I don't know what 4 commands to pass it? I do g++ assignment.cpp -o assignment and then ./assignment -- But this last command only counts as one argument so the assert triggers. If I change the function to >= 1 then it works.

Another question I have is, how can I make it display an error message when it doesn't meet the requirements?
I have tried assert("Not the right amount of arguments", argc > 4) but then I get an error message about too many arguments being passed into main.

Thanks for any help, and sorry if my formatting is wrong. First time posting.

huSh
  • 55
  • 1
  • 10
  • 1
    Your program shouldn't crash when someone uses it the wrong way. – chris Jul 09 '15 at 16:48
  • To clarify chris's comment: the semantics of assert are basically "Crash the program if this expression doesn't evaluate to 'true'" - You probably don't want to assert here. http://www.cplusplus.com/reference/cassert/assert/ or http://stackoverflow.com/questions/1571340/what-is-the-assert-function?rq=1 – Pete Baughman Jul 09 '15 at 16:48
  • `This code works, as far as I can tell, but I don't know what 4 commands to pass it?` We don't know, it's your program. The usage of the program has to be known by you and/or your professor. – PaulMcKenzie Jul 09 '15 at 16:51

4 Answers4

5

This is a completely incorrect usage of assert. Use assert to state things that you, as a programmer, think are logically necessary. It is logically possible for someone to call your program with fewer than 4 arguments, so assert is not correct.

A common usage of assert is at the start of a function. (This is not validating arguments.) Consider int foo(void *k){ assert(k != NULL); ...} Once again, this is not validating the argument k. The assertion is a piece of documentation that tells the human writing the code at the call site that foo is not to be called with a NULL argument. It is a claim that in the properly written code, it is a logical necessity that k be non-null. If you want to validate the argument and generate a pretty error message, use an if statement.

One thing about assert is that you should assume it does not execute in normal operation. Typically, the program will be compiled with -DNDEBUG, which will turn all of the assertions into whitespace.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • Hey, thank you for the answer! What you are saying makes sense, but this is a basic security class so I would assume the professor is aware of this. Here are his instructions: Item 2: Write a C++ program to assert that the number of arguments to the program is more than 4. Use assert to achieve this and abort if the condition is violated. Do not use any if or while statements in your program. The platform is PC or unix (you make the call). You may use DeterLab or any other computer. – huSh Jul 10 '15 at 17:26
  • 1
    You should not assume your instructor is aware of anything. Much ignorance is often expressed from the front of a classroom, and that your professor is instructing you to use `assert` in this way is a pretty good indicator that you should be wary of what is being taught. – William Pursell Jul 12 '15 at 16:37
  • Hey, just wanted to update the thread. The professor wanted us to learn a basic use for assert, I guess he didn't care enough to write up a program that was fully correct. Thanks again for the help! – huSh Jul 27 '15 at 16:04
0

I don't know what 4 commands to pass

That's up to you. You could do:

./assignment a b c d

You will get get argv[1]="a", argv[2]="b", etc. Parsing and using these arguments is up to you; in this context you could try to find an example of processing the arguments. Maybe print them in reverse order or something basic like that ?


Regarding assert(): your usage is not strictly correct as pointed out in another answer. To answer your question, one simple way to display a message is by using && : https://stackoverflow.com/a/3692961/2750093

I don't think your professor would like that though, so you could do something a little bit more naive :

if (argc <= 4 )
{
    printf("Not the right amount of arguments\n");
    assert(false); // or return -1;
}
Community
  • 1
  • 1
antou
  • 706
  • 6
  • 12
0

ok the number of arguments should be checked before you start executing any of the program code.

In this case i guess the professor wanted you to pass the arguments passed to the program to ls .so it should be something like

 ./assignment -l -s -a 

In the above case the -l -s and -a are the arguments.

You can use an if condition to check the number of arguments instead of assert.

 if (argc < 4) {
     // print the error message
     // exit from program
 }

Check answer by william to know the reason why not to use assert in this case

Pradheep
  • 3,553
  • 1
  • 27
  • 35
0

assert(condition) crashes the program with an error message like file bar.cc line 123: assertion failure 'condition'. As such it is not useful for the user, but it is useful for the developer.

Use assert to express expectations (on the state of certain internally controlled variables) that are assumed to hold in the code immediately following the assertion. Don't use assert to check user input (externally controlled variables), but throw an exception instead. Exceptions can be caught by the user, assertions cannot.

Walter
  • 44,150
  • 20
  • 113
  • 196