मैंने अपने कार्यक्रम में त्रुटि देखी है और एक साधारण लिखने का फैसला किया है, जो मुझे समझने में मदद करेगा कि क्या हो रहा है। संदेश यह है:munmap_chunk(): अमान्य सूचक
#include <stdio.h>
#include <stdlib.h>
char * first()
{
char * word = malloc(sizeof(char) * 10);
word[0] = 'a';
word[1] = 'b';
word[2] = '\0';
return word;
}
char * second()
{
char * word = malloc(sizeof(char) * 10);
word = "ab";
return word;
}
int main()
{
char * out = first();
printf("%s", out);
free(out);
out = second();
printf("%s", out);
free(out);
return 0;
}
first()
समारोह ठीक से काम कर रहा है, लेकिन second()
(वास्तव में free(out)
) genarates त्रुटि:
Error in `./a.out': munmap_chunk(): invalid pointer: 0x0000000000400714 *** ababAborted (core dumped)
मुझे समझ नहीं आता क्यों पहले समारोह सही है, लेकिन दूसरा नहीं है। क्या कोई बता सकता है क्यों?
आप सीधे एक सरणी में एक स्ट्रिंग असाइन नहीं कर सकते हैं, 'strcpy (word, "ab")' का उपयोग करें। – gengisdave