【发布时间】:2014-01-19 20:43:22
【问题描述】:
我的程序动态分配结构指针类型的节点。我想通过它的内存地址访问结构指针的数据。我的问题的主要目的是测试带有拆分的 b-tree 插入。这是结构:
struct LeafNode
{
int EmpID[M];
int Location[M];
int keys;
};
typedef struct LeafNode* LNodePtr;
这是我的测试代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int * a = (int *)malloc(sizeof(int));
*a = 5;
printf("int a created at %p\n", (void *) &a);
void * addr;
printf("Give the memory address: ");
scanf("%p", addr);
void * addr2 = (void *)addr;
printf("The value at memory is: %d\n",*(int*)addr2);
return 0;
}
结果如下:
int a 创建于 0xbff573b4
给出内存地址:0xbff583b4
分段错误
第二步是转换为 LNodePtr 而不是 int。我不知道我们是否可以使用双重投射,即
*(int *(LNodePtr))addr
这样我就可以访问了
struct's EmpID[0]
int 成员通过内存。感谢您的帮助。
【问题讨论】:
-
当我运行程序时,操作系统会改变进程的虚拟内存地址。谢谢。