首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >分割错误: 11次打印出循环

分割错误: 11次打印出循环
EN

Stack Overflow用户
提问于 2013-10-14 11:14:15
回答 2查看 1.1K关注 0票数 1

首先,我说我一直在网络上寻找信息,用gdb调试和.什么.我仍然不明白错误,我认为它可能来自"getline“指令,但我不确定.

代码的主要思想是逐行读取字符字符串,将字符字符串转换为浮点数,并将浮点数保存在一个名为nfloat的数组中,然后调用函数:*create_table*来创建类型向量的指针数组。

输入是包含以下内容的.txt:n=字符串数,在本例中为n=3

代码语言:javascript
复制
3
[9.3,1.2,87.9]
[1.0,1.0]
[0.0,0.0,1.0]

第一个数字,3是我们在图像中看到的向量数,但是这个数字不是静态的,输入可以是57等等,而不是3

到目前为止,我已经开始执行以下操作,但是代码有一些内存错误,我认为:

代码语言:javascript
复制
#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 :(!! }

问题就在打印指令中,我认为,当我在循环内部打印时,一切正常,但是当我尝试在for循环之外执行同样的操作时,它会打印分段故障error...might是内存泄漏吗?

对于我来说,重要的是知道*vn是否得到了良好的实现,并很好地存储了这些信息,以便继续基于*vn信息创建函数。

请有人帮我了解一下,当我打印出循环时,问题在哪里?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-14 11:17:16

代码语言:javascript
复制
vector *pointvector;
pointvector = &newvect;
return(pointvector);

返回指向局部变量的指针。这是不正确的,需要通过为newvect分配动态内存,或者在函数中使用static变量,然后复制数据(数据不会在两个调用之间持久化)来更改。

编辑:根据请求,使用动态分配的示例:

代码语言:javascript
复制
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;
}

当然,在某个时候,您需要同时释放数据和向量本身。

票数 3
EN

Stack Overflow用户

发布于 2013-10-14 11:29:45

在这一行

代码语言:javascript
复制
printf("%f\n", v[i]->data[1]); //segmentation fault:11 :(!! }

i等于n。当v被声明为v[n]时,上面的行通过访问超出界限的v来触发未定义的行为。

要解决将地址返回局部变量的问题,请执行以下操作:

代码语言:javascript
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19358917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档