0
Why this code still works
Why this code still works https://sololearn.com/compiler-playground/cI8GGEc7c0jo/?ref=app
6 odpowiedzi
+ 2
Since '0' has an ASCII value of 48, the compiler stores 48 in a. So the dynamic array allocation is 48 bytes, and all is well.
On the other hand, suppose you did this:
char a = 0; // '0';
char* p = new char[a];
p[0] = '1';
Is array size 0?
The surprising answer is yes! Yet despite that, p gets an assigned address and lets you access locations and overwrite heap memory outside the bounds of the array - as can be done with any pointer or array variable in C/C++. In a case like this, you may find that it overwrites your local variables (depending on its source code position in the declarations).
Here is an extreme example of deliberately going out of bounds of an array in order to hack local variables:
https://sololearn.com/compiler-playground/c7vF6GzfXama/?ref=app
+ 2
Ketan Lalcheta I am uncertain where you anticipated it to fail, but I suppose it is in the pointer increment ++p. Clearly by allowing that it sets you up for a memory leak. But indeed you are allowed to increment any pointer. Be careful not to do that and lose the original reference to dynamically-allocated memory without freeing it.
It might be a good idea to make the pointer a const in order to protect from this error.
char* const p = new char[a];
+ 1
1. You're getting caught in my web of arcane coding, Ketan Lalcheta.
void a(x,y,z)
unsigned long x,y,z;
{
This is an ancient way of declaring parameter types in C, and is not recommended. I am surprised that it is still supported. It works the way that you surmised. I did it just for fun.
2. The indices [8], [7], [6] are out of bounds of the array, and are so far out that they intrude upon the stack frame for main() where its own local variables are stored. The values 8, 7, 6 were carefully chosen to align with locations of variables i, j, k. The reverse order matches the order that i, j, k are found in memory after they are pushed onto the stack.
As a C programmer, it is useful to know that array indices get translated to the equivalent pointer + offset.
f[8] becomes *(f + 8)
in the final analysis, and there is no check to prevent the offset from going beyond the declared array size.
0
Thanks Brian
I got rid off memory leak by updating code to move pointer with temp variable and deleting memory allocation with []
Query now is about allocation. Char is 0 and hence is it allocate 45/46 bytes or what ? (Just thought it as asci value of 0) If not, why I can access first or second element of size 0
0
Thanks Brian got an overview now.
Two more questions on your shared code:
1. void a(x,y,z)
unsigned long x,y,z; What this means in function? Is it like another way to declare as
Void a(unsigned long x, unsigned long y,unsigned long z)?
If not, what it is called and probably what's the difference? Why you choose this?
2. Why you have set the [8] , [7] and [6] index ? What made you set these indices only and that too in reverse order.
0
Bro is spam declaring pointers, have you ever tried derefrencing them/ grtting the adress of a said variable?