K
Kevin Walzer
The following code compiles with gcc but returns a bus error when I run
the executable. Any ideas?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* generate serial number list*/
int main (void) {
char appname[] = "MYAPP"; //new app
char myserial[100]; //pointer variable for serial numbers
int count = 20000; //number of serial numbers
int i; //index number;
char *serials[count+1]; //full list of serial numbers
int seriallength; //length of serial number
char *serialblock; //block of memory for serial number
//trim application name
char *shortname = (char *) malloc(2);
strncpy(shortname, appname, 2);
free(shortname);
//build the serial number array
for (i = 1; i < count; ++i) {
int five = i*5; //first number of the serial array
int eleven = i/11; //second number
int one = (i-1); //third number
sprintf(myserial, "%s-%i-%i-%i-%s", appname, five, eleven, one,
shortname);//assign component values to serial number string
seriallength=strlen(myserial); //length of serial number
serialblock = (char *) malloc(seriallength + 1); //allocate memory
for serial
if (serialblock == NULL) {
return 0; //check for null memory
}
strcpy(serialblock, myserial); //copy data to address of serial
block from serial
serials=serialblock; //assign data at serial block address to
next indeas of serials array
free(serialblock); //free memory
}
for (i = 1; i < count; ++i) {
printf(" %s\n", i, serials);
}
return 0;
}
the executable. Any ideas?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* generate serial number list*/
int main (void) {
char appname[] = "MYAPP"; //new app
char myserial[100]; //pointer variable for serial numbers
int count = 20000; //number of serial numbers
int i; //index number;
char *serials[count+1]; //full list of serial numbers
int seriallength; //length of serial number
char *serialblock; //block of memory for serial number
//trim application name
char *shortname = (char *) malloc(2);
strncpy(shortname, appname, 2);
free(shortname);
//build the serial number array
for (i = 1; i < count; ++i) {
int five = i*5; //first number of the serial array
int eleven = i/11; //second number
int one = (i-1); //third number
sprintf(myserial, "%s-%i-%i-%i-%s", appname, five, eleven, one,
shortname);//assign component values to serial number string
seriallength=strlen(myserial); //length of serial number
serialblock = (char *) malloc(seriallength + 1); //allocate memory
for serial
if (serialblock == NULL) {
return 0; //check for null memory
}
strcpy(serialblock, myserial); //copy data to address of serial
block from serial
serials=serialblock; //assign data at serial block address to
next indeas of serials array
free(serialblock); //free memory
}
for (i = 1; i < count; ++i) {
printf(" %s\n", i, serials);
}
return 0;
}