How memory is allocated to an array in C++ [duplicate] - c++

This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
How do compilers treat variable length arrays
(4 answers)
Closed 2 years ago.
In the below program, when does the array c gets initialize ?
If is it at compile time then how I got the output as 20 which is the product of n i.e. 5 and size of integer in C++ i.e. 4 ,after I pass the value of n at runtime. And if it got allocated at runtime then, how is it possible ,as for runtime allocation we have to use new operator which is not use in this program.

Related

where are the variable names stored? [duplicate]

This question already has answers here:
How are variable names stored in memory in C?
(5 answers)
Closed 5 years ago.
It is always advised to use bigger and clearer variable names in programming suppose I define :
int captain=10 ;
I know that 10 is stored but where is the "captain" or the variable name stored and who allocates memory for it?
In the end they aren't stored anywhere unless you have linked in additional debug information.
They're translated into memory (be it RAM or ROM) addesses and address offsets by your toolchains linker.

Reinterpreting bytes of a vector at memory level [duplicate]

This question already has answers here:
Deserialize a byte array to a struct
(6 answers)
Serialization/Deserialization of a struct to a char* in C
(7 answers)
Serialization of struct
(5 answers)
Closed 5 years ago.
I have a vector aVector. It starts at some memory address, let it be 0x00cff87f in this case.
I also have a double D.
Now, when the program accesses the D above, it accesses some other address, of course.
What I need is that when the program accesses D above, to be pointed to address 0x00cff87f, the start of that array, and take the first sizeof(double) bytes as a double.
I tried passing pointer to D to a function and switch it, but that just changed where that pointer was pointing at, once I exited the function, D remained unchanged.
Basically, I need some way to tell the program that four bytes of memory starting at 0x00cff87f are a double and that when I ask for a double named D to get me the number at that address.
I have an array in memory that needs to be decomposed to basic types, but instead of copying everything unnecessarily, I'd rather just tell the program where it already is.
How do I do that?
EDIT:
I have a vector of unsigned chars that I want to read into other types. Something that C# BinaryReader would do with MemoryStream. I don't know how to do it in c++. There are only fstreams, there isn't one that deals with (binary) files already in memory.
double *p = (double *) &aVector;
I'm not sure why you want to do it, but it seems very likely that there is a better way to do what you're trying to do, because breaking type safety and directly accessing memory can lead to lots of weird problems.

I am writing code for CFD 3D solver ,program runs for small size of 3d matrix but it shows segmentation fault for large size [duplicate]

This question already has answers here:
Segmentation fault on large array sizes
(7 answers)
Closed 7 years ago.
I define arrays in this way:
double uo[n+2][m+2][n1+2] ,
vo[n+2][m+2][n1+2] ,
wo[n+2][m+2][n1+2] ,
du[n+2][m+2][n1+2] ,
dv[n+2][m+2][n1+2] ,
dw[n+2][m+2][n1+2] ,
w1[n+2][m+2][n1+2] ,
z1[n+2][m+2][n1+2] ,
z[n+2][m+2][n1+2] ;
Once I made it as static double then error removes but it keeps running and does not terminate.
If m or n are to big, you'll get a stackoverflow because you are trying to take to much space in your stack.
Dynamic allocation should be done in the heap using functions such as malloc()

Char arrays filled with 0xCC after allocation [duplicate]

This question already has answers here:
When and why will a compiler initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?
(9 answers)
Closed 7 years ago.
So after allocating char array in debugging windows I can see my array is filled with 0xCC. What does it mean? (-52 = 0xCC)
Uninitialized built-in types have an in-determined value, trying to read it is undefined behavior.
The actual values you can see depend on the compiler: You might for example see garbage, zeroes or (what seems to be the case in your example) some special value indicating "data uninitialized".
It's there as a sentinel value so that you know that the memory is uninitialized.
See the /GZ compiler switch.

Init local array with zeros in C++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C and C++ : Partial initialization of automatic structure
I'm looking for a fast way to init local array with zeros. (By "Fast", I mean "Fast to type.") When I do the following:
HANDLE hHandles[32] = {0};
does it zero out the first element or all 32?
It initializes all the 32 elements to zero.
See this surprisingly popular answer for details/alternatives. The difference between C and C++ seems to be that in C++ {} will do zero-initialization as well.