- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
此作业的要点是从包含城市状态的文件中读取一行,后跟包含纬度和经度坐标的两行。然后我们需要完全按照下面编写的那样动态分配结构来存储这些坐标。最后,我们将地标导出为 Google Earth 使用的 KML 格式。
此时我可以从我的结构中正确编写 KML。然而,这在我的grow_whole函数中使用了几个脏的for循环(如下所示)。问题是当我稍后释放所有分配的指针时。 (相信我,它存在,我将按下面的顺序粘贴)。我很确定问题是堆损坏。然而,我已经在这种方式上工作太久了,我觉得它可以更简单。
文件格式。 (大约有 150 个。)
city, state
40 30 N
40 20 W
Wholesome_t 只是一个容器,它将保存有关地标结构数量以及指向第一个地标的指针的信息。 Landmark_t 保存指向字符的指针,这些字符所分配的空间量正是它们包含解析的城市名称等所需的空间量。
struct landmark_t {
char *city;
char *state;
char *country;
float longitude;
float latitude;
};
struct wholesome_t {
struct landmark_t *landmarks;
int landcount;
int landmax;
};
下面是处理读取行并将其发送到正确的解析器的代码块。
最初,wholesome 被 malloc,并且地标指针被设置为 NULL。然后我们为 2 个标志性结构分配空间并开始下面的 while 循环。
struct landmark_t *land = NULL;
int coorflag = 0;
int flagcount = 0;
//Parse lines
while(fgets(buf, LEN, in)){
//Does that thing where we realloc
if(whole->landcount == whole->landmax){
grow_whole(whole);
}
//remove trailing newline
buf[strcspn(buf, "\n")] = 0;
if(!coorflag){//Check to see if we are on a flag or coordinate
//set land to be the pointer to our next empty landmark struct
land = whole->landmarks + (sizeof(struct landmark_t) * whole->landcount);
set_city_state_country(buf, land);
coorflag = 1; //Set flag to get a coordinate line next
}else{//We are on a coordinate line which will use
//the same land struct pointer as above
if(!flagcount){//Have we seen a coordinate line already?
land->latitude = number_muncher(buf);
flagcount = 1;
}else{//We have seen a coordinate line
land->longitude = number_muncher(buf);
//We are done filling this structure. Reset flags and move to the next.
flagcount = 0;
coorflag = 0;
whole->landcount++;
}
}
}
问题出在grow_whole上。我之前遇到一个问题,在运行 realloc 后,第一个地标结构将包含正确分配的指针,但其后直到文件末尾附近的所有内容都会包含城市、州和国家以及经度和纬度的所有指针归零。我添加了 for 循环,在重新分配之前将所有指针保存到数组,然后将这些指针重写回正确的结构。
void grow_whole(struct wholesome_t *whole)
{
//First collect all of the pointers inside our current landmark structs.
struct landmark_t *land = NULL;
char *city[whole->landcount];
char *state[whole->landcount];
char *country[whole->landcount];
float longitude[whole->landcount];
float latitude[whole->landcount];
for (int i = 0; i < whole->landcount; i++){
land = whole->landmarks + (sizeof(struct landmark_t) * i);
city[i] = land->city;
state[i] = land->state;
country[i] = land->country;
longitude[i] = land->longitude;
latitude[i] = land->latitude;
}
land = realloc(whole->landmarks, (GROW + whole->landmax) * sizeof(struct landmark_t));
if(land == NULL){
printf("Error in grow_whole.\n");
return;
}else{whole->landmarks = land;}
//Update landmax to represent aftergrow.
whole->landmax = GROW + whole->landmax;
//Refill all of the re allocated structs with their pointers.
for (int i = 0; i < whole->landcount; i++){
land = whole->landmarks + (sizeof(struct landmark_t) * i);
land->city = city[i];
land->state = state[i];
land->country = country[i];
land->longitude = longitude[i];
land->latitude = latitude[i];
}
//Fill two new structs with blank data.
for(int i = whole->landcount; i < whole->landmax; i++){
land = whole->landmarks + (sizeof(struct landmark_t) * i);
empty_fill(land);
}
return;
}
在上面的 while 循环之后我们立即运行
fclose(in);
char *s = argv[1];
s = strtok(s, ".");
s = strncat(s, ".kml", 4);
FILE *out = fopen(s, "w");
//Finally done lets write some KML
kml_begin(out);
for (int i=0; i < whole->landcount; i++){
land = whole->landmarks + (sizeof(struct landmark_t) * i);
kml_placemark(out, i, land);
}
kml_end(out);
fclose(out);
tea_party(whole);//free land
free(whole->landmarks);
free(whole);
到达kml_end(out)。 (我知道,因为我有一个格式正确、值正确的 .kml 文件)但是在 tea_party 期间,我遇到了 seg_fault。
void tea_party(struct wholesome_t *whole)
{
struct landmark_t *land = NULL;
for (int i = 0; i < whole->landcount; i++){
land = whole->landmarks + (sizeof(struct landmark_t) * i);
printf("here\n");
free(land->city);
printf("here2\n");
free(land->state);
printf("here3\n");
free(land->country);
}
return;
}
它发生在我们的第二个里程碑结构上。
land = whole->landmarks + sizeof(struct landmark_t)
并且发生在 free(land->state)(因为我在这里看到第一个结构的 1-3,然后在段错误之前看到这里的 1-2。)
即使我尝试在段错误之前打印 land->state,它也只会将段错误向上移动。
最佳答案
在进行指针算术时,您不必按 sizeof(structlandmark_t)
缩放偏移量 - 编译器已经为您完成了这项工作!
如果有一个指向数组的指针p
,则下一个数组元素位于p + 1
。就这么简单。
此外,您似乎创建了一个数组记录,而您可能应该创建一个记录数组。但这是另一个问题。
关于c - 重新分配包含动态大小的 char 指针的结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42214078/
我刚接触 C 语言几周,所以对它还很陌生。 我见过这样的事情 * (variable-name) = -* (variable-name) 在讲义中,但它到底会做什么?它会否定所指向的值吗? 最佳答案
我有一个指向内存地址的void 指针。然后,我做 int 指针 = void 指针 float 指针 = void 指针 然后,取消引用它们以获取值。 { int x = 25; vo
我正在与计算机控制的泵进行一些串行端口通信,我用来通信的 createfile 函数需要将 com 端口名称解析为 wchar_t 指针。 我也在使用 QT 创建一个表单并获取 com 端口名称作为
#include "stdio.h" #include "malloc.h" int main() { char*x=(char*)malloc(1024); *(x+2)=3; --
#include #include main() { int an_int; void *void_pointer = &an_int; double *double_ptr = void
对于每个时间步长,我都有一个二维矩阵 a[ix][iz],ix 从 0 到 nx-1 和 iz 从 0 到 nz-1。 为了组装所有时间步长的矩阵,我定义了一个长度为 nx*nz*nt 的 3D 指针
我有一个函数,它接受一个指向 char ** 的指针并用字符串填充它(我猜是一个字符串数组)。 *list_of_strings* 在函数内部分配内存。 char * *list_of_strings
我试图了解当涉及到字符和字符串时,内存分配是如何工作的。 我知道声明的数组的名称就像指向数组第一个元素的指针,但该数组将驻留在内存的堆栈中。 另一方面,当我们想要使用内存堆时,我们使用 malloc,
我有一个 C 语言的 .DLL 文件。该 DLL 中所有函数所需的主要结构具有以下形式。 typedef struct { char *snsAccessID; char *
我得到了以下数组: let arr = [ { children: [ { children: [], current: tru
#include int main(void) { int i; int *ptr = (int *) malloc(5 * sizeof(int)); for (i=0;
我正在编写一个程序,它接受一个三位数整数并将其分成两个整数。 224 将变为 220 和 4。 114 将变为 110 和 4。 基本上,您可以使用模数来完成。我写了我认为应该工作的东西,编译器一直说
好吧,我对 C++ 很陌生,我确定这个问题已经在某个地方得到了回答,而且也很简单,但我似乎找不到答案.... 我有一个自定义数组类,我将其用作练习来尝试了解其工作原理,其定义如下: 标题: class
1) this 指针与其他指针有何不同?据我了解,指针指向堆中的内存。如果有指向它们的指针,这是否意味着对象总是在堆中构造? 2)我们可以在 move 构造函数或 move 赋值中窃取this指针吗?
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: C : pointer to struct in the struct definition 在我的初学者类
我有两个指向指针的结构指针 typedef struct Square { ... ... }Square; Square **s1; //Representing 2D array of say,
变量在内存中是如何定位的?我有这个代码 int w=1; int x=1; int y=1; int z=1; int main(int argc, char** argv) { printf
#include #include main() { char *q[]={"black","white","red"}; printf("%s",*q+3); getch()
我在“C”类中有以下函数 class C { template void Func1(int x); template void Func2(int x); }; template void
我在64位linux下使用c++,编译器(g++)也是64位的。当我打印某个变量的地址时,例如一个整数,它应该打印一个 64 位整数,但实际上它打印了一个 48 位整数。 int i; cout <<
我是一名优秀的程序员,十分优秀!