Hi i am new to C++ I am have a error like the one above:
on a linux working in the termial
commands
c++ round.cpp -lpthread -o moo.exe -- build file
./moo.exe -- running file
get
*** glibc detected *** ./moo.exe: free(): invalid pointer: 0xb7f226ec ***
then shows
======= Backtrace: =========
stuff
======= Memory map: ========
stuff
Aborted
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <errno.h>
#include <dirent.h>
#include <sys/types.h>
#include <cstring>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <pthread.h>
using namespace std;
#define MAX_THREAD 65535
void *scanipport (void *arg);
void *ping (void *arg);
typedef
struct info {
string ips ;
int startport; //start port
int endport; //end port
} scout;
// typedef
// struct
// {
// int id;
// int nproc;
// } parm;
void *ping (void *arg)
{
scout *p = (scout *) arg;
int i =0;
static string bohica;
bohica = p[i].ips;
cout << "131.204.36.xxx subnet" << endl;
int please = system (("ping " + bohica+" -c 3 -W .0005 -w .0005").c_str());
}
void *scanipport (void *arg)
{
int sd; //socket descriptor
int start; //start port
int end; //end port
int rval; //socket descriptor for connect
char responce[1024]; //to receive data
char *message="shell"; //data to send
struct hostent *hostaddr; //To be used for IPaddress
struct sockaddr_in servaddr; //socket structure
static string bohica;
int port;
const char* host;
int s = 0;
scout *p = (scout *) arg;
bohica = p[s].ips;
start = p[s].startport;
end = p[s].endport;
host = bohica.c_str();
port = start;
sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); //created the tcp socket
if (sd == -1)
{
perror("Socket()\n");
cout<<errno<<endl;
}
memset( &servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port); //set the port #
hostaddr = gethostbyname( host ); //get the ip 1st argument
// memcpy(&servaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length);
//connects to the ip in hostaddr
rval = connect(sd, (struct sockaddr *) &servaddr, sizeof(servaddr));
if (rval == -1)
{
//printf("Port %d is closed\n", port);
close(sd);
}
else
printf("Port %d is open\n",port);
close(sd); //socket descriptor
}
int main(int argc, char **argv)
{
int start; //start port
int end; //end port
static string bohica;
static string host;
string ip[255];
// creates array of the ips to scan a see if alive
ip[0]="131.204.36.0"; ip[1]="131.204.36.10"; ip[2]="131.204.36.20"; ip[3]="131.204.36.30"; ip[4]="131.204.36.1"; ip[5]="131.204.36.11";
// these used for testing add the rest of range in after code works
int i =0;
int s=0;
scout *p;
pthread_t *threads;
pthread_attr_t pthread_custom_attr;
threads=(pthread_t *)malloc(end*sizeof(*threads));
pthread_attr_init(&pthread_custom_attr);
bohica = argv[1];
start = atoi(argv[2]);
end = atoi(argv[3]);
p[s].ips = bohica;
p[s].startport = start;
p[s].endport = end;
if (argc > 1 )
{
for (i=0; i<end; i++)
{
pthread_create(&threads[i], &pthread_custom_attr, scanipport, (void *)(p));
start++;
p[s].startport = start;
}
/* Synchronize the completion of each thread. */
for (i=0; i<end; i++)
{
pthread_join(threads[i],NULL);
}
//free(p);
if (end >= 65536)
cout << "There are only 65535 ports"<<endl;
}
// pthreads creation one for each ip
else
{
for(int gh=0; gh<256; gh++) // this does ip range of 131.204.36.0 – 131.204.36.255
{
host = ip[gh];
p[s].ips = host;
pthread_create(&threads[gh], &pthread_custom_attr, ping, (void *)(p));
}
for (int kt=0; kt<256; kt++)
{
pthread_join(threads[kt],NULL);
}
//free(p);
}
return 0;
}