where are the variable names stored? [duplicate] - c++

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.

Related

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

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.

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.

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.

Why uninitialized variable print a strange negative value? [duplicate]

This question already has answers here:
What happens when I print an uninitialized variable in C++? [duplicate]
(4 answers)
Closed 7 years ago.
Why uninitialized variable print a strange negative value ?
int x;
cout << x << endl;
What you're doing (reading the value of an uninitialised variable) is undefined behaviour; anything can happen, from it appearing to work, to printing random values, to crashing, to buying pizza with your credit card.
An uninitialized variable is a variable that is declared but is not set to a definite known value before it is used. It will have some value, but not a predictable one.
When a variable is not initialized , it shows you "Garbage Value".
What that mean is it can be any arbitrary number from anywhere, may be from another running application or random number from big pool of memory.