在 C 中为动态二维数组赋值时出现分段错误

发布于 2024-10-28 09:56:33 字数 1903 浏览 1 评论 0原文

您好,我正在用 C 编写一个程序,但是在运行我的程序时遇到分段错误。

我使用 gcc 进行编译,编译时没有给出警告或错误。

我尝试使用 gdb 来跟踪段错误的起源,它引导我到将数据值分配给二维数组的行: 数组[行][列] = 数据值;

当我运行我的程序时,会存储 3 个数据值,然后存储段错误。它应该存储 424 行 x 117 列的数据,但在仅存储 3 个数据值后始终出现段错误。

我的代码如下(省略了一些细节):

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void allmem(float** fpdata,...);            // allocate memory header
void stored(float** fpdata,...);            // store data header
void countnumber(int* num1, int*num2,...);  // count header

int main()   // main() function
{
int numberofrows = 424; // number of rows
float** fpdata;         // two dimensional array fpdata of floats

allmem(fpdata,...);     // call allocate memory function
stored(fpdata,...);     // call function to store data
...
return 0;
} // main ()

// --------------stored() function---------------
void stored(float** fpreal,...) {

FILE *fp;                  // file pointer
float datavalue;           // variable to hold data values read
int row;
int column;

fp = fopen("c:/file.txt","r");

for(column = 0; column < 117; column++) {
  for(row = 0; row < 424; row++) {
    fscanf(fp, "%f,", &datavalue);
    fpdata[row][column] = datavalue;
  } // for
} // for  
fclose(fp);
} // stored()

// ----------allmem() function----------------
// function to allocate memory for two dimensional arrays
// I have hard coded the values of the array sizes in, but
// in my actual program they are calculated at run time based
// on the size of some input files and this is done in the 
// countnumber() function

void allmem(float** fpdata,...) {
int i = 0;
fpdata = (float**) malloc((424)*sizeof(float*));
fpdata2 = (float**) malloc((424)*sizeof(float*));
...
for (i = 0; i<424; i++) {
  fpdata[i] = (float*) malloc((117)*sizeof(float));
  fpdata2[i] = (float*) malloc((117)*sizeof(float));
} // for

} // allmem()

Hi I am writing a program in C, however I am getting a segmentation fault upon running my program.

I am using gcc to compile and no warnings or errors are given at compilation time.

I tried using gdb to trace the origin of the segfault and it directs me to the line where I am assigning data values to my two dimensional array:
array[row][column] = datavalue;

When I run my program stores 3 data values and then seg faults. It should store data on 424 rows by 117 columns, but consistently it seg faults after only storing 3 data values.

My code is as follows (with some details left out):

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void allmem(float** fpdata,...);            // allocate memory header
void stored(float** fpdata,...);            // store data header
void countnumber(int* num1, int*num2,...);  // count header

int main()   // main() function
{
int numberofrows = 424; // number of rows
float** fpdata;         // two dimensional array fpdata of floats

allmem(fpdata,...);     // call allocate memory function
stored(fpdata,...);     // call function to store data
...
return 0;
} // main ()

// --------------stored() function---------------
void stored(float** fpreal,...) {

FILE *fp;                  // file pointer
float datavalue;           // variable to hold data values read
int row;
int column;

fp = fopen("c:/file.txt","r");

for(column = 0; column < 117; column++) {
  for(row = 0; row < 424; row++) {
    fscanf(fp, "%f,", &datavalue);
    fpdata[row][column] = datavalue;
  } // for
} // for  
fclose(fp);
} // stored()

// ----------allmem() function----------------
// function to allocate memory for two dimensional arrays
// I have hard coded the values of the array sizes in, but
// in my actual program they are calculated at run time based
// on the size of some input files and this is done in the 
// countnumber() function

void allmem(float** fpdata,...) {
int i = 0;
fpdata = (float**) malloc((424)*sizeof(float*));
fpdata2 = (float**) malloc((424)*sizeof(float*));
...
for (i = 0; i<424; i++) {
  fpdata[i] = (float*) malloc((117)*sizeof(float));
  fpdata2[i] = (float*) malloc((117)*sizeof(float));
} // for

} // allmem()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花桑 2024-11-04 09:56:33

fpdata 通过值传递,而不是通过指针或引用传递。这意味着当函数从 allmem 返回时,fpdata 仍然指向之前所做的相同操作,并且分配的内存会丢失。

您需要调用 allmem(&fpdata,...);

并使用 void allmem(float*** fpdata,...) 的函数签名

然后,在 allmem 中,设置 *fpdata = (float**)...

当然,(*fpdata)[i] = (float* ) malloc... 在“for”循环内。

编辑:

我希望你会对 fpdata2 做同样的事情。但是您不必更改 stored() 中的任何内容(尽管看起来您传入了 fpreal 但将值分配给 fpdata ,即可能只是一个简化的代码错误?)。传递给stored的指针应该是有效的。您并不是试图更改 stored 中的指针,只是更改它指向的内存中的值。

fpdata is passed by value instead of by pointer or by reference. That means that when the function returns from allmem, fpdata still points to the same thing it did before and the allocated memory is lost.

You'll want to call allmem(&fpdata,...);

And use a function signature of void allmem(float*** fpdata,...)

Then, in allmem, set *fpdata = (float**)...

And, of course, (*fpdata)[i] = (float*) malloc... inside the 'for' loop.

Edit:

I expect you'd do the same for fpdata2. But you shouldn't have to change anything in stored() (although it looks like you pass in fpreal but assign values to fpdata which is probably just a simplified code error?). The pointers passed to stored should be valid as they are. You're not trying to change the pointer in stored, just values in the memory that it points to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文