The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
'memory corruption' error when using Awk kooyee UNIX for Dummies Questions & Answers 5 07-09-2009 01:36 PM
OS is not detected CPU and memory arumsun SUN Solaris 1 06-29-2009 06:15 AM
Pointer to a struct (with pointers) *** glibc detected *** double free jatoo High Level Programming 1 12-05-2008 08:31 AM
*** glibc detected *** double free or corruption: 0x40236ff4 *** norsk hedensk High Level Programming 19 11-14-2008 01:43 PM
*** glibc detected *** free(): invalid next size (normal): 0x0000000000503e70 *** vbreddy High Level Programming 1 04-11-2006 01:18 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-30-2009
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 753
Quote:
Originally Posted by cdbug View Post
where and how should this be done? Would you please give an example?

Thanks
Here's how...

Code:
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;
}

  #2 (permalink)  
Old 10-30-2009
cdbug cdbug is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 52
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?
  #3 (permalink)  
Old 10-30-2009
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 753
Quote:
Originally Posted by cdbug View Post
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...

Code:
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.
  #4 (permalink)  
Old 4 Weeks Ago
cdbug cdbug is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 52
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)



Code:

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ē"

Last edited by cdbug; 4 Weeks Ago at 03:17 PM..
  #5 (permalink)  
Old 4 Weeks Ago
achenle achenle is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 78
Quote:
Originally Posted by cdbug View Post
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)



Code:

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?


Code:
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().
  #6 (permalink)  
Old 4 Weeks Ago
cdbug cdbug is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 52
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"
  #7 (permalink)  
Old 4 Weeks Ago
cdbug cdbug is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 52
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)?
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:44 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0