写入范围外的动态二维数组而没有Segmentation fault [英] Write to dynamic two dimension array outside the range without Segmentation fault
问题描述
此代码段是动态分配二维数组
的练习。虽然这个是微不足道的,但是它是正确的吗?
此外,当我试图对原来的
例子进行这些修改时:
for(i = 0; i< ROW + 2; ++ i){/ * line 14 * /
strcpy(array [i]," C!C!" ); / *第15行* /
显然,修改后的代码写入了
分配数组外的内存单元,但程序运行良好且没有分段
故障。谢谢。
#include< stdlib.h>
#include< string.h>
#include< ; stdio.h>
#define ROW 4
#define COL 3
int main(void) {
char(* array)[COL];
int i;
array = malloc(ROW * sizeof * array );
if(array){
for(i = 0; i< ROW; ++ i){/ * line 14 * /
strcpy(array [i]," C!"); / *第15行* /
printf("%s \ n",array [i]);
}
}
免费(数组);
返回0;
}
This code snippet is an exercise on allocating two dimension array
dynamically. Though this one is trivial, is it a correct one?
Furthermore, when I tried to make these changes to the original
example:
for (i = 0; i < ROW + 2; ++i){ /*line 14*/
strcpy(array[i], "C! C!"); /*line 15*/
Apparently, the modified code wrote to the memory unit outside the
allocated array, but the program ran well and there was no Segmentation
fault. Thank you.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define ROW 4
#define COL 3
int main(void){
char (*array)[COL];
int i;
array = malloc(ROW * sizeof *array);
if (array){
for (i = 0; i < ROW; ++i){ /*line 14*/
strcpy(array[i], "C!"); /*line 15*/
printf("%s\n", array[i]);
}
}
free(array);
return 0;
}
推荐答案
lovecreatesbea ... @ gmail.com写道:
lovecreatesbea...@gmail.com wrote:
strcpy(array [i ],C!; / *第15行* /
printf("%s \ n",array [i]);
strcpy(array[i], "C!"); /*line 15*/
printf("%s\n", array[i]);
strcpy没有终止,所以你对printf的调用会出错。
strcpy doesn''t nul terminate, so your call to printf will fault.
Op 3 Sep 2006 10:28:48 -0700 schreef bw*****@yahoo.com :
Op 3 Sep 2006 10:28:48 -0700 schreef bw*****@yahoo.com:
lovecreatesbea ... @ gmail.com写道:
lovecreatesbea...@gmail.com wrote:
> strcpy(array [i],C!); / *第15行* /
printf("%s \ n",array [i]);
> strcpy(array[i], "C!"); /*line 15*/
printf("%s\n", array[i]);
strcpy没有终止,所以你对printf的调用会出错。
strcpy doesn''t nul terminate, so your call to printf will fault.
strcpy没有,memcpy没有。
printf在这里不会失败。
- -
Coos
strcpy does, memcpy doesn''t.
printf won''t fail here.
--
Coos
bw ***** @ yahoo.com 写道:
lovecreatesbea ... @ gmail.com写道:
lovecreatesbea...@gmail.com wrote:
> strcpy(array [i],C!); / *第15行* /
printf("%s \ n",array [i]);
> strcpy(array[i], "C!"); /*line 15*/
printf("%s\n", array[i]);
strcpy没有终止,
strcpy doesn''t nul terminate,
究竟是什么让你这么想? strcpy是一个* string *副本,所以
当然它被定义为复制null终止,否则它将被称为
之类的东西,
CopyJustTooLittleOfAStringToBeUsefull99PercentOfTh eTimeOrMore。
What on earth makes you think that? strcpy is a *string* copy, so of
course it is defined to copy the null termination, otherwise it would be
called something like,
CopyJustTooLittleOfAStringToBeUsefull99PercentOfTh eTimeOrMore.
所以你对printf的调用会出错。
so your call to printf will fault.
即使你写的是strcpy(你不是),它仍然不会保证对printf的调用会出错。
-
Flash Gordon
Even if you were write about strcpy (which you are not) that would still
not guarantee that the call to printf would fault.
--
Flash Gordon
这篇关于写入范围外的动态二维数组而没有Segmentation fault的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!