3

While trying to understand some code in C++ I came across the following code (and trying to understand its meaning):

int SIZE = 256;
float* A = (float *) malloc(SIZE * sizeof(float*));
for (int i=0; i<M*K; i++) { A[i] = 0.0; }

I wanted to ask, how is the above different from the following:

float* A = (float *) malloc(SIZE * sizeof(float));

When I compile the code, both of the versions of "float* A=" compile and execute ok.

jwdonahue
  • 6,199
  • 2
  • 21
  • 43
user118837
  • 69
  • 7
  • 5
    Don't try to write C/C++. They are different languages, and if you pigeon hole yourself into writing code that compiles in both, you'd just makes things harder for yourself. – StoryTeller - Unslander Monica May 21 '18 at 06:07
  • On some computers `float` and `float*` happens to both use 32 bits. Then it kind of works. – Bo Persson May 21 '18 at 06:10
  • @StoryTeller I've corrected it to reflect that it was C++ code. As I have been searching through tutorials / explanations, most of them mention that it is best practice to use new in C++. However I would still like to know a bit more about the difference / similarities between the two. – user118837 May 21 '18 at 06:15
  • No, it is bad practice to use `new` in C++. –  May 21 '18 at 06:18
  • The first reserves space for a bunch of pointers to pointers to floats and the second reserves space for a bunch of pointers to floats. If a pointer to a float and a float happen to be the same size, you get the same space. – Mark Setchell May 21 '18 at 06:19
  • 2
    @user118837 *most of them mention that it is best practice to use new in C++* -- The best practice is to use `std::vector`, and forego using `new[]` until you have a darn good reason to use it. – PaulMcKenzie May 21 '18 at 06:25
  • @MarkSetchell Thus in order to allocate a memory for an array of floats, the second statement is the correct one, is that right? (as a[b] == *(a + b)) – user118837 May 21 '18 at 06:25
  • 1
    The first one is heading in the direction of (but not completing) allocation of a 2-d array because it will be a *"column of pointers"* to a bunch of rows of data. The second is a 1-d thing, because it is just a pointer to an area of memory containing individual floats. – Mark Setchell May 21 '18 at 06:34
  • I was going to write an answer and make a pretty diagram, but it is very well explained already here... especially the diagrams. I hope it helps. https://aticleworld.com/dynamically-allocate-2d-array-c/ – Mark Setchell May 21 '18 at 06:51

2 Answers2

1

...I wanted to ask, how is the above different from the following...

sizeof(float*) is the size of a pointer to float. On most systems that's whatever number of bytes there are in a pointer, often, but not always, the size of hardware address registers. sizeof(float) is the number of bytes in a float, which may or may not be the same size as a pointer.

jwdonahue
  • 6,199
  • 2
  • 21
  • 43
  • What's with the down-vote? At least have the balls to tell me what you think is wrong with my answer. – jwdonahue May 21 '18 at 06:22
  • 1
    Not the downvoter, but some people believe that `std::vector` is the way to go. – Bo Persson May 21 '18 at 06:24
  • @BoPersson, yes of course, but not every C++ target is a good candidate for C++ template code. – jwdonahue May 21 '18 at 06:26
  • 1
    @jwdonahue if an implementation lacks ``, I would categorise it as *not implementing C++* – Caleth May 21 '18 at 08:27
  • Having it available is one thing, having room for template code on the target system is another. I have worked on many embedded systems where the convenience of the C++ language is very nice to have, but the overhead of some of it's more advanced features just won't fit on the device. – jwdonahue May 21 '18 at 17:13
0

Float* is pointer. Regardless of what data type a pointer is pointing to, its size is fixed and is 32 bits On 32-bit machine and 64 bit on 64 bit machine.

Rezaeimh7
  • 1,467
  • 2
  • 23
  • 40
  • Pointer size is up to the compiler implementation, not the hardware it runs on. There are many 32 bit programs running on 64 bit machines. – jwdonahue May 21 '18 at 06:23