[英]Short & Quick malloc memory access issue
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char **wordlist=(char **)malloc(sizeof(char*)*4);
for(int i=0;i<4;i++)
{
char *word=(char*)malloc(sizeof(char*)*20);;
scanf("%s",word);
wordlist[i]=word;
free(word);
}
for(int i=0;i<4;i++)
{
printf("at %d value is %s\n",i,wordlist[i]);
}
free(wordlist);
return 0;
}
So the issue is as follows: I can run this code as many times as I want, and when it reads back the array I get COMPLETELY random results when it comes to where things are stored. 所以问题如下:我可以根据需要多次运行此代码,当它读回数组时,我会得到完全随机的结果,当涉及到存储的位置。 Example: If input was "foo bar is great" it would output any combination of the following "value at 0 is bar value at 1 is bar value at 2 is great value at 3 is foo" 示例:如果输入为“foo bar is great”,则会输出以下任意组合:“0处的值是1处的条形值是2处的条形值是3处的值是很大的值是foo”
Part of a much larger program, but this is the concept i'm struggling to find a solution (or proper implementation) to. 一个更大的计划的一部分,但这是我正在努力寻找解决方案(或适当的实施)的概念。 :( I have searched google high and low, as well as this site, with no solution that works properly. Any help is appreciated! :(我搜索谷歌的高低,以及这个网站,没有正常工作的解决方案。任何帮助表示赞赏!
The program is wrong and has memory leaks. 该程序是错误的并且有内存泄漏。
For example in this statement 例如在本声明中
char *word=(char*)malloc(sizeof(char*)*20);;
^^^^^^^^^^^^^
there is used sizeof( char * )
instead of sizeof( char )
使用sizeof( char * )
而不是sizeof( char )
Then after these statements 然后在这些陈述后
wordlist[i]=word;
free(word);
wordlist[I]
will point to memory that was deleted. wordlist[I]
将指向已删除的内存。
As result the program has undefined behavior. 结果该程序具有未定义的行为。
What you need is something like the following 您需要的是以下内容
#include <stdio.h>
#include <stdlib.h>
#define N 4
#define M 20
int main( void )
{
char ( *wordlist )[M] = malloc( sizeof( char[N][M] ) );
for ( size_t i = 0; i < N; i++ )
{
scanf( "%19s", wordlist[i] );
}
for ( size_t i = 0; i < N; i++ )
{
printf( "at %zu value is %s\n", i, wordlist[i] );
}
free( wordlist );
return 0;
}
For example if to enter 例如,如果要输入
one two free four
then the output will look like 然后输出看起来像
at 0 value is one
at 1 value is two
at 2 value is free
at 3 value is four
Take into account that if your compiler supports variable length arrays then there is not necessary that the right-most dimension would be a constant. 考虑到如果您的编译器支持可变长度数组,则最右侧维度不必是常量。
Another approach is to allocate dynamically an array of pointers to first elements of one-dimensional character arrays. 另一种方法是动态地将指针数组分配给一维字符数组的第一个元素。 For example 例如
#include <stdio.h>
#include <stdlib.h>
#define N 4
#define M 20
int main( void )
{
char **wordlist = malloc( sizeof( char *[N] ) );
for ( size_t i = 0; i < N; i++ )
{
wordlist[i] = malloc( sizeof( char[M] ) );
scanf( "%19s", wordlist[i] );
}
for ( size_t i = 0; i < N; i++ )
{
printf( "at %zu value is %s\n", i, wordlist[i] );
}
for ( size_t i = 0; i < N; i++ ) free( wordlist[i] );
free( wordlist );
return 0;
}
The result will be the same as for the preceding program. 结果与前面的程序相同。 However in this case instead to allocate only one two-dimensional array there are allocated several one-dimensional arrays. 然而,在这种情况下,仅分配一个二维阵列,分配几个一维阵列。
You free each one of your words before actually printing it. 在实际打印之前,您可以释放每个单词。 What you need to do is in the end(that is after the priniting -could be in the printing loop) 您需要做的是最后(即在打印之后 - 可以在打印循环中)
for (i=0;i<4;i++)
free(wordlist[i]);
free(wordlist)
and remove free(word);
并删除free(word);
in the first loop 在第一个循环中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.