[英]Proper way to have two pointers point to the same memory chunk
I have a structure: 我有一个结构:
struct generic_attribute{
int current_value;
int previous_value;
};
And a constructor which outputs a pointer to this structure: 还有一个构造函数,它输出指向此结构的指针:
struct generic_attribute* construct_generic_attribute(int current_value){
struct generic_attribute *ga_ptr;
ga_ptr = malloc (sizeof (struct generic_attribute));
ga_ptr->current_value = current_value;
ga_ptr->previous_value = 0;
return ga_ptr;
}
Now, in another function, I want to define a pointer and set it to point to the same address as the pointer that the above constructor outputs. 现在,在另一个函数中,我想定义一个指针并将其设置为指向与上述构造函数输出的指针相同的地址。
struct tagged_attribute* construct_tagged_attribute(int num_args, int *args){
...
struct generic_attribute* generic = malloc (sizeof(struct generic_attribute));
generic = construct_generic_attribute(args[0]);
...
}
It looks to me that what I am doing here is this: 在我看来,我在这里所做的是:
1) I define a pointer "generic" and allocate a memory chunk to hold an instance of generic_attribute structure. 1)我定义了一个指针“ generic”,并分配了一个内存块来保存generic_attribute结构的实例。
2) I call a function construct_generic_attribute within which, the program once again allocates a memory chunk of size of a generic_attribute structure . 2)我调用一个函数construct_generic_attribute,在该函数中,程序再次分配一个大小为generic_attribute结构的内存块 。 It outputs a pointer to this memory chunk.
它输出一个指向该内存块的指针。
3) In construct_tagged_attribute I set "generic" pointer equal to the pointer output by the construct_generic_attribute function, so now both of them point to the same memory slot. 3)在construct_tagged_attribute中,我将“通用”指针设置为与construct_generic_attribute函数输出的指针相等,因此现在它们都指向相同的内存插槽。
However, it appears that I am allocating twice as much memory as I need to allocate. 但是,看来我分配的内存是我需要分配的两倍。
Is there a way for me to allocate memory only once without getting a segmentation fault for failing to allocate space for "generic" pointer? 有没有一种方法可以让我只分配一次内存,而不会因无法为“通用”指针分配空间而导致分段错误? Alternatively, am I misunderstanding what is happening in this code?
另外,我是否误解了这段代码中发生了什么?
struct generic_attribute* generic = construct_generic_attribute(args[0]);
Should do the trick. 应该做到的。 Pointer variable is just that, a variable.
指针变量就是那个变量。 You can trade pointer values around just like numbers.
您可以像围绕数字一样交易指针值。
Yes, you're misunderstanding, but I can't quite figure out what you think is happening to explain how it's wrong. 是的,您误会了,但是我无法完全弄清您的想法以解释它是怎么回事。
struct generic_attribute *generic = construct_generic_attribute(args[0]);
a pointer is a kind of value. 指针是一种价值。 If you assign a pointer to another, you get two pointers to the same thing, without any allocation of memory.
如果将指针分配给另一个指针,则将获得指向同一对象的两个指针,而不分配任何内存。 Since C doesn't manage memory for you, it's up to you to make sure that any object that's allocated is freed exactly once, and that you don't try to use pointers to an object after it's been freed.
由于C不会为您管理内存,因此您有责任确保所分配的任何对象都完全释放一次,并且不要在释放对象后尝试使用指向该对象的指针。
Here 这里
struct generic_attribute* generic = malloc (sizeof(struct generic_attribute));
you allocate a memory block, big enough to keep a generic_attribute
structure, then store a pointer to that structure (technically: an address of the block) in the generic
variable. 您分配了一个内存块,该内存块的大小足以保留
generic_attribute
结构,然后在该generic
变量中存储一个指向该结构的指针(从技术上讲:该块的地址)。 Note: you do not initialize the structure members. 注意:您不初始化结构成员。
Then in 然后在
generic = construct_generic_attribute(args[0]);
you call a function, which internally allocates (another) block of memory and initializes it and returns a pointer to it (which was stored in a ga_ptr
variable during the function execution). 您调用一个函数,该函数会在内部分配(另一个)内存块并对其进行初始化,并返回指向该内存的指针(该指针在函数执行期间存储在
ga_ptr
变量中)。 The pointer returned is then assigned to the generic
variable, overwriting the value stored there by a previous instruction. 然后,将返回的指针分配给
generic
变量,并用上一条指令覆盖存储在其中的值。 Consequently you loose an access to the first allocated structure. 因此,您将失去对第一个分配的结构的访问权限。
EDIT 编辑
I'm afraid I do not quite understand what you're trying to achieve. 恐怕我不太了解您要达到的目标。 If you want two pointers to the same structure, just declare
ga1
and assign it a pointer to the created structure: 如果要两个指向同一结构的指针,只需声明
ga1
并为其分配一个指向创建的结构的指针:
struct generic_attribute *ga1 = construct_generic_attribute(args[0]);
then make a copy of the pointer: 然后复制指针:
struct generic_attribute *ga2 = ga1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.