Search code examples
cmallocaccess-violation

Access violation using malloc c language


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.


Solution

  • Just wanting to answer this old question from long ago when I was a noob with no rep to answer. (originally answered it with an update to the question).

    Somewhere else in the code I was allocating memory the size of a pointer to a struct. I'm guessing this causes tiny excess or misalignment in memory allocation.

    Not exactly sure why but this causes the very next use of malloc to crash but only under certain environmental conditions.

    The crashing happening long after the problem code was executed caused me to look for the problem in all the wrong places. Eventually I sat down and rewrote the entire program until eventually I noticed one line of code was allocating sizeof(a pointer).🤦‍♂️

    So allocating sizeof(the actual struct) rather than sizeof(pointer to the stuct) fixed all the problems.