-1

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: My program

Test case and output

Spark
  • 219
  • 2
  • 13
  • No I need to change – Spark Sep 20 '21 at 05:52
  • you store 19KB on the stack – Raildex Sep 20 '21 at 05:53
  • Check the return value of `scanf`. If it has failed for any reason then `strlen` calls may be operating on unintialised variables. It's basic good programming to do error checking of all functions. For further help please provide the exact input being used for testing. – kaylum Sep 20 '21 at 05:54
  • 2
    The length returned by `strlen` does *not* include the null-terminator. You must remember to add space for it when you allocate your memory. Otherwise the null-terminator will be written out of bounds of the allocated memory, and you will have *undefined behavior*. – Some programmer dude Sep 20 '21 at 05:56
  • yeah so I used *pp='\0'; – Spark Sep 20 '21 at 05:58
  • 1
    Yeah but you didn't `malloc` enough space for the terminating character if the output string is the same length as the input string. Needs to be `malloc(l+1);` – kaylum Sep 20 '21 at 05:59
  • Also there's [some discussion about casting the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) and the consensus is "no". – Some programmer dude Sep 20 '21 at 06:00
  • 1
    I also recommend you take this as an opportunity to learn how to use a *debugger* to catch crashes and similar events, and how to locate where in your code it happens, and how to examine variables at that point. – Some programmer dude Sep 20 '21 at 06:01
  • yeah i will change malloc to malloc(l+1) and check – Spark Sep 20 '21 at 06:02
  • but no change every after changing to malloc(l+1); – Spark Sep 20 '21 at 06:06
  • 2
    Why is `pp` an `int*` ? – John3136 Sep 20 '21 at 06:16
  • Bro wow Nice I change to char* and got my expected output – Spark Sep 20 '21 at 06:20
  • 1
    @John3136 Oh that's definitely a major problem! And one that the compiler should be able to catch and warn about. – Some programmer dude Sep 20 '21 at 06:20
  • Lesson of today: Enable extra warnings, and treat them as errors that *must* be fixed. – Some programmer dude Sep 20 '21 at 06:21
  • But still I need to know what is the meaning of that error – Spark Sep 20 '21 at 06:21
  • You are only dealing with plain text here. Please do not post pictures of plain text. Instead copy&paste it into your question. This also applies to input and output. You already have the code as text. There is absolutely no need to add it as image as well. – Gerhardh Sep 20 '21 at 07:02

1 Answers1

1

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.

Ptit Xav
  • 3,006
  • 2
  • 6
  • 15
  • Any idea what is the meaning of the error?? – Spark Sep 20 '21 at 06:23
  • As I wrote ´undefined behaviour ´ can be anything. You setup a memory place past the end of allocated memory then anything can happen. In this case, It is not necessary to understand what happens in malloc. If you want you may check it source code but that may be waste of time. – Ptit Xav Sep 20 '21 at 06:29
  • Yeah Now I got the point – Spark Sep 20 '21 at 06:33