When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
54 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
you can't use a pointer to initialize an array like that. Since VLAs are bad, your best bet will be to either allocate new memory of correct size and copy over the contents of y_coords, or just use y_coords directly
How would I do that? Sorry if this is a stupid question, but I'm trying to translate from Python
you could use malloc to allocate new memory (don't forget to free it when you're done)
though y_coords can just be used directly
Unfortunately, this won't work for what I'm trying to do.
then the allocate-and-copy path seems like the move
You are suggesting I use malloc and then copy each element?
no, allocate enough memory for the entire array, then copy over the full contents of y_coords in a single move
double d2_coords[] = malloc(Nj * sizeof(double));
```How would I then set it to `y_coords`?
@tulip anchor
you'd need a double*, not a double[], for d2_coords
and you can copy it over with memcpy
Would that work with my use case?
again, the dn_coords need to be double*, not double[]
and then you're still trying to use = to move the data over, which won't work. use memcpy
that's a lot of arguments for a function
also that
you're also re-declaring dn_coords right after mallocing them, which won't compile
What would be the best way to solve that?
you probably want Nx * sizeof (double) as the size for the memcpy calls
now you have a double*[]. you just want a double*
FINALLY. Thank you I really appreciate it.
I need to see the code that's actually generating the error
Split in two because of max message length
@open vale
Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.
values3D is a double* but you're trying to subscript it twice
So should I use the & or how does that work
Again, sorry about the really basic questions
you should change the parameter type to actually match what you're trying to pass into it and use it as
looks like you're passing this into it:
double rho[Ni][Nj][Nk];
Right
so besides the whole issue with VLAs being bad, that is not a double*
What is a VLA?
A Variable Length Array (VLA) is an array where the size is not constant and depends on a variable.
VLAs have poor compiler support and can lead to inefficient code. The core issue with VLAs is that the compiler doesn't know the size of the stack frame. Without warning flags like -Wvla (Note: -Wvla is not turned on by -Wall nor any other warning flag) it can be easy to create a VLA by accident, even in C++ with some compilers.
✅ available since C99 ⛔ not available in C++ at all ⛔ was never supported by MSVC ⚠ optional feature since C11 ⚠ supported as non-standard extension by GCC, clang
:stackoverflow: What technical disadvantages do C99-style VLAs have? :stackoverflow: What's the point of VLA anyway?
I thought it's not a VLA because I use const?
I'm not really sure where this is a problem?
that isn't what actually does it, but you're right. it's not a VLA
I didn't check where Ni and such came from
const means cannot change, not that it is known at compile time
const int x = foo();
int a[x]; // the size can vary
Where is double* not used correctly?
in your function signature
you have it as just a double* which is what a 1D array of double would decay into
your 3d array would decay into double (*values3d)[Nj][Nk]
inter_d2_values[d2] = inter_1D(d1_coords, double (*values3D)[d3][d2], Nd1, pos_d1);
Like so? I'm confused about what we are talking about
How would I change my function call afterwards?
look at the error. You're declaring values3D in the parameter list as just a double*, so the first layer of subscripting will get you a double. You're then trying to subscript that single double again, which isn't going to work. If you instead declare values3D to be a double (*)[Nj][Nk], (a pointer to double[Nj][Nk]) then the first subscript will get you a double[Nj][Nk], and then the second subscript will get you a double[Nk], which will decay into the double* that inter_1D expects
That makes sense to me
@open vale Has your question been resolved? If so, run !solved :)
!solved