To be clear, my code works perfectly. The issue that concerns me is that i am unsure of my array allocation type.
My task is rather simple: i am required to do some operations within a dynamically allocated array.
Yet, the values are already given in the array. So therefore i am required to add these values in it.
To keep my vector dynamically allocated and to avoid the following situation:
float *p;
p = malloc(9 * sizeof(float));
* p=2;
* (p+1)=60;
* (p+2)=-23;
.
.
.
* (p+8)=9;
I tried doing this :
float *p;
p = malloc(9 * sizeof(float));
memcpy (p, (float[]) {2 ,60 ,-23, 55, 7, 9, -2.55, -66.9, 9}, 9 * sizeof(float));
Now I am unsure because memcpy copies a static allocated array into my p
. My question is then: my array still remains dynamically allocated?
EDIT: My question refers to 2nd code.
memcpy
copies whatever you pass to it. The compound literal is a temporary automatic object. – Goodden9 * sizeof(float)
takes care of the amount, or did I miss anything? Agree, there is excess, but that should not be a problem, correct? – Nianiabip[0] ... p[9]
(no idea why OP uses the additions)! Accessing unallocated memory is always a problem! – Gooddenconst
, otherwise you might make the resulting code even worse. – Gooddenvoid*
pointer frommalloc()
, just to be super safe. – Karlakarlan*(p+9)=9;
-->*(p+8)=9;
. That coding mis-step is distracting. – Shults