Can anyone explain why I'm getting this error and how can I rectify it?
#include<stdio.h>
#include<stdlib.h>
char makemeunique(char *s,int l)
{
char *xp=(char *)malloc(l*sizeof(char));
int *pp=xp;
for(int i=1;i<l; i )
{
int yes=0;
for(int j=i-1;j>=0; j--)
{
if(s[i]==s[j])
yes=1;
}
if(yes-1)
*pp =s[i];
}
*pp='\0';
printf("%s\n",xp);
return xp;
}
Main function
int main()
{
char s[9999],x [9999];
scanf("%s\n%s",s,x);
char *p1, *p2;
p1=makemeunique(s, strlen(s));
p2=makemeunique(x, strlen(x));
}
My output:
Hello: malloc.c:2385: sysmalloc: Assertion (old_top == initial_top (av) && old_size == 0) || ((uns igned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pa gesize - 1)) == 0)' failed. Aborted (core dumped)
What is the meaning of this output??
This program simply gets two strings and calls the function and stores created heap array in the pointer.
This is my program and output:
CodePudding user response:
You create wrong pointer type at :
int *pp=xp;
pp should be char* : each time you do *pp you add sizeof(int) instead of sizeof(char) : result is you go beyond allocated memory to xp and this can result in usual C undefined behaviour.