Why does this occur? *** glibc detected *** malloc(): memory corruption: 0x10013ff8 ***

there seems not to be error in this segment. In some computers, it can work well. But in others, it will give a failure.

why it ocurrs and how to deal with it?

in a function:

        if( *ver == NULL ) {
                *ver = (vertex *) malloc(sizeof(vertex));    //this line
                if( *ver == NULL ) {
                        printf("memory allocation error\n");
                }
        }

Hi. Is this better ?

if( ver == NULL ) 
{
   ver = (vertex *) malloc(sizeof(vertex));
   if(  ver == NULL ) 
   {
      printf("memory allocation error\n");
   }
 }

does not make effect. should generate many vertices for a graph. but it will fail after 20 vertices.

can generate 20 vertices. after then, it fails. can't figure out.

Can you post more of your code including the typedef of vertex...and on which platforms does it fail.

Linux 3.2.3-59


struct  vertex {
        int id;
        int seq;
        int pos;
        int clique;
        int numOfVer;
        int numOfPrev;
        int numOfNext;
        int *prev;
        int *next;
        vertex *front;
        vertex *back;
        vertex *left;
        vertex *right;
};

static void make_vertex(vertex **ver, int index, int s, int ps) {
       
        *ver = NULL;
        if( *ver == NULL ) {
                *ver = (vertex *) malloc(sizeof(vertex));
                if( *ver == NULL ) {
                        printf("memory allocation error\n");
                        exit(1);
                }
        }
        (*ver)->id        = index;
        (*ver)->seq       = s;
        (*ver)->pos       = ps;
        (*ver)->clique    = 0;
        (*ver)->numOfPrev = 0;
        (*ver)->numOfVer  = 0;
        (*ver)->numOfNext = 0;
        (*ver)->prev      = NULL;
        (*ver)->next      = NULL;
        (*ver)->front     = NULL;
        (*ver)->back      = NULL;
        (*ver)->left      = NULL;
        (*ver)->right     = NULL;
}

Regards

Allocate storage for ver before mallocing memory for *ver as ver maybe pointing to some arbitrary location producing the malloc corruption.

where and how should this be done? Would you please give an example?

Thanks

Here's how...

static void make_vertex(vertex **ver, int index, int s, int ps)
{
        ver = (vertex **)malloc(sizeof(vertex *));
        if (ver == NULL)
            exit(1);

        *ver = (vertex *)malloc(sizeof(vertex));
        if ( *ver == NULL ) {
            printf("memory allocation error\n");
            exit(1);
        }

        (*ver)->id        = index;
        (*ver)->seq       = s;
        (*ver)->pos       = ps;
        (*ver)->clique    = 0;
        (*ver)->numOfPrev = 0;
        (*ver)->numOfVer  = 0;
        (*ver)->numOfNext = 0;
        (*ver)->prev      = NULL;
        (*ver)->next      = NULL;
        (*ver)->front     = NULL;
        (*ver)->back      = NULL;
        (*ver)->left      = NULL;
        (*ver)->right     = NULL;
}

I tried this:

vertex *root;
make_vertex(&root, -1, 0, -1);

(gdb) p root
$1 = (vertex *) 0x0
(gdb) p *root
Cannot access memory at address 0x0

What's wrong?

You can do that only if you return a pointer to vertex from make_vertex...

vertex *root;
root = make_vertex(&root,-1,0,-1);

For this you need to change the definition of make_vertex to return a pointer to vertex instead of void.

Add return statement and change return type and it can work.

But it still fails after generating many vertices. Notice where failure occurs

This seems to be a general problem.

Thanks for all your help

____________________________________
Program received signal SIGSEGV, Segmentation fault.
0x0027ff92 in _int_malloc () from /lib/tls/libc.so.6
(gdb) bt
#0 0x0027ff92 in _int_malloc () from /lib/tls/libc.so.6
#1 0x0027f0fd in malloc () from /lib/tls/libc.so.6
#2 0x0804956e in make_vertex (ver=0xbfffb624, index=-1, se=0, ps=2)


vertex *root;
root = make_vertex(&root, -1, 0, -1);


static vertex* make_vertex(vertex **ver, int index, int s, int ps)
{
        ver = (vertex **)malloc(sizeof(vertex *)); //Here:fails
        if (ver == NULL)
            exit(1);

        *ver = (vertex *)malloc(sizeof(vertex));
        if ( *ver == NULL ) {
            printf("memory allocation error\n");
            exit(1);
        }

        (*ver)->id        = index;
        (*ver)->seq       = s;
        (*ver)->pos       = ps;
        (*ver)->clique    = 0;
        (*ver)->numOfPrev = 0;
        (*ver)->numOfVer  = 0;
        (*ver)->numOfNext = 0;
        (*ver)->prev      = NULL;
        (*ver)->next      = NULL;
        (*ver)->front     = NULL;
        (*ver)->back      = NULL;
        (*ver)->left      = NULL;
        (*ver)->right     = NULL;

	return *ver;
}

---------- Post updated at 12:37 PM ---------- Previous update was at 11:01 AM ----------

Tried to use make_vertex. It can change memory in this way. A new trouble takes place. Allocation really gives unexpected problems.

dataset[4] changes
__________________________________________________________________
110 r = make_vertex(&rho, -1, 0, i);
(gdb) p *(dataset+3)
$33 = 0x8145200 "AATTGTAAC"
(gdb) p *(dataset+4)
$34 = 0x8145a30 "GCTGAATGA"
(gdb) n
111 first = rho;
(gdb) p *(dataset+3)
$35 = 0x8145200 "AATTGTAAC"
(gdb) p *(dataset+4)
$36 = 0xb25a90 "\210Z�"

First, if you're going to overwrite the original contents of the pointer you pass it, why bother passing in its address in the first place?

static vertex* make_vertex( int index, int s, int ps )
{
    vertex *ver;
    ver = ( vertex * ) calloc( sizeof( vertex ) );
    if ( NULL != ver )
    {
        ver->id = index;
        ver->seq = s;
        ver->pos = ps;
    }
    return( ver );
}

If THAT blows up, your heap is getting corrupted before you even make the call to make_vertex().

I change make_vertex as you showed. It appears to work well. But dataset is destructed. This also gives troubles since it is what the code works on

dataset[4] has a different address after a call to make_vertex
_______________________________________________________________
(gdb) p dataset[4]
$1 = 0x8b91a30 "GCTGAATGA"
(gdb) n
(gdb) p dataset[4]
$2 = 0x792a90 "\210*y"

Here, dataset is an array of pointers to sequence. It is desctructed by the call to make_vertex.

Can we use some technique to protect this array(data)?

Can you post your entire code...you either dont have the right include file or something else is going on.

The file is large. I remove some parts which are not related but the array troube changes although still exists.

I will first try to deal with it with the methods recommended by the below window

Thanks

Check out valgrind, dmalloc, etc. See Memory debugger - Wikipedia, the free encyclopedia

Is this a threaded application?