4 Replies - 367 Views - Last Post: 17 January 2013 - 10:46 PM Rate Topic: -----

#1 The_afridi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • 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
Was This Post Helpful? 1
  • +
  • -

#3 The_afridi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.

Posted Image
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • 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
Was This Post Helpful? 1
  • +
  • -

#5 The_afridi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1