我用C编程语言编写了此代码,并在下面进行了编译。
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define LENGTH 100
int main()
{
char *Texts = (char*) malloc (LENGTH * sizeof (char));
for (int i = 0; i < 10; i++)
{
printf("Enter your string number %hd : ", i + 1);
gets(Texts + ((i * LENGTH));
system("cls");
Texts = realloc(Texts, (i + 2) * LENGTH * sizeof (char));
}
for (int i = 0; i < 10; i++)
{
printf("String number %hd : %s", i + 1, (Texts + (i * LENGTH)));
printf("-------------------------------------------------------");
}
return 0;
}
当我编译并运行它时,在程序获得输入并显示文本后,它给出了未知的输出。我猜这个问题来自(realloc())函数。但是我不认为。你怎么看?你能帮我吗?
编写减少内存代码的更好方法是
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 100
int main()
{
char *Texts=NULL;
int len=0;
const int n=5;
for (int i = 0; i < n ;i++)
{
Texts = realloc(Texts,(len+(LENGTH*sizeof(char)))); //Reallocating for Next string with 100 chars
if(!Texts) //Checking the memory is allocated or not
{
printf("Memory Error");
exit(1);
}
printf("Enter your string number %d : ", i + 1);
fgets ( (Texts+len) ,100,stdin); //Here len is length of string including NULL character
len+=strlen(Texts+len); //Calculating length of string (lastly entered only)
Texts[len-1]='\0'; //Replace '\n' as '\0'
system("cls");
}
Texts=realloc(Texts,len); //To clear unused memory (lastly allocated)
if(!Texts)
{
printf("Memory Error");
exit(1);
}
for (int i = 0,l=0; i < n; i++,l=(strlen(Texts)+1))
{
printf("String number %hd : %s", i + 1,Texts+l);
printf("\n-------------------------------------------------------\n");
}
printf("\nMemory Map:\n\n"); //Memory Map (for understanding only)
printf(" =======================================");
for(int i=0;i<len;i++)
{
if(Texts[i]=='\0')
printf("\n|\t%p\t|\t\\0\t|\n",&Texts[i]);
else
printf("\n|\t%p\t|\t%c\t|\n",&Texts[i],Texts[i]);
printf(" =======================================");
}
free(Texts); //Freeing The Memory
return 0;
}
输出:(输入为Hai1 Hai2 Hai3 Hai4 Hai5)
String number 1 : Hai1
-------------------------------------------------------
String number 2 : Hai2
-------------------------------------------------------
String number 3 : Hai2
-------------------------------------------------------
String number 4 : Hai2
-------------------------------------------------------
String number 5 : Hai2
-------------------------------------------------------
Memory Map:
=======================================
| 00D931B8 | H |
=======================================
| 00D931B9 | a |
=======================================
| 00D931BA | i |
=======================================
| 00D931BB | 1 |
=======================================
| 00D931BC | \0 |
=======================================
| 00D931BD | H |
=======================================
| 00D931BE | a |
=======================================
| 00D931BF | i |
=======================================
| 00D931C0 | 2 |
=======================================
| 00D931C1 | \0 |
=======================================
| 00D931C2 | H |
=======================================
| 00D931C3 | a |
=======================================
| 00D931C4 | i |
=======================================
| 00D931C5 | 3 |
=======================================
| 00D931C6 | \0 |
=======================================
| 00D931C7 | H |
=======================================
| 00D931C8 | a |
=======================================
| 00D931C9 | i |
=======================================
| 00D931CA | 4 |
=======================================
| 00D931CB | \0 |
=======================================
| 00D931CC | H |
=======================================
| 00D931CD | a |
=======================================
| 00D931CE | i |
=======================================
| 00D931CF | 5 |
=======================================
| 00D931D0 | \0 |
=======================================
Process returned 0 (0x0) execution time : 18.236 s
Press any key to continue.
但是在您的代码中,每个字符串都有100个字节。即使它是否被占用。
注意:realloc(NULL,size)等于malloc(size)。