Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 03-26-2009, 05:15 AM   #1
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.

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

#define MAXLINES2 10
structure DATA defined like:
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, 07:28 AM   #2
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, 01:36 PM   #3
LQ Newbie
Registered: Mar 2009
Posts: 2

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

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);
        return( vpTemp );
when I change it to regular malloc, there is the same problem.

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

Rep: Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190
Is this yet another restart of the thread at?

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
if ( db->data_counter > db->data_rows ) {
if ( db->data_counter >= db->data_rows ) {
But if you intended to use 0 through db->data_rows (inclusive) before reallocating, change
sizeof(DATA) * db->data_rows + 12
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 03:02 PM.


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 05:40 PM
in external function: realloc of array within a struct ruh31 Programming 1 01-05-2008 11:02 AM
realloc and segfaulting problem SciYro Programming 2 05-30-2005 09:46 AM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 11:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 05:30 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:18 PM.

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