-4

What is the difference between using

float* f;

and

float f[4];

Or are they treated the exact same way? Will using one over the other potentially cause you to run into memory allocation issues? If not, are there ANY situations where they are treated differently?

Don't know if this is relevant, but I'm using the type float* as a function argument. For example:

void myfun(float* f){
     f[0] = 0;
     f[1] = 1;
     f[2] = 2;
     f[3] = 3;
}

which compiles and runs fine (I'm not really sure why - I would think that since I didn't allocate any memory to f that it would throw some kind of exception).

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
asrjarratt
  • 314
  • 6
  • 17
  • 1
    Just look up "arrays and pointers in C". – Jashaszun Jul 08 '15 at 20:49
  • This article might help a little: http://eli.thegreenplace.net/2009/10/21/are-pointers-and-arrays-equivalent-in-c – scrappedcola Jul 08 '15 at 20:52
  • 1
    The name of an array IS a pointer to its first element. 'float f [4]' will allocate space on the stack, float *f defines just an uninitialized pointer. When you call your function passing the array, you don't pass all the data, just the address of data that is already allocated by the caller. So the function doesn't have to allocate it. – Jacques de Hooge Jul 08 '15 at 20:55
  • You are being confused by what `[ ]` means in two contexts. To access elements, `[ ]` is used, whether it is a pointer or array. That is not related to how you *declare* pointers and arrays. – PaulMcKenzie Jul 08 '15 at 20:55
  • 1
    @jacdeh No, the name of an array is the name of an array. It may decay to a pointer to the first element in some situations. Claiming otherwise is misleading. – juanchopanza Jul 08 '15 at 21:10
  • They are different things. – Martin James Jul 08 '15 at 21:32

3 Answers3

5
float f[4]

is an array that is allocated with automatic storage (typically the stack)

float* f;

is a pointer, which by itself has no allocation other than the size of a pointer. It can point to a single floating point value, or an array of floats that was allocated with either automatic or dynamic storage (heap)

An unallocated pointer typically points to random memory, so if you pass it into your myfun function, you will get undefined behavior. Sometimes it may seem to work (overwriting whatever random memory it is pointing too), and sometimes it will except (trying to write to invalid or inaccessible memory)

They are treated the same in many situations, and not in others. i.e. sizeof(f) will be different.

Gerald
  • 23,011
  • 10
  • 73
  • 102
1
float* f;

It is a pointer to a variable named f of type float

float f[4];

It is an array variable named f of type float with size 4

What is the difference between using

float* f;

and

float f[4];

Or are they treated the exact same way?

No

Will using one over the other potentially cause you to run into memory allocation issues?

You are not supposed to use one in place of other, you may face multiple issues not only memory related issues.

If not, are there ANY situations where they are treated differently?

They always treat differently except the scenario array and pointer declarations interchangeable as function formal parameters.

which compiles and runs fine (I'm not really sure why - I would think that since I didn't allocate any memory to f that it would throw some kind of exception).

This scenario is explained above as an exception in previous answer.

Read arrays and pointers

Steephen
  • 14,645
  • 7
  • 40
  • 47
1

"I would think that since I didn't allocate any memory to f that it would throw some kind of exception)."

No, if you didn't allocate memory for f appropriately, dereferencing is simply undefined behavior. No exceptions guaranteed.

Your fridge might eat your cat in case ...

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190