For over the few months I've being trying to display LIST the of IPV4 Adresses in C++ programming , but unfotunately when I am going to compile the code it done properly but it shows nothing to me and error message generate please fix this problem you genius guy's...

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <stdlib.h>
#include <time.h>
#define MAX 30
#define TEMP 5

using namespace std;
void randomNumber_array(int *arr[]) {
    int r=0;
    srand(time(NULL));
    for(int i=0;i<4;++i)
    {
        r=0+rand()%246;
        *arr[i]=r;
    }

}
void ipAdress_listGenrator(char *ip_str,int num) {
    int i,j,temp=0,*a[TEMP],arr[TEMP];
    char temp_str[TEMP];

    for(i=0;i<num;++i)
    {
        for(j=0;j<4;++j)
        {
            a[i]=&arr[i];
        }
        randomNumber_array(a);
        for(j=0;j<4;++j)
        {
            sprintf(temp_str,"%d",*a[j]);
            if(i<3)
            {
                strcat(ip_str,temp_str);
                strcat(ip_str,".");
            } else {
                strcat(ip_str,temp_str);
            }
        }
        cout<<ip_str<<endl;
        *ip_str='\0'; //clearing a string 
    }
}
int main() {
    char ip_string[MAX]={'\0'};
    unsigned long int n;

    cout<<"Enter a number : "; cin>>n;
    ipAdress_listGenrator(ip_string,n);

    system("pause");
    return 0;
}

Dani AI

Generated

The crash and the weird output come from a handful of concrete problems (not a mysterious compiler bug). was right to recommend removing the pointer-array and moving srand to main. On top of that, these specific bugs in the later posts will crash or corrupt memory:

  • You use the wrong loop variable when deciding to add a dot: if (i < 3) in the octet loop should be if (j < 3). That makes the string format wrong.
  • The copy loop does for (c=0; c<strlen(ip_str); ++c) { ip_array[k][c]=ip_str[c]; ++c; } — it increments c twice, skipping bytes and corrupting memory.
  • Declaring char ip_string_arr[MAXIMUM][MAXIMUM] with MAXIMUM = 10000 allocates ~100 million bytes (or more) on the stack and will crash. Large arrays must be allocated dynamically or, better, use std::vector/std::string.
  • Calling srand repeatedly or from inside generator functions is wrong; call it once (or use <random>).

A simple, safe modern-C++ approach (avoids raw arrays entirely):

#include <iostream>
#include <random>
#include <vector>
#include <string>
#include <sstream>

int main() {
    size_t n;
    if (!(std::cin >> n) || n > 1000000) return 1; // sanity check
    std::vector<std::string> ips; ips.reserve(n);
    std::mt19937 rng(std::random_device{}());
    std::uniform_int_distribution<int> d(0,255);
    for (size_t i=0;i<n;++i) {
        std::ostringstream oss;
        for (int j=0;j<4;++j) oss << d(rng) << (j<3 ? "." : "");
        ips.push_back(oss.str());
    }
    for (auto &s: ips) std::cout << s << '\n';
}

Practical tips: use std::string or vector<string> instead of 2‑D char arrays, snprintf/strncpy if you must use C strings, and compile/tests with AddressSanitizer (-fsanitize=address) or run under Valgrind to find overflows. Fix the if(i<3) -> if(j<3), remove the extra ++c, and eliminate the enormous stack array — that will stop the crash.

Recommended Answers

All 3 Replies

It crashes because you are using i instead of j in a couple of places. Look at line 28.

As for the program as a whole, it's too complicated. You have an array of integers and an array of pointers to integers. The array of pointers to integers point to the array of integers so they add nothing and only complicate things, so my vote would be to get rid of them. You have a few other unneeded or unused variables. For example, this function...

void randomNumber_array(int *arr[]) {
    int r=0;
    srand(time(NULL));
    for(int i=0;i<4;++i)
    {
        r=0+rand()%246;
        *arr[i]=r;
    }

}

Can and should be simplified to this...

void randomNumber_array(int arr[]) 
{
    for(int i=0;i<4;++i)
    {
        arr[i]=rand()%246;
    }
}

Stick the srand(time(NULL)); at the top op main. You call it once and only once.

Change line 30 to...

randomNumber_array(arr);

Line 33 becomes...

sprintf(temp_str,"%d",arr[j]);

With that you can get rid of the a array entirely and with it, the bug that crashed the program.

Thank you Brother you are the best in the world.

My code crashed again after I made certain changes in it and I still don't know what'swrong with my code

/*
here is my code which I made changes and run 
*/

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <stdlib.h>
#include <time.h>
#define MAX 30
#define TEMP 5
using namespace std;
void randomNumber_array(int arr[]) 
{
    for(int i=0;i<4;++i)
    {
        arr[i]=0+rand()%246;
    }
}
void ipAdress_listGenrator(char *ip_str,int num) {
    int i,j,temp=0,*a[TEMP],arr[TEMP];
    char temp_str[TEMP];
    for(i=0;i<num;++i)
    {
        for(j=0;j<4;++j)
        {
            a[j]=&arr[j];
        }
        randomNumber_array(arr);
        for(j=0;j<4;++j)
        {
            sprintf(temp_str,"%d",*a[j]);
            if(i<3)
            {
                strcat(ip_str,temp_str);
                strcat(ip_str,".");
            } else {
                strcat(ip_str,temp_str);
            }
        }
        cout<<ip_str<<endl;
        *ip_str='\0'; //clearing a string 
    }
}
int main() {
    srand(time(NULL));
    char ip_string[MAX]={'\0'};
    unsigned long int n;

    cout<<"====================="<<endl;
    cout<<" IPV4 List Generator "<<endl;
    cout<<"====================="<<endl;
    cout<<endl;
    cout<<"How many IP-Adresses you want to generate : "; cin>>n;
    ipAdress_listGenrator(ip_string,n);
    system("pause");
    return 0;
}
/*
It give me that output 
=====================
 IPV4 List Generator
=====================

How many IP-Adresses you want to generate : 10
121.37.107.11.
18.40.188.33.
132.84.208.156.
4118583214
58169052
1381792388
15210113049
9824417462
22073108174
529395173
Press any key to continue . . .

*/
// And here is the new which I made certain changes 
// in it because I want to store the IP-Adress string into the 2-D string array

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <stdlib.h>
#include <time.h>
#define MAX 30
#define TEMP 5
#define MAXIMUM 10000
using namespace std;
void randomNumber_array(int arr[]) 
{
    for(int i=0;i<4;++i)
    {
        arr[i]=0+rand()%246;
    }
}
void ipAdress_listGenrator(char *ip_str,int num,char ip_array[][MAXIMUM]) {
    int i,j,temp=0,arr[TEMP],c=0,k=0;
    char temp_str[TEMP];
    for(i=0;i<num;++i)
    {
        randomNumber_array(arr);
        for(j=0;j<4;++j)
        {
            sprintf(temp_str,"%d",arr[j]);
            if(i<3)
            {
                strcat(ip_str,temp_str);
                strcat(ip_str,".");
            } else {
                strcat(ip_str,temp_str);
            }
        }
        for(c=0;c<strlen(ip_str);++c)
        {
            ip_array[k][c]=ip_str[c];
            ++c;
        }
        ip_array[k][c]='\0';
        ++k;
        c=0;
        *ip_str='\0'; //clearing a string 
    }
}
int main() {
    srand(time(NULL));
    char ip_string[MAX]={'\0'},ip_string_arr[MAXIMUM][MAXIMUM];
    unsigned long int n;

    cout<<"====================="<<endl;
    cout<<" IPV4 List Generator "<<endl;
    cout<<"====================="<<endl;
    cout<<endl;
    cout<<"How many IP-Adresses you want to generate : "; cin>>n;
    ipAdress_listGenrator(ip_string,n,ip_string_arr);

   cout<<"List Generated "<<endl;
   for(int i=0;i<n;++i)
   {
       cout<<ip_string_arr[i]<<endl;
   }

   system("pause");
   return 0;
}

//Result Program Crashed
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.