[英]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.