我以为是内存入侵问题,是吗?
I thought there is a memory intrusion problem, is that right?
如下,我的代码是:
int *text(char *str)
{
int *cond;
int *temp;
int cond_size;
int num;
int i;
cond_size = -1;
cond = malloc(sizeof(int) * 1);
*cond = 0;
while (*str != '[=10=]')
{
if (*str == ' ')
str++;
num = 0;
while (*str >= '0' && *str <= '9')
num = num * 10 + *(str++) - '0';
temp = cond;
cond = malloc(sizeof(int) * (++cond_size));
i = -1;
while (++i < cond_size)
cond[i] = temp[i];
cond[i] = num;
free(temp);
}
g_size = (i + 1) / 4;
return (cond);
}
我的主要功能是:
int *text(char *str);
#include <stdio.h>
#include <stdlib.h>
int g_size = 0;
int main(void)
{
int *test;
int i;
i = 0;
test = text(" 4 3 2 1 1 2 2 2 4 3 2 1 1 2 2 2");
while(i < g_size)
{
printf("\n%d\n", test[i]);
i++;
}
}
使用输入字符串 4 3 2 1 1 2 2 2 4 3 2 1 ...
,将打印以下输出:
==============
| 4 3 2 1 |
|4 '1'| <==
|3 2 |
|2 2 |
|1 2 |
| 1 2 2 2 |
==============
但是,正如我检查的那样 (<==),
'1' 打印为 '4'、
和我预期的不一样。
是不是我的代码在使用malloc时有内存入侵或者其他错误?
我能看到一些问题:
cond_size 是-1:
cond_size = -1; // Here is problem
cond = malloc(sizeof(int) * 1);
*cond = 0;
while (*str != '[=10=]')
{
if (*str == ' ')
str++;
num = 0;
while (*str >= '0' && *str <= '9')
num = num * 10 + *(str++) - '0';
temp = cond;
cond = malloc(sizeof(int) * (++cond_size)); // you are trying allocate memory with size 0
i = -1;
while (++i < cond_size)
cond[i] = temp[i];
cond[i] = num; // you are writing to not allocated memory
您可以找到有关 malloc 的更多信息。
温度超出范围:
temp = cond;
cond = malloc(sizeof(int) * (++cond_size));
i = -1;
while (++i < cond_size)
cond[i] = temp[i]; // temp size is cond_size-1
所以你应该限制循环 cond_size-1.
<script src="//onlinegdb.com/embed/js/SJADLQEM8?theme=dark"></script>
如下,我的代码是:
int *text(char *str)
{
int *cond;
int *temp;
int cond_size;
int num;
int i;
cond_size = -1;
cond = malloc(sizeof(int) * 1);
*cond = 0;
while (*str != '[=10=]')
{
if (*str == ' ')
str++;
num = 0;
while (*str >= '0' && *str <= '9')
num = num * 10 + *(str++) - '0';
temp = cond;
cond = malloc(sizeof(int) * (++cond_size));
i = -1;
while (++i < cond_size)
cond[i] = temp[i];
cond[i] = num;
free(temp);
}
g_size = (i + 1) / 4;
return (cond);
}
我的主要功能是:
int *text(char *str);
#include <stdio.h>
#include <stdlib.h>
int g_size = 0;
int main(void)
{
int *test;
int i;
i = 0;
test = text(" 4 3 2 1 1 2 2 2 4 3 2 1 1 2 2 2");
while(i < g_size)
{
printf("\n%d\n", test[i]);
i++;
}
}
使用输入字符串 4 3 2 1 1 2 2 2 4 3 2 1 ...
,将打印以下输出:
==============
| 4 3 2 1 |
|4 '1'| <==
|3 2 |
|2 2 |
|1 2 |
| 1 2 2 2 |
==============
但是,正如我检查的那样 (<==),
'1' 打印为 '4'、
和我预期的不一样。
是不是我的代码在使用malloc时有内存入侵或者其他错误?
我能看到一些问题:
cond_size 是-1:
cond_size = -1; // Here is problem cond = malloc(sizeof(int) * 1); *cond = 0; while (*str != '[=10=]') { if (*str == ' ') str++; num = 0; while (*str >= '0' && *str <= '9') num = num * 10 + *(str++) - '0'; temp = cond; cond = malloc(sizeof(int) * (++cond_size)); // you are trying allocate memory with size 0 i = -1; while (++i < cond_size) cond[i] = temp[i]; cond[i] = num; // you are writing to not allocated memory
您可以找到有关 malloc 的更多信息。
温度超出范围:
temp = cond; cond = malloc(sizeof(int) * (++cond_size)); i = -1; while (++i < cond_size) cond[i] = temp[i]; // temp size is cond_size-1
所以你应该限制循环 cond_size-1.
<script src="//onlinegdb.com/embed/js/SJADLQEM8?theme=dark"></script>