# Logical error Problem! Pointers 2d array

Page 1 of 1

## 4 Replies - 813 Views - Last Post: 17 January 2013 - 10:46 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=305264&amp;s=5e66b7d3b8efa4a7f980b650a4d5b03b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 The_afridi

Reputation: 0
• Posts: 9
• Joined: 18-May 12

# Logical error Problem! Pointers 2d array

Posted 31 December 2012 - 03:48 AM

```#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void Union(int *a,int *b,int m,int n)
{
int *c,i,j,k=0,flag=0;
c=(int *)malloc(sizeof(int)*m);
for(i=0;i<m;i++)
{
c[k]=a[i];
k++;
}
for(i=0;i<n;i++)
{
flag=0;
for(j=0;j<m;j++)
{
if(b[i]==c[j])
{
flag=1;
break;
}
}
if(flag==0)
{
c[k]=b[i];
k++;
}
}

printf("\nElement of resultant set\n\n");

for(i=0;i<k;i++)
{
printf("\t%d",c[i]);
}
}

int main()
{
int *a,*b,m,n,i,j;
int ch;
//clrscr();
printf("\nEnter the number of elements in first set:\n");
scanf("%d",&m);
a=(int *)malloc(sizeof(int)*m);
printf("\nEnter the elements:\n");
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}

printf("\nElement of First set:\n\n");
for(i=0;i<m;i++)
{
printf("%d\t",a[i]);
}

printf("\nEnter the number of elements in second set:\n");
scanf("%d",&n);
b=(int *)malloc(sizeof(int)*n);

printf("\nEnter the elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
printf("\nElement of second set\n");
for(i=0;i<n;i++)
{
printf("%d\t",b[i]);
}
Union(a,b,m,n);

for(i=0;i<m;i++)
free(a++);

for(i=0;i<m;i++)
free(b++);

free(a);
free(B)/>;
return 0;
getchar();
}

```

Is This A Good Question/Topic? 0

## Replies To: Logical error Problem! Pointers 2d array

### #2 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: Logical error Problem! Pointers 2d array

Posted 31 December 2012 - 04:15 AM

Well, when you say 'logical error'...what do you mean by it...is it something to do with erroneous output?

It would certainly help if you could post a sample of an input-output pair so that we can see where exactly your program is going wrong.

regards,
Raghav

### #3 The_afridi

Reputation: 0
• Posts: 9
• Joined: 18-May 12

## Re: Logical error Problem! Pointers 2d array

Posted 31 December 2012 - 06:05 AM

Sir here is the screen shot of the output of the program it works but at the end it shows this kind of error which is shown below in the picture.

### #4 jimblumberg

Reputation: 4912
• Posts: 15,349
• Joined: 25-December 09

## Re: Logical error Problem! Pointers 2d array

Posted 31 December 2012 - 06:38 AM

First find an indentation style you like and use it consistently, it will make reading your program much easier.

Your problem is that you are misusing the free() function in the following snippet:
```   for(i=0; i<m; i++)
free(a++);

for(i=0; i<m; i++)
free(b++);

free(a);
free(B)/>;

```

First how many times do you think you need to free memory you allocate with malloc?

Next why are you using the increment operator on a freed pointer?

You don't need either of these loops so just get rid of them. Also you are not freeing the memory you allocated in your function.

Jim

### #5 The_afridi

Reputation: 0
• Posts: 9
• Joined: 18-May 12

## Re: Logical error Problem! Pointers 2d array

Posted 17 January 2013 - 10:46 PM

Thank you sir for giving your time.