0

I think I have got my head mostly around the difference, but if I am correct, then this should be correct also:

1.)

char *string1 = (char*) malloc(runtime_determined_number);

2.)

char string2val[runtime_determined_number];
char *string2 = &string2val;

Here I would expect string1 and string2 to be the same, is this the case?

Matthew Haworth
  • 426
  • 1
  • 5
  • 17
  • To be the same in what sense? Also, the cast in the first sample is wrong, and the pointer types in the second one mismatch. –  Apr 08 '13 at 13:41
  • H2CO3: There's no pointer-type-mismatch - it's weird, and I wouldn't write the '&', but it's valid. – Dougall Apr 08 '13 at 13:43
  • This is tagged as a 'C' question. I don't believe that C allows a declaration like `char string2val[runtime_determined_number];` You could use a *compile time* symbol there, but not a runtime variable. I found elsewhere that this is bad in C89, but ok in C99. I guess I'm too old. – Steve Valliere Apr 08 '13 at 13:44
  • 1
    I've said it before, and H2CO3 said it above but somewhat opaquely. So, here we go again: [don't cast the return value of `malloc()` in C](http://stackoverflow.com/a/605858/28169). – unwind Apr 08 '13 at 13:45
  • I meant same in the sense that both fragments of code achieve the same goal. – Matthew Haworth Apr 08 '13 at 13:46
  • @MatthewHaworth example of usage will help to get the good answer – MOHAMED Apr 08 '13 at 13:47
  • @Dougall: `&string2val` is a pointer to an array of char, but `string2` is a pointer to char. The pointer types do not match. The automatic conversion of array to pointer does not happen when the array is the operand of `&`. – Eric Postpischil Apr 08 '13 at 13:54
  • My mistake, sorry. I think effect is correct, but there is a pointer type mismatch. – Dougall Apr 08 '13 at 14:07

4 Answers4

1

string1 and string2 are not pointed to the same memory area

string1 is a pointer pointing to a char array allocated dynamically with malloc

string2 is a pointer pointing to a char array allocated statically

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • Does the `runtime_determined_number` not make `string2`'s allocation dynamic? – Matthew Haworth Apr 08 '13 at 13:51
  • @MatthewHaworth since C99 it's allowed to define static array with runtime var as you did in your example but this is still static allocation and not dynamic allocation. The dynamic allocation memory could be free later in your code with free() but this is not possible for static memories – MOHAMED Apr 08 '13 at 13:54
  • The definitions shown for `string2val` and `string2` may appear inside a function or outside. If they appear inside a function as shown except with the type mismatch corrected, the storage duration is automatic, not static. – Eric Postpischil Apr 08 '13 at 14:01
  • @EricPostpischil I missed that in my last comment – MOHAMED Apr 08 '13 at 14:02
  • @MatthewHaworth Refer to the `EricPostpischil` answer it complete my answer – MOHAMED Apr 08 '13 at 14:04
  • Sorry, what is the type mismatch? – Matthew Haworth Apr 09 '13 at 12:39
0

They both point to uninitialized blocks of memory of the same length. So in that respect they are the same, yes.

Note that in case 1, you are responsible for freeing the memory once you're finished. And in case 2, you can't safely return the pointer from a function as the memory will go out of scope on exit.

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
0

They are similar in that they have the same type and have at least runtime_determined_number bytes allocated for them.

They are different in that:

  1. the first version requires an explicit free() to avoid a memory leak;
  2. the lifetime of the two objects may or may not be the same.

Note that the second version is only valid in C99, since it makes use of a variable-length array.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

Using malloc you are asking the OS for memory during runtime. If malloc succeeded you are able to work with the allocated memory. You must deallocate this memory later on!
In your second part you are creating a char-array and then assign its address to a pointer. In this case the memory is taken from the stack and will be freed automatically when the array goes out of scope.
Your char*s won't be the same as they will be pointing to different locations in memory. There is aminor chance they contain the same garbage as you have not initialized them...

bash.d
  • 13,029
  • 3
  • 29
  • 42