error free function and how to debug

  • Thread starter =?iso-8859-1?B?cG92b2Hn428=?=
  • Start date
?

=?iso-8859-1?B?cG92b2Hn428=?=

Hi all,

I need some help because I don't know what happens here.
At the code above at the lines

printf("before free");
free(people);
printf("after free");

At the free line I got error

"*** glibc detected *** double free or corruption (out): 0x0804a008 ***
before freeAborted"

I'm using gcc 4.0.3, ubuntu and I want to keep the use of pointer in
the program.
If possible I need some how-to debug using gcc and linux.

thanks



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

struct person
{
char name[20];
float value;
}*people, *p;

sort(int size)
{
int cont=0;
struct person temp[size];
for(cont=0;cont<=size;cont++)
{
if(people[cont].value<people[cont+1].value)
temp[cont]=p[cont];
people[cont]=people[cont+1];
people[cont+1]=temp[cont];
}
}

int main(void)
{
int a=0,size=0;
do
{
scanf("%d",&size);
people=malloc((size+1) * sizeof(struct person));
p=people;
for(a=0;a<size;a++)
{
scanf("%s",&p->name);
scanf("%f",&p->value);
p++;
}
sort(size);
p=people;
for(a=0;a<size;a++)
{
printf("%s\n",p->name);
printf("%g\n",p->value);
p++;
}
printf("before free");
free(people);
printf("after free");
}while(size>0);
}
 
M

Malcolm

povoação said:
Hi all,

I need some help because I don't know what happens here.
At the code above at the lines

printf("before free");
free(people);
printf("after free");

At the free line I got error

"*** glibc detected *** double free or corruption (out): 0x0804a008 ***
before freeAborted"

I'm using gcc 4.0.3, ubuntu and I want to keep the use of pointer in
the program.
If possible I need some how-to debug using gcc and linux.

thanks



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

struct person
{
char name[20];
float value;
}*people, *p;

sort(int size)
{
int cont=0;
struct person temp[size];
for(cont=0;cont<=size;cont++)
This looks like your bug. C arrays go from 0 to N-1, so this should be <
size.
{
if(people[cont].value<people[cont+1].value)
temp[cont]=p[cont];
people[cont]=people[cont+1];
people[cont+1]=temp[cont];
}
}

int main(void)
{
int a=0,size=0;
do
{
scanf("%d",&size);
people=malloc((size+1) * sizeof(struct person));
p=people;
for(a=0;a<size;a++)
{
scanf("%s",&p->name);
scanf("%f",&p->value);
p++;
}
sort(size);
p=people;
for(a=0;a<size;a++)
{
printf("%s\n",p->name);
printf("%g\n",p->value);
p++;
}
printf("before free");
free(people);
printf("after free");
}while(size>0);
}

gcc will detect corrupted memory at the free stage. Unfortunately you need
to trawl through the whole program to find out where the illegal access
occured. It is probably but not necessarily a write to the block passed to
free.
 
P

pete

Malcolm said:
povoação said:
Hi all,

I need some help because I don't know what happens here.
At the code above at the lines

printf("before free");
free(people);
printf("after free");

At the free line I got error

"*** glibc detected *** double free or corruption (out):
#include <stdio.h>
#include <stdlib.h>

struct person
{
char name[20];
float value;
}*people, *p;

sort(int size)
{
int cont=0;
struct person temp[size];
for(cont=0;cont<=size;cont++)
This looks like your bug. C arrays go from 0 to N-1, so this should be <
size.
{
if(people[cont].value<people[cont+1].value)
temp[cont]=p[cont];
people[cont]=people[cont+1];
people[cont+1]=temp[cont];
}
}

The function shown above, is not close to being able to sort.

void sort(int size)
{
int cont;
struct person *p, *first, *middle, temp;

p = people;
while (size-- > 1) {
first = middle = p + 1;
cont = size;
while (--cont != 0) {
++middle;
if (first -> value > middle -> value) {
first = middle;
}
}
if (p -> value > first -> value) {
temp = *p;
*p = *first;
*first = temp;
}
++p;
}
}
 
C

CBFalconer

povoação said:
I need some help because I don't know what happens here.
At the code above at the lines

printf("before free");
free(people);
printf("after free");

At the free line I got error

"*** glibc detected *** double free or corruption (out):
0x0804a008 *** before freeAborted"

I see no code above.

Please don't top-post. See the following links.

--
Some informative links:
< <http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top