如何将相等的内存分配给输入文件读取的变量?
我正在构建一个代码,以模拟我的博士学位的某些动态系统的响应。基本上,我要做的是:
- 向用户咨询包含模拟参数的输入文件的名称;
- 读取输入文件,并将每个参数分配给我程序中的特定变量。
- 进行计算。
我在步骤2上遇到问题,因为输入文件中的值之一是dinamatial分配的数组*x
的dimension dim
。
这是代码的工作示例:
#include <stdio.h>
#include <stdlib.h>
void read_file(char *name, int *dim, double **x) {
// Open input file
FILE *input = fopen(name, "r");
if (input == NULL) {
// Return error
perror(name);
return;
}
// Read and assign system constants
fscanf(input, "%i", dim);
printf("dim = %i\n", *dim);
// Allocate memory for x[dim]
*x = malloc((*dim) * sizeof(*x));
double y[(*dim)];
printf("size of (*x) = %llu\n", sizeof(*x));
printf("size of y = %llu\n", sizeof(y));
// Security check for pointers
if(*x == NULL) {
free(*x);
printf("Memory allocation for *x did not complete successfully\n");
return;
}
// assign values to x[dim] vector
for (int i = 0; i < *dim; i++) {
fscanf(input, "%lf", &(*x)[i]);
printf("(*x)[%i] = %lf\n", i, (*x)[i]);
}
// Close input file
fclose(input);
}
char *get_input_filename(void) {
char *filename = malloc(200 * sizeof(*filename));
printf("Enter Input Filename: ");
scanf("%s", filename);
return filename;
}
int main (void) {
int DIM;
double *x = NULL;
char *input_filename = get_input_filename();
read_file(input_filename, &DIM, &x);
printf("size of (*x) = %llu\n", sizeof(*x));
for (int i = 0; i < DIM; i++) {
printf("(*x)[%i] = %lf\n", i, x[i]);
}
free(x); free(input_filename);
}
这是输入文件in.txt
的内容:
5
0.0 0.1 0.2 1.4 2.6
当我调用*x = malloc((*dim)*sizeof((*dim)* *x));
我希望找到系统的维度为5 *8 Bytes
,因为*DIM
的值是在上一行中分配的但是,仅分配8个字节
。然后,我声明了y [(*dim)]
变量以检查sizeof
a vla
的行为与<代码> sizeof *x
,只是为了比较。 sizeof(y)
是我所期望的,但是sizeof(*x)
并不是,如输出所示:
Enter Input Filename: in.txt
dim = 5
size of (*x) = 8
size of y = 40
(*x)[0] = 0.000000
(*x)[1] = 0.100000
(*x)[2] = 0.200000
(*x)[3] = 1.400000
(*x)[4] = 2.600000
size of (*x) = 8
(*x)[0] = 0.000000
(*x)[1] = 0.100000
(*x)[2] = 0.200000
(*x)[3] = 1.400000
(*x)[4] = 2.600000
我知道如果它不能分配内存,如果的值*DIM
是未知的,但是上一行中分配了一个值。
另外,我什至不知道该程序如何成功地将值分配给(*x)
,因为它没有必要的字节分配来执行此操作。
我在这里想念什么?如何正确分配内存?
提前致谢
I'm building a code to simulate the response of some dynamical systems for my PhD. Basically what I'm trying to do is:
- Ask the user for the name of a input file that contains all parameters for the simulation;
- Read the input file and assign each parameter to a specific variable in my program.
- Do the calculations.
I'm having problems on step 2, as one of the values in the input file is the dimension dim
of an dinamically allocated array *x
.
Here is a working example of the code:
#include <stdio.h>
#include <stdlib.h>
void read_file(char *name, int *dim, double **x) {
// Open input file
FILE *input = fopen(name, "r");
if (input == NULL) {
// Return error
perror(name);
return;
}
// Read and assign system constants
fscanf(input, "%i", dim);
printf("dim = %i\n", *dim);
// Allocate memory for x[dim]
*x = malloc((*dim) * sizeof(*x));
double y[(*dim)];
printf("size of (*x) = %llu\n", sizeof(*x));
printf("size of y = %llu\n", sizeof(y));
// Security check for pointers
if(*x == NULL) {
free(*x);
printf("Memory allocation for *x did not complete successfully\n");
return;
}
// assign values to x[dim] vector
for (int i = 0; i < *dim; i++) {
fscanf(input, "%lf", &(*x)[i]);
printf("(*x)[%i] = %lf\n", i, (*x)[i]);
}
// Close input file
fclose(input);
}
char *get_input_filename(void) {
char *filename = malloc(200 * sizeof(*filename));
printf("Enter Input Filename: ");
scanf("%s", filename);
return filename;
}
int main (void) {
int DIM;
double *x = NULL;
char *input_filename = get_input_filename();
read_file(input_filename, &DIM, &x);
printf("size of (*x) = %llu\n", sizeof(*x));
for (int i = 0; i < DIM; i++) {
printf("(*x)[%i] = %lf\n", i, x[i]);
}
free(x); free(input_filename);
}
And here is the content of the input file in.txt
for this example:
5
0.0 0.1 0.2 1.4 2.6
When I call *x = malloc((*dim) * sizeof(*x));
I expected to find the dimension of the system to be 5 * 8bytes
, as the value for *dim
is assigned in the previous line, however only 8 bytes
are allocated. Then, I declared the y[(*dim)]
variable to check if the sizeof
a VLA
would behave in the same way as the sizeof
*x
, just to compare. The sizeof(y)
was what I expected, but the sizeof(*x)
wasn't, as shown in the output:
Enter Input Filename: in.txt
dim = 5
size of (*x) = 8
size of y = 40
(*x)[0] = 0.000000
(*x)[1] = 0.100000
(*x)[2] = 0.200000
(*x)[3] = 1.400000
(*x)[4] = 2.600000
size of (*x) = 8
(*x)[0] = 0.000000
(*x)[1] = 0.100000
(*x)[2] = 0.200000
(*x)[3] = 1.400000
(*x)[4] = 2.600000
I understand that it cannot allocate memory if the value of *dim
is unknown, but a value is assigned in the previous line.
Also, I even don't know how the program assigned values to (*x)
successfully as it don't have the necessary allocation of bytes to do it.
What am I missing here? How can I allocate the memory correctly?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
dim
和x
都是 output 参数;指的是指您要存储此功能结果的位置(double
值的大小和内存分配序列)。这是从轨道上走的地方:
左侧还可以。右侧的最多也是如此。但是,如果您从指向指向的验证数据的大小分配给指针,则需要使用它来做到这一点:指针指向的东西的大小。
x
是指指针,*x
是指指针,而后者是' ,其地址将存储在*x
上。因此,应该是:
注意:当我使用变量作为
sizeof
的运算符参数时,我亲自努力不是 使用Parens。它可以确保我实际上使用变量ID,而不是类型ID,因为没有Parens,后者不允许。根据自己的酌处权/宁可使用您喜欢的东西。但是,更基本的是,您对 的熟悉和理解是错误的和/或误导。您无法使用
sizeof
获得动态分配的内存区域的大小。使用指针变量上的sizeof
运算符将为您提供 您要求的内容:指针变量的大小(例如指针的大小)。它是您的 有责任维护和跟踪动态分配的幅度(顺便说一句,您的代码可以使用dim
)。Both
dim
andx
are output arguments; pointers that refer to locations where you're to store your results of this function (a size and a memory allocation sequence ofdouble
values).This is where things are going off the rails:
The left side is ok. And so is most of the right side. But if you're ever allocating to a pointer based on the size of the dereferenced data a pointer points to, you need to do so using just that: the size of the thing the pointer points to.
x
is a pointer-to-pointer,*x
is a pointer, and the latter is the 'thing' we're allocating memory for, the address of which will be stored at*x
.Therefore, that should be:
Note: when I'm using variables as the operator argument for
sizeof
I personally strive not to use parens. It ensures I'm actually using a variable id rather than a type id, since the latter isn't allowed without parens. use what you favor at your discretion/whim.More fundamental, however, your familiarity and understanding of
sizeof
is wrong and/or misled. You cannot acquire the size of dynamic-allocated memory region usingsizeof
. Using thesizeof
operator on a pointer variable will give you exactly what you asked for: the size of the pointer variable (e.g. the size of a pointer). It is your responsibility to maintain and track magnitude of dynamic allocations (which, btw, your code does, usingdim
).