简体   繁体   English

有两个指针指向相同内存块的正确方法

[英]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. 您可以像围绕数字一样交易指针值。

  1. Yes, you're misunderstanding, but I can't quite figure out what you think is happening to explain how it's wrong. 是的,您误会了,但是我无法完全弄清您的想法以解释它是怎么回事。

  2. 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.

相关问题 指向正确的内存位置的指针 - pointers to point proper memory location 如果两个指针指向同一个memory地址,是只需要使用free(ptr)一次还是两次? - If two pointers point to the same memory address, do you only need to use free(ptr) once or twice? 检查两个指针​​是否在同一块内存中 - Checking whether two pointers are in the same block of memory 为什么两个相同值的整数指针指向同一个东西(via ==)? - Why are two integer pointers of the same value point to the same thing (via ==) ? 是否可以将相同的指向 struct 的指针数组指向不同类型的 struct? - Is it possible to have the same array of pointers to struct point to different types of struct? 如果两个文件指针指向同一个文件,关闭其中一个是否足够? - If two file pointers point to the same file, is closing one of them enough? 为什么这两个应该相同的指针指向不同的数据? - Why do these two pointers that should be the same point to different data? 具有相同数据的两个C空指针是否是不同的存储对象? - Are two C void pointers with the same data different memory objects? 我错过了指针吗? - Have I missed the point of pointers? 两个指针指向同一个内存地址,如何避免在独立位置发生这两个释放的情况下的内存泄漏? - Two pointers are pointing to same memory address, how to avoid memory leak in case freeing of these two happens at independence place?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM