This program I'm working on with another person was running fine at some point. Then sometimes it would crash but then run fine again after restarting Windows. Was very intermittent and now always crashes. At some point I thought maybe its because I upgraded to Windows 10 somehow (I know). On my Windows 10 machine it now it always crashes no matter what.
So I installed VS on my Windows 7 machine and got different behavior again.
So it runs fine but displays no output if it is run without debugging.
If I run with debugging it triggers an unhandled exception break point when allocating memory in the following section of code.
/*
* clone()
* Precondition: s is defined and not NULL
* Postcondition: a copy of s has been returned
* Informally: Clone a sample
*/
sample clone(sample s)
{
sample c;
c = (sample)malloc(sizeof(struct sample_int)); //<-----exception error
c->name = (char *)malloc((strlen(s->name) + 1)*sizeof(char));
strcpy(c->name, s->name);
c->sequence = (char *)malloc((strlen(s->sequence) + 1)*sizeof(char));
strcpy(c->sequence, s->sequence);
c->match = s->match;
return c;
}
When clicking continue I get an Access Violation at location... error.
By this stage it is the 2nd time the function has been called. Below is the header file for sample.c
/*
* Specification for the Sample ADT
* Author Julian Dermoudy
* Version 20/8/15
*/
#ifndef SAMPLE_H
#define SAMPLE_H
struct sample_int;
typedef struct sample_int *sample;
void init_sample(sample *s, char *name, char *sequence);
char *getName(sample s);
char *getSequence(sample s);
int getMatch(sample s);
void setName(sample s, char *name);
void setSequence(sample s, char *name);
void setMatch(sample s, int m);
sample clone(sample s);
char *toString(sample s);
#endif
The clone function is taking an existing sample struct with data in all fields.
Am I missing something about malloc? And why would it seem to work fine on one computer never on another and bring up this exception error on yet another computer?
Any advice would be greatly appreciated.