Hi,
The following statement will give the size.
(char*)(ptr+1)-(char*)(ptr)
Kalyan.S
A pointer is a variable that stores value of address of a variable. Since a pointer itself is a variable, it is allocated a memory location.Pointer to pointer means a pointer which points to the address of a pointer. In other words a pointer to a pointer has the address of the address of a variable.We can have pointers to int, and pointers to char, and pointers to any structures we've defined, and in fact pointers to any type in C, it shouldn't come as too much of a surprise that we can have pointers to other pointers. If we're used to thinking about simple pointers, and to keeping clear in our minds the distinction between the pointer itself and what it points to, we should be able to think about pointers to pointers, too, although we'll now have to distinguish between the pointer, what it points to, and what the pointer that it points to points.
// Assuming you dynamically allocated this array using "new"... delete array[arraysize - 1]; arraysize--;
It depends, especially if you are going to use C++.If you allocated the variable using the malloc call or any of its derivatives you must use the corresponding 'free' subroutine call to delete them.If you use the more modern C++ 'new' operator, then use the 'delete' operator to remove the memory dynamically in the program.
An error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers in the source code, dereferencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.
An error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers in the source code, dereferencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.
Example: void foo( MyClass& object ){} // function with call by reference signature MyClass* p = new MyClass(); // instantiate a pointer to MyClass foo( *p ); // call by reference using the pointer
1. Using memory before/after the allocated area.2. Using the memory after deallocation.3. Forgetting deallocation.4. Multiple deallocation.5. Non-checking whether the allocation was succesfull or not.Not releasing dynamically allocated memory when it is no longer needed can cause the system to run out of mmory prematurely .this sometimes called a"memory leak"
Delete is a built-in operator used forcibly release a dynamically allocated resource. Delete can only be used on pointer types, including pointer arrays. A destructor is a class method used to clean up any resources acquired by objects of the class. Destructors are invoked automatically from the most-derived class to the least-derived class, as soon as an object falls from scope. When you delete a pointer to an object, the object's destructor sequence is invoked. Note that you must not delete named objects otherwise you end up with a null reference. References must never be null.
pointer is used when we want to retain the change in values between the function calls. Similarly double pointer is used when we want to retain the change in pointers between the function calls. If you want to modify pointers than you need double pointer to retain the change.
There is no concept similar to pointers in Java. Pointers are a feature in C programming using which a programmer can access the memory. This was the cause of major catastrophic programming bugs. The creators of Java excluded this feature just to avoid such catastrophic bugs.
// Inventory Pointer // Demonstrates returning a pointer #include <iostream> #include <string> #include <vector> using namespace std; //returns a pointer to a string element string * ptrToElement(vector<string>* const pVec, int i); int main() { vector<string> inventory; inventory.push_back( "sword"); inventory.push_back( "armor"); inventory.push_back( "shield"); //displays string object that the returned pointer points to cout << "Sending the objected pointed to by returned pointer:\n"; cout << *(ptrToElement(&inventory, 0)) << "\n\n"; //assigns one pointer to another - inexpensive assignment cout << "Assigning the returned pointer to another pointer.\n"; string* pStr = ptrToElement(&inventory, 1); cout << "Sending the object pointed to by new pointer to cout:\n"; cout << *pStr << "\n\n"; //copies a string object - expensive assignment cout << "Assigning object pointed by pointer to a string object.\n"; string str = *(ptrToElement(&inventory, 2)); cout << "Sending the new string object to cout:\n"; cout << str << "\n\n"; //altering the string object through a returned pointer cout << "Altering an object through a returned pointer.\n"; *pStr = "Healing Potion"; cout << "Sending the altered object to cout:\n"; cout << inventory[1] << endl; return 0; } string * ptrToElement(vector<string>* const pVec, int i) { //returns address of the string in position i of vector that pVec points to return &((*pVec)[i]); }
I think you're referring to the C/C++ concept of "dangling pointers." This is when you allocate some memory to a pointer, then deallocate that memory, but don't change the pointer. This causes any attempted use of the pointer to return an unused memory address. There is no such concept in Java, since the programmer has little to no control over how memory is allocated or freed. The closest thing I can think of is if you're using a class such as a Reader, in which you can close the object (Reader.close()) and then still have a reference to it. But in this case (and other similar cases) attempting to use the Reader further will result in an IOException being thrown.