Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Strange error when reallocating memory

Reply
Thread Tools

Strange error when reallocating memory

 
 
francesco
Guest
Posts: n/a
 
      12-22-2012
I get this strange error message


subsets.exe: malloc.c:2451: sYSMALLOc: Assertion `(old_top == (((mbinptr)
(((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct
malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >=
(unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))
+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) &&
((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)'
failed.

The program works correcly for a certain numbers of data, say n=7, when I
try it for n>7, I get this error message.

I need to dinamically allocate memory for 2^n structs. It workd properly
until I try the program for n=8 or greater, then it fails.

Francesco
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      12-22-2012
francesco wrote:
> I get this strange error message
>
>
> subsets.exe: malloc.c:2451: sYSMALLOc: Assertion `(old_top == (((mbinptr)
> (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct
> malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >=
> (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))
> +((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) &&
> ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)'
> failed.
>
> The program works correcly for a certain numbers of data, say n=7, when I
> try it for n>7, I get this error message.
>
> I need to dinamically allocate memory for 2^n structs. It workd properly
> until I try the program for n=8 or greater, then it fails.


You have an error at line 42.

--
Ian Collins
 
Reply With Quote
 
 
 
 
francesco
Guest
Posts: n/a
 
      12-22-2012
>
> You have an error at line 42.


Sorry, this is he code

#include<stdlib.h>
#include<stdio.h>
typedef struct {
int *ptrSet;
int elementi;
}Set;
int pot(int base,int esponente);
void stamparesti( int *resti, int n);
int main()
{
Set *ptrPDS=NULL;
int *ptr2Set=NULL;
int *resti=NULL;
int *set=NULL;
int n,m;
int i=0,j=0;
int N,Q=0;
int card=0;
printf("Inserisci il numero di elementi di S\n");
scanf("%d",&n);
m=pot(2,n);
printf("L'insieme dei sottoinsiemi di un insieme S ha %d sottoinsiemi
\n",m);
ptrPDS=(Set *)malloc(sizeof(Set)*m);
if(ptrPDS==NULL)
{
printf("Impossibile allocare spazio per l'insieme PDS: memoria
insufficiente\n");
free(ptrPDS);
exit(1);
}
set=malloc(sizeof(int)*n);
// Verifica se esiste abbastanza memoria per l'allocazione dell'insieme
if(set==NULL)
{
printf("Impossibile allocare spazio per l'insieme S: memoria
insufficiente\n");
free(set);
exit(1);
}

printf("Iserire gli elementi di S\n");
for(i=0;i<n;i++)
{
printf("\nInserire elemento %d",i);scanf("%d",set+i);
}
for(i=0;i<m;i++)
{
printf("i=%d\n",i);
resti=malloc(sizeof(int)*n);
if(resti==NULL)
{
printf("Impossibile allocare spazio per l'insieme S:
memoria insufficiente\n");
free(resti);
exit(1);
}
N=i;
//Conversione in binario del numero i
for(j=0;j<n;j++)
{
Q=N/2;
*(resti+j)=N%2;
N=Q;
}
stamparesti(resti,n);
ptr2Set=malloc(sizeof(int));
if(ptr2Set==NULL)
{
printf("Allocazione di memoria non riuscita\n");
free(ptr2Set);
exit(1);
}
card=0;
for(j=0;j<n;j++)
{
printf("j=%d card=%d\t",j,card);
if(*(resti+j)==1)
{
ptr2Set=realloc(ptr2Set,++card);
if(ptr2Set==NULL)
{
printf("Allocazione di memoria non riuscita\n");
free(ptr2Set);
exit(1);
}
*(ptr2Set+card-1)=*(set+j);
}
}
free(resti);
(ptrPDS+i)->elementi=card;
(ptrPDS+i)->ptrSet=ptr2Set;
printf("i=%d\telementi=%d\tptrSet=%p\n",i,card,ptr 2Set);
}
// Stampa elementi sottoinsieme.
for(i=0;i<m;i++)
{
ptr2Set=(ptrPDS+i)->ptrSet;
printf("S%d={",i);
card=(ptrPDS+i)->elementi;
if (card==0) printf("}\n");
else
{
for(j=0;j<card-1;j++) printf("%d,",*(ptr2Set+j));
printf("%d}\n",*(ptr2Set+j));
}
printf("\n");

}
free(ptrPDS);
free(ptr2Set);
free(set);
return 0;
}
// Funzione per il calcolo della potenza di un numero intero n ad un
esponente
int pot(int base,int esponente)
{
int p=1;
int i=0;
while(i++<esponente)
p=p*base;
return p;
}
// Stampa dei resti
void stamparesti( int *resti, int n)
{
int j;
for(j=0;j<n;j++)
{
printf("\t%d",*(resti+j));
}
printf("\n");
}


I use Open Suse Linux 12.2, gcc compiler 4.7.1


Francesco

 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      12-22-2012
"francesco" <(E-Mail Removed)> wrote in message
news:50d60828$0$6827$(E-Mail Removed)...

> ptr2Set=malloc(sizeof(int));


Should this be:
ptr2Set=malloc(sizeof(int)*n);
?

(BTW the layout of your code could be improved!)

--
Bartc

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      12-22-2012
On 12/22/2012 2:21 PM, francesco wrote:
> [...]
> ptr2Set=realloc(ptr2Set,++card);


ptr2set = realloc(ptr2set, ++card * sizeof(int));

Better,

ptr2set = realloc(ptr2set, ++card * sizeof *ptr2set);

Other improvements are possible, and there may be other
problems. Start by fixing this one, and see how far you get.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
Rosario1903
Guest
Posts: n/a
 
      12-22-2012
On 22 Dec 2012 19:21:12 GMT, francesco <(E-Mail Removed)>
wrote:

>>
>> You have an error at line 42.

>
>Sorry, this is he code


tu scrivi qualcosa come
v=malloc(200000);
if(v==NULL)
{free(v);
return 0;
}

mentre e sufficiente scrivere
v=malloc(200000);
if(v==NULL) return 0;

poiche' se v == 0 allora v non punta a memoria
da liberare... cmq free(v) quando v==0 non reca danni
solo: perche' scriverlo se non necessario?

per il resto, quando ho visto m=2^n e m usato come parametro
per malloc ed come parametro per il ciclo for: ho abbandonato...

 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      12-22-2012
francesco <(E-Mail Removed)> writes:
> >
> > You have an error at line 42.

>
> Sorry, this is he code
>
> #include<stdlib.h>
> #include<stdio.h>
> typedef struct {
> int *ptrSet;


Fix your indentation (might be a news client issue).

> int elementi;
> }Set;
> int pot(int base,int esponente);
> void stamparesti( int *resti, int n);
> int main()
> {
> Set *ptrPDS=NULL;
> int *ptr2Set=NULL;
> int *resti=NULL;
> int *set=NULL;
> int n,m;
> int i=0,j=0;
> int N,Q=0;
> int card=0;
> printf("Inserisci il numero di elementi di S\n");
> scanf("%d",&n);
> m=pot(2,n);
> printf("L'insieme dei sottoinsiemi di un insieme S ha %d sottoinsiemi
> \n",m);
> ptrPDS=(Set *)malloc(sizeof(Set)*m);
> if(ptrPDS==NULL)
> {
> printf("Impossibile allocare spazio per l'insieme PDS: memoria
> insufficiente\n");
> free(ptrPDS);


pointless

> exit(1);
> }
> set=malloc(sizeof(int)*n);
> // Verifica se esiste abbastanza memoria per l'allocazione dell'insieme
> if(set==NULL)
> {
> printf("Impossibile allocare spazio per l'insieme S: memoria
> insufficiente\n");
> free(set);


pointless

> exit(1);
> }
>
> printf("Iserire gli elementi di S\n");
> for(i=0;i<n;i++)
> {
> printf("\nInserire elemento %d",i);scanf("%d",set+i);
> }
> for(i=0;i<m;i++)
> {
> printf("i=%d\n",i);
> resti=malloc(sizeof(int)*n);
> if(resti==NULL)
> {
> printf("Impossibile allocare spazio per l'insieme S:
> memoria insufficiente\n");
> free(resti);


pointless

> exit(1);
> }
> N=i;
> //Conversione in binario del numero i
> for(j=0;j<n;j++)
> {
> Q=N/2;
> *(resti+j)=N%2;


use array notation

> N=Q;
> }
> stamparesti(resti,n);
> ptr2Set=malloc(sizeof(int));
> if(ptr2Set==NULL)
> {
> printf("Allocazione di memoria non riuscita\n");
> free(ptr2Set);


pointless

> exit(1);
> }
> card=0;
> for(j=0;j<n;j++)
> {
> printf("j=%d card=%d\t",j,card);
> if(*(resti+j)==1)


use array notation

> {
> ptr2Set=realloc(ptr2Set,++card);


2nd parameter wrong - use ++card*sizeof(*ptr2Set) instead.

always store the return value in a temporary variable to check if it's NULL - upon failure, the original block remains unchanged.

that if statement looks odd - why should the decision whether to realloc the block depend on anything apart from the index being addressed, and the current size of the allocated block?

> if(ptr2Set==NULL)
> {
> printf("Allocazione di memoria non riuscita\n");
> free(ptr2Set);


pointless

> exit(1);
> }
> *(ptr2Set+card-1)=*(set+j);


use array notation

> }
> }
> free(resti);
> (ptrPDS+i)->elementi=card;
> (ptrPDS+i)->ptrSet=ptr2Set;


use array notation

> printf("i=%d\telementi=%d\tptrSet=%p\n",i,card,ptr 2Set);
> }
> // Stampa elementi sottoinsieme.
> for(i=0;i<m;i++)
> {
> ptr2Set=(ptrPDS+i)->ptrSet;
> printf("S%d={",i);
> card=(ptrPDS+i)->elementi;


use array notation

> if (card==0) printf("}\n");
> else
> {
> for(j=0;j<card-1;j++) printf("%d,",*(ptr2Set+j));
> printf("%d}\n",*(ptr2Set+j));


use array notation

indent/bracket consistently

> }
> printf("\n");
>
> }
> free(ptrPDS);
> free(ptr2Set);
> free(set);
> return 0;
> }
> // Funzione per il calcolo della potenza di un numero intero n ad un
> esponente
> int pot(int base,int esponente)
> {
> int p=1;
> int i=0;
> while(i++<esponente)
> p=p*base;
> return p;
> }
> // Stampa dei resti
> void stamparesti( int *resti, int n)
> {
> int j;
> for(j=0;j<n;j++)
> {
> printf("\t%d",*(resti+j));


use array notation

> }
> printf("\n");
> }
>
> I use Open Suse Linux 12.2, gcc compiler 4.7.1
>
>
> Francesco



Phil

--
I'm not saying that google groups censors my posts, but there's a strong link
between me saying "google groups sucks" in articles, and them disappearing.

Oh - I guess I might be saying that google groups censors my posts.
 
Reply With Quote
 
James Harris
Guest
Posts: n/a
 
      12-22-2012
On Dec 22, 6:09*pm, francesco <(E-Mail Removed)> wrote:
> I get this strange error message
>
> subsets.exe: malloc.c:2451: sYSMALLOc: Assertion `(old_top == (((mbinptr)


An assertion failure in malloc suggests you have previously
overwritten one or more of its internal data structures. They are
often placed between the spaces malloc/realloc gives to you and should
not be touched.

In your code I see

*(ptr2Set+card-1)=*(set+j);

Is this right? It looks like card starts out as zero so this could
refer to the memory before your allocated area that you should not
touch. This may be it - or there may be others.

James
 
Reply With Quote
 
francesco
Guest
Posts: n/a
 
      12-23-2012

>> {
>> ptr2Set=realloc(ptr2Set,++card);

>
> 2nd parameter wrong - use ++card*sizeof(*ptr2Set) instead.


yes, it fixed the problem. I have already fixed this sort of problems,
but now I wasn't able to see the solution

>
> always store the return value in a temporary variable to check if it's
> NULL - upon failure, the original block remains unchanged.


Ok
>
> that if statement looks odd - why should the decision whether to realloc
> the block depend on anything apart from the index being addressed, and
> the current size of the allocated block?

Do you mean this uf statement ?

if(*(resti+j)==1)

I need to create sets of variable size, and the size depends upon this
test, if rest==1 I need to add an item to the set, else I don't.

{
ptr2Set=realloc(ptr2Set,++card*sizeof(int));
if(ptr2Set==NULL)
{
printf("Allocazione di memoria non riuscita\n");
free(ptr2Set);
exit(1);
}
*(ptr2Set+card-1)=*(set+j);
}
 
Reply With Quote
 
francesco
Guest
Posts: n/a
 
      12-23-2012
Il Sat, 22 Dec 2012 15:45:33 -0500, Eric Sosman ha scritto:

> On 12/22/2012 2:21 PM, francesco wrote:
>> [...]
>> ptr2Set=realloc(ptr2Set,++card);

>
> ptr2set = realloc(ptr2set, ++card * sizeof(int));


yes, this fixed the program, it is always a subtle error
>
> Better,
>
> ptr2set = realloc(ptr2set, ++card * sizeof *ptr2set);
>
> Other improvements are possible, and there may be other
> problems. Start by fixing this one, and see how far you get.


Ok, I fixed that subtle error and the program works now.

Francesco
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AVG Email Scanner activating at strange times with strange IP addresses dennispublic@hotmail.com Computer Support 1 08-26-2006 04:27 AM
Strange memory access error after calling dll Mongoose7 C++ 2 03-08-2006 05:31 PM
Strange error of memory sylsau C Programming 9 11-22-2005 05:17 PM
Differences between Sony Memory Stick & memory Stick Pro vs Memory Stick Duo? zxcvar Digital Photography 3 11-28-2004 10:48 PM
Question About Strange 'C' Code Syntax ( Well strange to me anyway ) Harvey Twyman C Programming 8 10-25-2003 05:54 AM



Advertisments