[英]Segmentation fault: 11 when printing out of a loop
首先要說的是,我一直在尋找網絡上的信息,使用gdb調試進行測試……什么也沒有...我仍然不理解該錯誤,我認為它可能來自“ getline”指令,但我不確定...
該代碼的主要思想是逐行讀取並將char字符串轉換為浮點數,然后將浮點數保存在名為nfloat
的數組中,然后調用函數:* create_table *以創建類型為vector的指針數組。
輸入是一個包含以下內容的.txt
: n =字符串數,在這種情況下, n = 3
3
[9.3,1.2,87.9]
[1.0,1.0]
[0.0,0.0,1.0]
第一個數字3
是我們在圖像中看到的向量數量,但是該數量不是靜態的,輸入可以是5
或7
,以此類推而不是3
。
到目前為止,我已經開始執行以下操作,但是代碼中存在一些我認為的內存錯誤:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
float* data;
int size;
} vector;
vector *create_vector(int n, float* comps){
vector newvect;
newvect.data = (float *) malloc(n*sizeof(float));
int i;
for(i = 0; i < n; i++) {
newvect.data[i] = comps[i];
printf("Newvec.data[%d] = %.1f\n", i, newvect.data[i]);
}
newvect.size = n;
vector *pointvector;
pointvector = &newvect;
return(pointvector);
}
int NumsVector(char *linea, ssize_t size){
int numsvector = 1;
int n;
for(n = 2; n<= size; n++){
if (linea[n] != '[' && linea[n] != ']'){
if(linea[n] == 44){
numsvector = numsvector + 1;
}
}
}
return numsvector;
}
int main(){
int n, i;
scanf("%d\n", &n);
vector *v[n];
for(i = 0; i<n; ++i) {
char *line = NULL;
size_t len = 0;
ssize_t read;
read = getline(&line,&len,stdin);
int numsvector = NumsVector(line, read);
float nfloat[numsvector];
int j = 0;
/* Replaces the end ] with a , */
line[strlen(line) - 1] = ',';
/* creates a new pointer, pointing after the first [ in the original string */
char *p = line + 1;
do
{
/* grabs up to the next comma as a float */
sscanf(p, "%f,", &nfloat[j]);
/* moves pointer forward to next comma */
while (*(p++) != ',');
}
while (++j < numsvector); /* stops when you've got the expected number */
v[i] = create_vector(numsvector, nfloat);
printf("%f\n", v[i]->data[1]); //prints ok :)!
free(line);
}
printf("%f\n", v[i]->data[1]); //segmentation fault:11 :(!! }
好吧,問題與我認為的printf指令有關,當我在循環內打印時,一切正常,但是當我嘗試在for循環中執行相同操作時,它會打印分段錯誤錯誤...可能是內存泄漏?
對於我來說,了解* v [n]是否得到了很好的實現並很好地存儲了信息非常重要,以便繼續基於* v [n]信息創建函數。
請問有人可以幫助我了解打印循環時問題出在哪里嗎?
vector *pointvector;
pointvector = &newvect;
return(pointvector);
您正在返回指向局部變量的指針。 這是不正確的,需要通過為newvect
分配動態內存或通過在函數內使用static
變量然后復制數據(兩次調用之間的數據不會持久)來更改。
編輯:根據要求,動態分配示例:
vector *create_vector(int n, float* comps){
vector *newvect = malloc(sizeof(*newvect));
newvect->data = malloc(n*sizeof(float));
memcpy(newvect->data, comps, sizeof(float) * n);
newvect->size = n;
return newvector;
}
當然,在某些時候,您將需要釋放數據和向量本身。
在這條線
printf("%f\n", v[i]->data[1]); //segmentation fault:11 :(!! }
i
等於n
。 當v
被聲明為v[n]
,上述行通過超出范圍訪問v
引發了未定義的行為。
要解決將地址返回到局部變量的問題,請執行以下操作:
vector * create_vector(size_t n, float * comps)
{
vector * newvect = malloc(sizeof(*newvect);
if (NULL != newvect)
{
return NULL;
}
newvect->data = malloc(n * sizeof(*newvect->data));
if (NULL != newvect->data)
{
free(newvect);
return NULL;
}
for(size_t i = 0; i < n; i++)
{
newvect->data[i] = comps[i];
printf("newvect->data[%zu] = %.1f\n", i, newvect->data[i]);
}
newvect->size = n;
return newvect;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.