debug assertion failed error nptr!=NULL - c++

I get an Debug Assertion Error with expression: nptr!=NULL
my code:
#include <iostream>
using namespace std;
void main(int argc, char* argv[])
{
cout << "Hello Number " << atoi(argv[1]) << endl;
}
can somebody please help me solve this?

Most likely explanation is that you're not passing any parameters to your program, such as you would with the command runme 7.
The argv[argc] string is required to be NULL so this would explain why the assertion is happening.
Check that you have the correct number of parameters before trying to use them:
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[]) {
if (argc != 2) {
cerr << "Usage: runme <integer argument>" << endl;
return 1;
}
cout << "Hello Number " << atoi(argv[1]) << endl;
}

Related

Crypto++ : Hash generation hangs on windows 10

I have the following simple program :
#include <cryptlib.h>
#include "sha.h"
#include <sha3.h>
#include <filters.h>
#include <hex.h>
#include <beast/core/detail/base64.hpp>
using namespace CryptoPP;
using namespace boost::beast::detail::base64;
int main(int argc, char** argv) {
if (argc < 2) {
std::cout << "missing argument 1 : password";
return 0;
}
std::string password = std::string(argv[1]);
byte digest[SHA3_256::DIGESTSIZE];
SHA3 digestAlgo = SHA3_256();
std::cout << "going to calculate the digest\n";
digestAlgo.Update((const byte*) password.data(), password.size());
std::cout << "updated...\n";
digestAlgo.Final(digest);
std::cout << "calculated the digest\n";
char* b64encodedHash = (char*)malloc(sizeof(byte)*1000);
encode(b64encodedHash, digest, sizeof(byte)*1000);
std::cout << "password hashed : " << b64encodedHash << "\n";
return 1;
}
When I run it the text : "going to calculate the digest" is output on the command line and the program does not continue. It hangs.
Does anyone know why ? I am trying to follow the examples on the Crypto++ wiki, and this is very similar to theirs.
After the Final call I want to base64 encode the digest, you can remove that part, it uses a boost header file.
Thanks,
Regards
Change the line
SHA3 digestAlgo = SHA3_256();
to
SHA3_256 digestAlgo;

problem with compiling a simple program with c++

I am really new to C++. I am trying to compile this simple program but I get an error
#include <iostream>
int main(int argc, char** argv)
{
cout << "You have entered " << argc
<< " arguments:" << "\n";
for (int i = 0; i < argc; ++i)
cout << argv[i] << "\n";
return 0;
}
The error that I get is
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
I try to compile it as g++ file1.cpp
I also tried
g++ -c file1.cpp and then g++ main.exe file1.o
which does not work as well.
What am I doing wrong?
Maybe try and create a new file in an empty directory name it if you want something like main.cpp and put the code in that you needed for namespace and everything else and run
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
cout << "You have entered " << argc
<< " arguments:" << "\n";
for (int i = 0; i < argc; ++i)
cout << argv[i] << "\n";
return 0;
}
It should work after and output a result
Maybe check out this medium article for more about why you need using namespace std
https://medium.com/breaktheloop/why-using-namespace-std-is-used-after-including-iostream-dc5ae45db652
You can try specify namespace like this ->
#include <iostream>
using namespace std;
EDIT 'must' replaced by 'can try'. It works but why !

Opening image with ImageMagick via c++

Creating a simple project for an open image with ImageMagick.
Using simple code:
#include "Magick++.h"
#include <iostream>
using namespace std;
using namespace Magick;
int main(int argc, char **argv)
{
const char *c = "test.png";
cout << c << endl;
try {
InitializeMagick(*argv);
Magick::Image img;
img.read(c);
img.write("logo.png");
}
catch (Exception &error_)
{
cout << "Caught exception: " << error_.what() << endl;
return 1;
}
return 0;
}
But while opening any image(img.read(c)) I get an error:
Caught exception: MyProject.exe: unable to open image `≡█I': No such file or directory # error/blob.c/OpenBlob/2695
Work with: ImageMagick-7.0.3-Q8, Windows 7 x64

char **argv[] segfaults when trying to get input from the command line

I'm trying to do the most basic of things and am hitting a brick wall. I'm trying to read in a file name from the command line to use later in my program, but I can't even seem to extract the name from argv[]. Here's the code:
#include <iostream>
#include <string.h>
using namespace std;
int main(int argc, char **argv[]){
cout << "argc = " << argc << "\n\n";
cout << "Filename: " << argv[1] << "\n";
return 0;
}
I'm testing it on inputs that supply an argument of course, since there's no error checking. Here's what I get when I run the program:
./a.out testfilename
*
argc = 2
Filename: 0x7fff56e41d30
Now I understand argv[1] is a pointer to an array of chars, so this output makes sense. However, if I make the following change,
cout << "Filename: " << argv[1] << "\n";
to
cout << "Filename: " << *argv[1] << "\n";
in an attempt to dereference argv[1] to pull out the actual string, I get a segmentation fault..
int main(int argc, char *argv[]){
or
int main(int argc, char **argv){
but not
int main(int argc, char **argv[]){
That should be:
int main(int argc, char *argv[])
^ only one * here

Thread exception when compiling this basic C++ code in Xcode

Everytime I compile this C++ code I get a thread exception I can't understand. What is wrong here?
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
string arg = argv[1];
if (arg == "-r")
cout << "First arg is -r" << endl;
return 0;
}
You forgot to check argc>=2 before assigning argv[1] to the string arg.
Are you sure you are running this program with passing a parameter?
A possible correction:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
if(argc<2)
{
cerr << "Not enough parameters" << endl;
abort();
}
string arg = argv[1];
if (arg == "-r")
cout << "First arg is -r" << endl;
return 0;
}