[英]Segmentation fault (core dumped) in dynamic memory allocation using a pointer(member) from struct
[英]Segmentation fault (core dumped) error in dynamic memory allocation
我是編碼新手,也是 C++ 新手。 我正在嘗試編寫一個輸入矩陣元素的代碼。 之后,應刪除一列,並添加一行。 刪除一列工作正常。 但是,在為新的行數重新分配 memory 后,我收到消息:分段錯誤(核心轉儲)。 我正在使用指針來創建和編輯我的矩陣。 這是我的代碼。 謝謝你的幫助!
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c,r,**p,column,row;
printf("Enter the number of rows and columns:\n");
scanf("%d\n%d",&r,&c);
p=(int**)calloc(r,sizeof(int*));
if(p==NULL) printf("Memory not allocated.\n");
else{
for(int i=0;i<r;i++){
*(p+i)=(int*)calloc(c,sizeof(int));
printf("Enter %d. row\n",i+1);
for(int j=0;j<c;j++){
scanf("%d",*(p+i)+j);
}
}
printf("Original matrix:\n");
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
printf("%d ",*(*(p+i)+j));
}
printf("\n");
}
printf("Which column do you want to remove?");
scanf("%d",&column);
while(column<1||column>c){
printf("Wrong entry, enter again:");
scanf("%d",&column);
}
for(int i=0;i<=r-1;i++){
for(int j=column-1;j<=c-2;j++)
*(*(p+i)+j)=*(*(p+i)+j+1);
*(p+i)=(int*)realloc(*(p+i),(c-1)*sizeof(int));
}
printf("Matrix without %d. column:\n",column);
for(int i=0;i<r;i++){
for(int j=0;j<c-1;j++)
printf("%d ",*(*(p+i)+j));
printf("\n");
}
printf("Which row do you want to replace?\n");
scanf("%d",&row);
while(row<1||row>r){
printf("Wrong entry, enter again:\n");
scanf("%d",&row);
}
p=(int**)realloc(p,(r+1)*sizeof(int*));
if(p==NULL)
printf("Memory not allocated.\n");
else{
printf("Enter %d. row",row);
for(int i=r+1;i>row-1;i++){
*(p+i)=*(p+i-1);
}
for(int j=0;j<c-2;j++)
scanf("%d",*(p+row-1)+j);
printf("New matrix:\n");
for(int i=0;i<=r;i++){
for(int j=0;j<c-2;j++)
printf("%d ",*(*(p+i)+j));
printf("\n");
}
}
}
return 0;
}
當您為p
重新分配 memory 時,您會向該數組添加一個新行(一個額外的指針)。 但是您永遠不會為該新行分配 memory,最終您訪問了這個未初始化的指針。
但這不是真正的問題。 在循環中,您要向上提升指針以為插入的行騰出空間,您的初始值和循環增量都是錯誤的。 使用int i=r+1
的初始值,第一次寫入*(p+i)
將訪問已分配空間(即p[0]
到p[r]
)之后的一個空間。 循環增量應該是減量, --i
。 完成此循環后,您可以為新行分配空間。
正如這里的其他人所說,使用 std::vector 絕對更容易做到這一點。
但是,我假設您將其作為學習練習,因此我絕對建議您完成此操作並了解您出錯的地方。 你很接近,看起來你只是對你的索引感到困惑。 嘗試使用更具描述性的變量名稱並將內容拆分為函數,以使其更難迷失在自己的代碼中。
我修改了你的代碼,讓它做我認為你想做的事情。 看看,讓我知道這是否有幫助。
但是,是的,如果你將來做任何 C++ 工作,請避免做 C 風格的 malloc、alloc、free 等......並考慮使用標准庫。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c, r, ** p, column, row;
printf("Enter the number of rows and columns:\n");
scanf_s("%d\n%d", &r, &c);
p = (int**)calloc(r, sizeof(int*));
if (p == NULL) printf("Memory not allocated.\n");
else {
for (int i = 0; i < r; i++) {
*(p + i) = (int*)calloc(c, sizeof(int));
printf("Enter %d. row\n", i + 1);
for (int j = 0; j < c; j++) {
scanf_s("%d", *(p + i) + j);
}
}
printf("Original matrix:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("%d ", *(*(p + i) + j));
}
printf("\n");
}
printf("Which column do you want to remove?");
scanf_s("%d", &column);
while (column<1 || column>c) {
printf("Wrong entry, enter again:");
scanf_s("%d", &column);
}
for (int i = 0; i <= r - 1; i++) {
for (int j = column - 1; j <= c - 2; j++)
* (*(p + i) + j) = *(*(p + i) + j + 1);
*(p + i) = (int*)realloc(*(p + i), (c - 1) * sizeof(int));
}
c -= 1;
printf("Matrix without %d. column:\n", column);
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++)
printf("%d ", *(*(p + i) + j));
printf("\n");
}
printf("Which row do you want to replace?\n");
scanf_s("%d", &row);
while (row<1 || row>r) {
printf("Wrong entry, enter again:\n");
scanf_s("%d", &row);
}
p = (int**)realloc(p, (r + 1) * sizeof(int*));
if (p == NULL)
printf("Memory not allocated.\n");
else {
printf("Enter %d. row", row);
for (int i = 0; i < c; i++)
scanf_s("%d", *(p + row -1) + i);
printf("New matrix:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++)
printf("%d ", *(*(p + i) + j));
printf("\n");
}
}
}
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.