0

Let's have some pointer to a structure, like tm:

time_t timestamp = time(NULL);
tm* now = localtime(&timestamp);

To create a pointer to a copy in automatic memory, copy by value can be used:

tm copy = *now;
tm* next = ©
// next points to a copy in memory

But why this shortcut doesn't copy the value to a new memory block? (gcc compiler)

tm* next = &(*now);
// next points to the address of now

This may sound trivial but I'm not sure about the mechanism behind. Why is there a difference?

Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
  • is it really possible to copy value using pointers i know about it only reference memory address of some data – Shushant Oct 07 '13 at 08:12
  • It seems like you need to read (or reread) a good book on C. Here's a [list](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to got you started. – RedX Oct 07 '13 at 08:12
  • there's no "automatic memory" in C (at least not in the way you want to use it); you have to allocate the memory somehow, and `tm*next = ...` will only allocate memory for the pointer, *not* for the struct. – umläute Oct 07 '13 at 08:13

4 Answers4

2

your first example will reserve memory on the stack for the variable copy. it will then copy the content of now to the new memory-location, and finally have next point to that memory location.

tm copy = *now;
tm* next = ©

the second example will not reserve any new memory; instead it will simply assign next the address (&) of the object now points to (*).

tm* next = &(*now);

the reason they are different, is that C never does any "automagic" memory allocation. you get what you ask for: if you ask for memory for a variable (as in the first example) you get it; if you don't ask for it, you don't get it.

so the difference between the two examples is really that declaring a variable (tm copy) is asking for memory.

umläute
  • 28,885
  • 9
  • 68
  • 122
  • Now it's clear. I hope you don't mind I accept the user694733's answer - just because it was the first to see my point. Thank you. – Jan Turoň Oct 07 '13 at 08:35
1

Pointers, as the name suggests, point to a certain place in memory. They do not allocate the necessary memory on their own. You have to do it yourself.

In your code, the pointer next is actually pointing th the location in memory, where now is is located. If you modify any value through the pointer, you are actually modifying the original structure. That's how pointers work.

If you want to allocate memory, use malloc or calloc. But remember, that memory allocated like that has to be freed.

Once you have allocated the memory, you can copy it by value.

tm prev = ...// get the value
tm* now = calloc (1, sizeof (tm));
*now = prev; // copy by value
Dariusz
  • 21,561
  • 9
  • 74
  • 114
  • I know. But I don't want to use dynamic memory management here. I'm just curious why there is a difference between the shortcut and the two-lines form. – Jan Turoň Oct 07 '13 at 08:25
1

= is only for copying data from one memory location to another. & only gives you address of a variable. * only dereferences a variable. None of these allocate memory in any form, or create any temporary variables.

All memory allocations must be explicitly stated in C. There is no difference to be found here.

user694733
  • 15,208
  • 2
  • 42
  • 68
  • That makes a sense. So when the compiler sees a declaration of a variable, it translates it as allocation of memory on that place? – Jan Turoň Oct 07 '13 at 08:31
  • 2
    @JanTuroň Yes, basically your `tm copy` and `tm* next` allocate memory from stack. – user694733 Oct 07 '13 at 08:32
0

With tm* next = &(*now); both next and now will point to same memory location which contains data for structure tm.

No new memory is allocated for next to point to.

Rohan
  • 52,392
  • 12
  • 90
  • 87