Random IP Generation Program

Hello,I was Designing a Program that has two function the random_ip() and main()
the random_ip() returns an array of random generated ip address
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include<stdio.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<errno.h>

char* random_ip(char figure,int size,int check)//a thousand,mil,bil etc
{
	printf("Figure %c Size %d\n",figure,size);
	int i=65536;	
	int j=0;
	int  BUFSIZE;
	struct sockaddr_in temp;
	switch(figure)
	{
		case 't':
		BUFSIZE=(size*1024);
		break;

		case 'm':
		BUFSIZE=(size*1024*1024);
		break;
	
		case 'b':
		BUFSIZE=(size*1024*1024*1024);
		break;

		default:printf("Choose btn choices available!\n");
	}	
	printf("BUFSIZE %d\n",BUFSIZE);
	char *ip_buffer[BUFSIZE];
	srand(i);//seed the randomizer
	while(j<BUFSIZE)
	{
		ip_buffer[j]=(char*)malloc(16);
		temp.sin_addr.s_addr=(unsigned long)rand()%4294967295;

		ip_buffer[j]=(char*)malloc(16);

		strcpy(ip_buffer[j],inet_ntoa(temp.sin_addr));
		
		j++;
	
	}
	
	if(check){
	int a;
	int repeated=0;
	char*ip;
	for(j=0;j<BUFSIZE;j++)
	{
		ip=(char*)malloc(16);
		strcpy(ip,ip_buffer[j]);
		
		for(a=++j;a<BUFSIZE;a++)
		{	
			if((strcmp(ip,ip_buffer[a]))==0){//repeated ip	
				repeated++;	
				printf("\t\t\t\tFound %s repeated addresses.\n",ip);
			}	
		}	
		ip=NULL;
	}
	if(repeated>0)
	printf("Found %d repetead Addresses in %d Addresses.\n",repeated,BUFSIZE);
	}
	
return ip_buffer;
}
main(int argc,char**argv)
{

	int i=9;
	int j=0;
	int BUFSIZE;
	srand(i);//seed the randomizer
	switch((char)*argv[1])
	{
		case 't':
		BUFSIZE=(atoi(argv[2])*1024);
		break;

		case 'm':
		BUFSIZE=(atoi(argv[2])*1024*1024);
		break;
	
		case 'b':
		BUFSIZE=(atoi(argv[2])*1024*1024*1024);
		break;

		default:printf("Choose btn choices available!\n");
	}
	printf("BUFSIZE MAIN:%d\n",BUFSIZE);
	char *ip_buffer[BUFSIZE];
	memcpy(ip_buffer,random_ip((char)*argv[1],atoi(argv[2]),0),BUFSIZE);
	
	while(j<BUFSIZE)
	{
	printf("%s\t\t\t %d\n",ip_buffer[j],j);
	//printf("%s\n",inet_addr((char*)ip_buffer[j]));
	j++;
	
	}printf("%d Addresses\n",j);
	
}

it seems the code has a problem with the memcpy() function.Please Help figure this one out!
Thanks.
That code shouldn't compile. If you're going to determine BUFSIZE at runtime, you have to dynamically allocate memory.

The whole things' a bit of a mess. I'd say the memcpy is the least of your problems. You know that code is asking for trouble, right?
There is quite a lot of dangerous code there! ;o)

I would take a look at using std::string instead of your char*s and std::vector instead of your arrays. Also you could look at using C++ streams rather than the old, C printf() functions:

strings
http://cplusplus.com/reference/string/string/

vectors
http://cplusplus.com/reference/stl/vector/

streams
http://cplusplus.com/doc/tutorial/basic_io/
There is a very distinct difference between writing C++ code and writing C code. While C++ does provide backwards compatibility with C, the languages are remarkably different from one another. This is clearly C code. It almost seems off-topic for this site.
Topic archived. No new replies allowed.