我正在创建一个群众中心计划,我正在努力推广它,它最多只适用于3D。使用具有3D和质量元素的结构。类型对象的变量数量,我希望由用户而不是我定义。但我找不到使用循环创建变量的方法。也许有一种不同的方式去做,我只是不知道它。这是我的代码的一个例子,它不是整个代码,但它是所有方程式的相同逻辑。
#include <stdio.h>
#include <stdlib.h>
struct objects
{
int Xcdnt;
int Ycdnt;
int Zcdnt;
int Mass;
};
int main()
{
int objectcount = 0;
int dimensioncount = 0;
printf("How many objects are there?");
scanf(" %d", &objectcount);
printf("How many dimensions are used?");
scanf(" %d", &dimensioncount);
for(int i = 0; i < objectcount; i++)
{
struct objects (i);
}
for(int i = 0; i < objectcount; i++)
{
printf("X coordinate is %d.\n", (i).Xcdnt);
}
return 0;
}
答案 0 :(得分:1)
这是用C处理记忆的一个基本部分。这并不困难,但在灯泡眨眼之前,你必须与之交朋友之一。 (注意:如果你的编译器支持C99可变长度数组对象,你可以使用VLA而不是动态分配,这里没有解决)
您有三种工具可动态分配内存,malloc
,calloc
和realloc
。 (calloc
与malloc
做同样的事情,但是将所有字节初始化为零 - 当你想要确保所有内存都被初始化并且只比malloc
稍慢时
对于您的示例,您可以动态为对象分配内存,如下所示(注意以下/* comments */
中的其他信息):
#include <stdio.h>
#include <stdlib.h>
typedef struct { /* a typedef will do (and make things easier) */
int xcdnt; /* C generally uses all lowercase variable and */
int ycdnt; /* function names, avoiding MixedCase or */
int zcdnt; /* camelCase, reserving all uppercase for */
int mass; /* constants and macros. */
} object;
int main (void) {
int objectcount = 0;
int dimensioncount = 0;
object *objects = NULL;
printf ("How many objects are there?: ");
if (scanf (" %d", &objectcount) != 1) { /* validate ALL user input */
fprintf (stderr, "error: invalid conversion or EOF.\n");
return 1;
}
printf ("How many dimensions are used?: ");
if (scanf (" %d", &dimensioncount) != 1) {
fprintf (stderr, "error: invalid conversion or EOF.\n");
return 1;
}
/* create objectcount objects using calloc */
objects = calloc (objectcount, sizeof *objects);
if (!objects) { /* validate all memory allocations */
fprintf (stderr, "error: virtual memory exhausted.\n");
return 1;
}
/* create some random values */
for (int i = 0; i < objectcount; i++) {
objects[i].xcdnt = i;
objects[i].ycdnt = i + 1;
objects[i].zcdnt = i + 2;
objects[i].mass = i % 10 + 5;
}
printf ("\ndimensioncount: %d\n\n", dimensioncount);
/* output the random data in objects */
for (int i = 0; i < objectcount; i++) {
printf ("X coordinate is %d.\nY coordinate is %d.\n"
"Z coordinate is %d.\nmass is: %d\n\n",
objects[i].xcdnt, objects[i].ycdnt, objects[i].zcdnt,
objects[i].mass);
}
free (objects); /* don't forget to free the memory you allocate */
return 0;
}
示例使用/输出
$ /bin/dynamicobjects
How many objects are there?: 10
How many dimensions are used?: 3
dimensioncount: 3
X coordinate is 0.
Y coordinate is 1.
Z coordinate is 2.
mass is: 5
X coordinate is 1.
Y coordinate is 2.
Z coordinate is 3.
mass is: 6
<snip... objects[2-7]>
X coordinate is 8.
Y coordinate is 9.
Z coordinate is 10.
mass is: 13
X coordinate is 9.
Y coordinate is 10.
Z coordinate is 11.
mass is: 14
内存使用/错误检查
在你编写的动态分配内存的任何代码中,你有2个职责关于任何分配的内存块:(1)总是保留一个指向起始地址的指针内存块,(2)当不再需要时,它可以释放。
您必须使用内存错误检查程序,以确保您不会尝试在已分配的内存块的范围之外/之外进行写入,尝试读取或基于未初始化值的条件跳转,最后,确认您释放了已分配的所有内存。
对于Linux valgrind
是正常的选择。每个平台都有类似的记忆检查器。它们都很简单易用,只需通过它运行程序即可。
$ valgrind ./bin/dynamicobjects
==9105== Memcheck, a memory error detector
==9105== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==9105== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==9105== Command: ./bin/dynamicobjects
==9105==
How many objects are there?: 10
How many dimensions are used?: 3
dimensioncount: 3
X coordinate is 0.
Y coordinate is 1.
Z coordinate is 2.
mass is: 5
<snip... remaining>
==9105==
==9105== HEAP SUMMARY:
==9105== in use at exit: 0 bytes in 0 blocks
==9105== total heap usage: 1 allocs, 1 frees, 160 bytes allocated
==9105==
==9105== All heap blocks were freed -- no leaks are possible
==9105==
==9105== For counts of detected and suppressed errors, rerun with: -v
==9105== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
始终确认已释放已分配的所有内存并且没有内存错误。
仔细看看,如果您有任何其他问题,请告诉我。
答案 1 :(得分:0)
你需要malloc一个对象数组。您当前的代码只是挖掘创建和销毁相同的代码
struct objects* objects = malloc(objectcount * sizeof(struct objects));
for(int i = 0; i < objectcount; i++)
{
objects[i].Xcdnt = 42;
.....
}
答案 2 :(得分:0)
要创建一个对象数组,而不是循环,
con <- DBI::dbConnect(odbc::odbc(),
driver = "/Library/ODBC/FileMaker ODBC.bundle/Contents/MacOS/FileMaker ODBC",
server = "127.0.0.1",
database = "/Users/bradcannell/Dropbox/Filemaker Pro/Notes/test_r.fmp12",
uid = "Admin",
pwd = "password")
其中struct objects foo[objectcount];
是数组的名称,而foo
是数组中的项目数。
然后访问每个元素的代码将是:
objectcount
虽然您需要在此之前执行一些其他步骤,才能在打印之前将值分配给for(int i = 0; i < objectcount; i++)
{
printf("X coordinate is %d.\n", foo[i].Xcdnt);
}
。
答案 3 :(得分:0)
使用malloc动态创建数组
package.json
你也可以像这样创建一个本地数组:
struct objects* objs = malloc(sizeof *objs * objectcount);
但如果它太大,你就有可能炸毁堆栈。