简体   繁体   中英

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.

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

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.

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. 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). The pointer returned is then assigned to the generic variable, overwriting the value stored there by a previous instruction. 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:

    struct generic_attribute *ga1 = construct_generic_attribute(args[0]);

then make a copy of the pointer:

    struct generic_attribute *ga2 = ga1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM