LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-26-2009, 04:15 AM   #1
denalta
LQ Newbie
 
Registered: Mar 2009
Posts: 2

Rep: Reputation: 0
Realloc problem of struct


I have a problem with dynamic allocation of memory in code.
The function must return by reference array "data" that looked out of function, like: data[0].word.

Code:
void insert_data(DATA **data, DB * db,
                    char * label,
                    char * filepath,
                    char * cmd,
                    OPERANDS operands,
                    char * word,
                    char * are) {

    if ( *data == NULL ) {
        db->data_rows = MAXLINES2;
        (*data) = (DATA *)safeMalloc(sizeof(DATA) * db->data_rows + 12,__FILE__,__LINE__);
    } else {

        if ( db->data_counter > db->data_rows ) {
            db->data_rows += MAXLINES2;
            // not working!!!!!!
            *data = (DATA *) realloc((*data), sizeof(DATA) * db->data_rows + 12); // ?????????

        }
    }


    // set decimal address
    (*data)[db->data_counter].d_addr = db->data_counter;

    // do something with strings

    db->data_counter++;
}
#define MAXLINES2 10
structure DATA defined like:
Code:
typedef struct table_data DATA;
    struct table_data {
        int ovalue;            // octal machine code
        char *bcode;            // binary machine code
            // fields of binary code
            char *t_cmd;        // target command
            char *t_addr;        // target address
            char *s_cmd;        // source command
            char *s_addr;        // source address
            char *c_cmd;        // command code
        // operands
        char *op1;            // operand no.1
        int  type1;            // type of operand no.1
        char *op2;            // operand no.2
        int  type2;            // type of operand no.2
        char *cmd;            // command
        int    o_addr;            // octal address
        int    d_addr;            // decimal address
        char *label;

        char * word;            // binary machine code
        char * are;            // absolute, relocatable, external
    };
the error that I get is:
*** glibc detected *** realloc() invalid next size: ...

function safeMalloc() is regular malloc() of gcc.

Thanks for help.
 
Old 03-26-2009, 06:28 AM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
safeMalloc probably returns a pointer other than one directly provided by malloc. Try changing that call to malloc and see what happens.
Kevin Barry
 
Old 03-27-2009, 12:36 PM   #3
denalta
LQ Newbie
 
Registered: Mar 2009
Posts: 2

Original Poster
Rep: Reputation: 0
that is code of safeMalloc:


Code:
void * safeMalloc( unsigned int uiSize, char *cpFileName, int iLineNo ) {
void * vpTemp;

     vpTemp = malloc( (unsigned) uiSize );
     if (vpTemp == (void *) NULL) {
         fprintf( stderr, "ERROR : malloc() failed in safeMalloc()\n");
         fprintf( stderr, "ERROR : safeMalloc called in file %s : line %d\n", cpFileName, iLineNo );
         exit (1);
     }
     else
        return( vpTemp );
}
when I change it to regular malloc, there is the same problem.

Last edited by denalta; 03-27-2009 at 12:37 PM.
 
Old 03-27-2009, 01:49 PM   #4
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,051

Rep: Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100
Is this yet another restart of the thread at?
http://www.linuxquestions.org/questi...ucture-714380/

But I'll still tell you what the bug is in your code:

You allocated room for 10 structs plus 12 bytes.
Then you used 11 structs (indexes 0 through 10 inclusive).
Then, preparing to use index 11, you tried to realloc for 20 structs plus 12 bytes.

But when you used the 11'th struct (index 10) you corrupted the heap, so you can't realloc.

If you intended to only use 0 through 9 after allocating room for 10, change
Code:
if ( db->data_counter > db->data_rows ) {
to
Code:
if ( db->data_counter >= db->data_rows ) {
But if you intended to use 0 through db->data_rows (inclusive) before reallocating, change
Code:
sizeof(DATA) * db->data_rows + 12
to
Code:
sizeof(DATA) * ( db->data_rows + 1 )
In both places, to allocate room for one extra structure rather than 12 extra bytes.

Last edited by johnsfine; 03-27-2009 at 02:02 PM.
 
  


Reply

Tags
error, realloc


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GCC compile problem:struct A have a member variable which is just a struct type name? leon.zcom Programming 3 04-18-2008 04:40 PM
in external function: realloc of array within a struct ruh31 Programming 1 01-05-2008 10:02 AM
realloc and segfaulting problem SciYro Programming 2 05-30-2005 08:46 AM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM


All times are GMT -5. The time now is 01:19 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration