LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 07-19-2011, 04:06 AM   #1
golden_boy615
Member
 
Registered: Dec 2008
Distribution: Ubuntu Fedora
Posts: 445

Rep: Reputation: 18
problem with realloc(): invalid next size


hello
I wrote a test program for learning usage of realloc() and I thin I did everything right but I get this error exactly with my 4th time that while tries to reallocate memory in MakeDSt function for DSt->SArray:
Quote:
realloc(): invalid next size
this is my complete code :

Code:
#include <stdio.h>
#include <stdlib.h>



typedef struct DRA
{
	int addr;
} DRA;

typedef struct {
    int	  index;
    DRA *RArray;
} DR;


typedef struct {
    int	  start;
} DSA;

typedef struct {
    int	  index;
    DSA *SArray;
} DS;




int MakeDRg(DR *DRg)
{
	unsigned int Current_Row=0;

    DRg->index=5;
    DRg->RArray=(DRA *)malloc(DRg->index*sizeof(DRA));
    while(Current_Row<DRg->index)
    {
        DRg->RArray[Current_Row].addr=Current_Row;
        Current_Row++;
    }
	return 1;
}


int MakeDSt(DS *DSt,DR *DRg)
{

	unsigned int Current_Row=1;

	DSt->SArray=(DSA*)malloc(sizeof(DSA));
	DSt->SArray[0].start=DRg->RArray[0].addr+1;

	while(Current_Row < DRg->index)
	{
        DSt->SArray=(DSA*)realloc(DSt->SArray,sizeof(DSA));
        DSt->SArray[Current_Row].start=DRg->RArray[Current_Row].addr+1;
		Current_Row++;
		DSt->index=Current_Row;
	}

	return 1;
}



int main()
{
	DR DRg;
	DS DSt;
	unsigned int Current_Row=0 ;
	while(1)
	{


        MakeDRg(&DRg);
        MakeDSt(&DSt,&DRg);
        while(Current_Row<DSt.SArray)
        {
            printf("arr:: %i\n",DSt.SArray[Current_Row].start);
            Current_Row++;
        }
        free(DSt.SArray);
        free(DRg.RArray);
	}
	return 0;
}
what did I do wrong?

Thanks for any help.
 
Old 07-19-2011, 05:01 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by golden_boy615 View Post
Code:
    DSt->SArray=(DSA*)malloc(sizeof(DSA));
...
        DSt->SArray=(DSA*)realloc(DSt->SArray,sizeof(DSA));
        DSt->SArray[Current_Row].start=DRg->RArray[Current_Row].addr+1;
what did I do wrong?
Your realloc doesn't increase the size. Then you use a larger size (corrupting the memory after the allocation).

Did you misunderstand the inputs to realloc? The size input to realloc is the total new size; It is not an increment.

Last edited by johnsfine; 07-19-2011 at 05:02 AM.
 
1 members found this post helpful.
Old 07-19-2011, 05:24 AM   #3
golden_boy615
Member
 
Registered: Dec 2008
Distribution: Ubuntu Fedora
Posts: 445

Original Poster
Rep: Reputation: 18
yes exactly I misunderstood I is working now.
 
  


Reply



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
Won't get realloc to work - glibc: invalid old size ruh31 Programming 5 06-08-2011 05:39 PM
Realloc problem of struct denalta Programming 3 03-27-2009 01:49 PM
Error during cimserver start : *** glibc detected *** realloc(): invalid pointer MaverickLikesMustang Linux - Software 0 02-14-2008 06:42 AM
*** glibc detected *** ./input: realloc(): invalid next size: custode Programming 4 05-04-2007 03:25 PM
realloc and segfaulting problem SciYro Programming 2 05-30-2005 08:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:46 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration