I’m hoping to implement a simple molecular dynamics program. My first step is to define the system as a series of atoms, each with a type, an id number, a 3 dimensional position vector, and a 3D velocity vector. Below is the program I’ve written to do so:
FILE *init;
static int randomVelocity(void)
{
return rand()/RAND_MAX - 0.5;
}
int main(int argc, char *argv[])
{
int iType;
int iID;
int i;
double* pdPosition;
double* pdVelocity;
char* line;
Atom* poAtoms;
int count = 0;
init = fopen("newdat.txt", "r+");
srand((unsigned)time(NULL));
line = malloc(81*sizeof(char));
while (fgets(line, 80, init) != NULL)
{
char* tok1;
char* tok2;
char* tok3;
char* tok4;
tok1 = strtok(line, " \t");
if ((tok1 == NULL) || (tok1[0] == '*'))
{
break;
}
tok2 = strtok(NULL, " \t");
tok3 = strtok(NULL, " \t");
tok4 = strtok(NULL, " \t");
iType = atoi(tok1);
iID = count;
pdPosition = (double*)malloc(3*sizeof(double));
pdVelocity = (double*)malloc(3*sizeof(double));
pdPosition[0] = atof(tok2);
pdPosition[1] = atof(tok3);
pdPosition[2] = atof(tok4);
pdVelocity[0] = randomVelocity();
pdVelocity[1] = randomVelocity();
pdVelocity[2] = randomVelocity();
poAtoms[count] = Atom_new(iType, iID, pdPosition, pdVelocity);
count++;
}
for (i = 0; i < count; i++)
{
Atom_print(poAtoms[i]);
Atom_free(poAtoms[i]);
}
free(line);
return 0;
}
Here is the header file atom.h:
/**** atom.h ****/
typedef struct Atom_str *Atom;
Atom Atom_new(int iType, int iID, double* adPosition, double* adVelocity);
void Atom_free(Atom oAtom);
void Atom_print(Atom oAtom);
and the test input file:
1 5 7 9
2 12 13 14
The program compiles, but when I run it, I get the expected output followed by a seg fault. I’m using the GDB debugger, and the seg fault appears to happen on the very last line of code, after the return statement! Is it a memory management issue?
You’ve never
malloced memory forpoAtoms. Writing to wherever that uninitialized pointer points can easily cause a segfault.Before you start reading the file, you should allocate some space,
And then you have to check inside the read-loop that you’re not writing past the allocated memory. Before
insert a check,
if the allocated space for
poAtomsis already used, try to get more withrealloc, if that fails, abort unless you know how to fix it. If the reallocation succeeds, we can continue collecting new atoms.