1

In my understanding,

Example1

cPtr = (char*)malloc(100);

Example2

1 char c = 0;
2 char* cPtr = &c
3 cPtr = (char*)malloc(100);

In Example1, malloc creates an memory space and returns the the first block of address of allocated memory. So cPtr gets an arbitrary address inside heap.

In Line 2 of Example2, cPtr is pointing to c. So cPtr has an address of c.

At this moment, when you execute line3 of Example 2, What would be the value of cPtr? Does it get an arbitrary memory address as I mentioned in Example1? Or, Does it keep the address of c and creates a spaces?

Peter Hwang
  • 971
  • 1
  • 12
  • 25

3 Answers3

3

Does it get an arbitrary memory address as I mentioned in Example1?

Yes. That line just overwrites the previous value of cPtr.

Or, Does it keep the address of c and creates a spaces?

No, it doesn't "keep" anything. cPtr is overwritten with the return value of the malloc() call, which points to some block of memory, just the same as your first example.

Lines 1 and 2 of your second example are no-ops, essentially.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • If I want to allocate the address onward the address of `c` then how can I do? – haccks Oct 09 '13 at 17:28
  • 1
    You can't do that. Something else might already be there. `cPtr` itself, for example. – Carl Norum Oct 09 '13 at 17:30
  • Looke like line 1 and 2 is pointless, right? I want to use c as a first character of allocated memory. Could putting "char c= *cPtr" after line 3 be a solution? – Peter Hwang Oct 09 '13 at 17:32
  • 1
    You want the reverse : "*cPtr = c" ie: copy the character in c in the first character of the region pointed by cPtr. – hivert Oct 09 '13 at 17:33
  • No, that won't work. You'll just assign an unknown value to `c`. If you want to use pointers, you have to use pointers. – Carl Norum Oct 09 '13 at 17:34
1

malloc returns value that succesfully can initialize or change value of pointer.
Initialization takes place in Example1, changing value of cPtr takes place in Example2.
That's the only difference between those two.

zubergu
  • 3,646
  • 3
  • 25
  • 38
1

With regard to the value of cPtr, in your second example line 3 completely discards all effects of line 1 and 2. I.e. the value returned by malloc simply overwrites the previous value in cPtr. So, in the second example lines 1 and 2 make absolutely no impact on the code behavior.

In other words, both examples are 100% equivalent, with the second one having two extra lines of completely inconsequential ("dead") code.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • The effects of line 1 are not discarded. The identifier `c` remains in scope, and the object it refers to continues to exist during the execution of the block. – Eric Postpischil Oct 09 '13 at 17:41
  • @Eric Postpischil: I'm referring to the effects of those lines with the context of the specific question. The question is about the effects the code has non the value of `cPtr`. This is exactly what I'm addressing by my answer. Within this context the existence of the additional object named `c` has about as much relevance as the fact that the source code for example 2 might occupy a bit more space on the hard drive. – AnT stands with Russia Oct 09 '13 at 18:01
  • Given the OP’s confusion, you might cause them to think `c` is released by line 3. – Eric Postpischil Oct 09 '13 at 18:17