#Simple array issue

54 messages · Page 1 of 1 (latest)

sinful hamletBOT
#

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.

tulip anchor
#

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

open vale
tulip anchor
#

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

open vale
tulip anchor
#

then the allocate-and-copy path seems like the move

open vale
tulip anchor
#

no, allocate enough memory for the entire array, then copy over the full contents of y_coords in a single move

open vale
#

@tulip anchor

tulip anchor
#

you'd need a double*, not a double[], for d2_coords

#

and you can copy it over with memcpy

open vale
tulip anchor
#

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

autumn oar
#

that's a lot of arguments for a function

tulip anchor
#

also that

#

you're also re-declaring dn_coords right after mallocing them, which won't compile

open vale
tulip anchor
#

you probably want Nx * sizeof (double) as the size for the memcpy calls

#

now you have a double*[]. you just want a double*

open vale
#

FINALLY. Thank you I really appreciate it.

tulip anchor
#

I need to see the code that's actually generating the error

open vale
#

Split in two because of max message length

sinful hamletBOT
#

@open vale

Please Do Not Delete Posts!

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.

tulip anchor
#

values3D is a double* but you're trying to subscript it twice

open vale
#

Again, sorry about the really basic questions

tulip anchor
#

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];
tulip anchor
#

so besides the whole issue with VLAs being bad, that is not a double*

sinful hamletBOT
#
What Is a VLA And Why Is It 'Bad'?

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.

Compiler Support

✅ 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

open vale
open vale
tulip anchor
#

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

autumn oar
#
const int x = foo();
int a[x]; // the size can vary
open vale
#

Where is double* not used correctly?

tulip anchor
#

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]

open vale
#

Like so? I'm confused about what we are talking about

tulip anchor
#

in your 3D function, not your 1D function

#

and in the signature, not the call

open vale
#

How would I change my function call afterwards?

tulip anchor
#

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

open vale
#

That makes sense to me

sinful hamletBOT
#

@open vale Has your question been resolved? If so, run !solved :)

open vale
#

!solved