In jumping into C++ it says something like this: It's not necessary but when you delete a pointer it's a good idea to reset it as a null pointer. That if your code try's to dereference the pointer after being freed, your program will crash. This happens to a lot of experienced programmers.
This could corrupt users data. delete p_int; p_int = NULL;
1. If you can deference a pointer after the memory is freed, why can't you just delete the pointer?
2. If you can do 1, how do you delete the pointer using code?
3. Every thing I've read says that free memory is handed out in a sequenced order. I don't believe that is true at all. I may be wrong. Why can't you put the data in any number of places if it will fit. Isn't the compiler smart enough to know where bytes (bits)and pieces are stored?
4. If you storing anything in free memory must use a pointer to it?
5. Can a pointer or something similar be used with stack memory?
#include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; class singleTon {
[Code] ....
this is a singleton pattern first,it doesn't matter, why I could delete this pointer twice?because the gcc compiler?That mean in the surface, "delete pInstance1;" this movement just mark the memory pInstance1 has been deleted but not real?does any one encounter this phenomenon?
I'm tying to create a program that evaluates all possible actions for a certain problem.
So what i'm basically trying to do is to create a sequence of actions, evaluate them to check if it's the best sequence, change the sequence, evaluate again and so on until all possible scenarios are exhausted, leaving the best one in the end.
My approach to this at first was to create a tree of all possible options and then evaluate each branch. Since there are a lot of possible cases i ran out of memory while the program was still creating the tree. I changed this to create just a branch, evaluate it and then modify it.
I was still getting memory problems. I declared a class tNode and declared a vector<tNode*> branch. Then i created all the nodes i needed for that branch with branch.push_back( new tNode() ). When i wanted to modify the branch i simply used branch.pop_back() and again a branch.push_back( new tNode() ). I figured i was getting the problem because although i clear the vector, i don't actually clear the reference in memory. Is this correct? If so, how can i actually delete the memory space and not just the pointer in the vector?
struct Point {int* a; int b;}; vector<vector<Point> > numbers; vector<int> example;
The numbers vector has a matrix of a sort and each of the members are pointing to one member in the example vector. A member numbers.at(2).at(3).a is pointing at example.at(3). Now, can I remotely delete a member in the example vector using the pointers? Like so:
delete (*(numbers.at(2).at(3).a));
I know there is a more convenient way to delete members, but this is a very specific case I'm working on.
An attempt to create a class which is basically a mimic of vector<int> i don't seem to know how to delete pointer x in a destructor to free memory, also on pushback and pushfront methods, i can't free y when i implement delete[] y; y=NULL; i get some NULL out put when cout 'ing the object in main, why is that happening and how do i free memory y.
#include<iostream> using namespace std; class vectorOfint{ int* x; int size; public: vectorOfint();
Goal: To allocate some memory as a char*, read in some binary data, re-interpret it as a float* and then free the memory.
My code looks like:
void someFunction(float* &result) { char * tmp = new char[1000]; //...Fill the char buffer here... result = (float*)tmp; //Reinterpret binary data as floats
[Code] ....
Is the cast back to char* necessary on the red line (or could I have validly left it as float*)? Would it be different if I had written char * tmp = (char*)malloc(sizeof(char)*1000); on the blue line (and correspondingly used free (char*)floatData on the red line?
i have been fiddling with pointers but I don't understand how the proper syntax is written when I want to acces an element of an array through a pointer to a pointer...The code is all mostly just random bs for learning purposes. I marked the problem "// THIS LINE"
This is a sample program that declares a Matrix as a structure with an array as a pointer to a pointer. The following program is supposed to store a matrix in the structure "_Matrix" and later print the matrix just entered but it fails giving me a "segmentation fault". The sample code is given below
Code: #include <stdio.h> #include <stdlib.h> struct _Matrix { int row_size; int col_size; int **mat;
how to delete an element(s) from an array; suppose I have an array x[10] = {1,2,3,4,5,6,7,8,9,10}, and I want to delete array{5} so that the values of the array become {1,2,3,4,5,7,8,9,10}; how do I go about this? This is not the same as setting the value of array{5} to null; but completely eliminating it so that it does not get printed alongside the other elements of the screen.
I have this code and so far it does everything it should although I'd like to know how to actually delete the elements from the array so after the duplicates have been removed the size of the array should be 7 and not 10.
#include<iostream> using namespace std; int main(){ int nums[10] = {2,7,2,5,4,0,7,6,9,0}; int a, b, t; int size; size = 10; //array size
We can't use the std::vector, std::map and std::lists implementations.
std::realloc doesn't call the destructor automatically.
If it is theoretically possible, we'll consider it and write lines similar to it, which should remain commented until something similar is possible.
int main(){ #define START 1000000000 #define END 10 unsigned int * array=new unsigned int[START]; delete[START-END]array;//Our suggestion to shrink the array delete[]array;//Delete the array return 0; }
struct b { char fullname[100]; char title[100]; bopname };
and i declare a pointer to the struct as follows
b* bp;
and at runtime,
bp = new b[5];
In the end, is the statement delete[ ] bp enough to release the dynamically allocated memory. Or do i need to separately delete bp[0],bp[1]...
Does delete[ ] bp indicate that the array[ ] pointed by bp has to be deleted?? [I am confused as to how this happens, since the pointer only points at the 1st struct address]
I have to manage a Clinic. I need to delete a booking (for example, if John said he's coming on March 22nd at 15:30 but then he say he's not going to come, I have to delete that booking so another person can use it).
idSearched: the id of the person that is not going to come. I have a lot of specialties and each one has a list. So I ask for the speciality to delete the node (the node contains John's booking). If I don't find it, I return (-1). searchSpecByID return a pointer to the list where the speciality is. So head will point to the first node of the list. In nodeToDelete I have the node I want to delete.
The program detects OK when is the first in the list and when not, but it doesn't delete the node.
I wish to know how to traverse or loop through a dynamic 2D array using pointer to pointer as returned by the code above. Like I would in a static T[20][20] 2D array.
So I'm trying to make an employee list that holds the information so I can then delete or add from that array. I'm getting some errors regarding overloading function.
General Purpose: Delete all "white spaces" in text file, until the read-in char is _not_ a whitespace (mark as start of text file).
Problem: Cannot seem to shift char's over properly. (I think my problem is the inner loop - however other code may lead to this problem - I do not know)
Code:
#include <fstream> #include <iostream> using namespace std;
bool trimWhiteSpace(fstream &file, char * charMemoryBlock) { if (!file.is_open()) {
In my refference book I have got a example with a part saying to access the a[4][0] element of the array (named a) with pointer this can be written:
*((int*)a+4)
I wonder if the cast is really required. The book says it is required so that the pointer arithmetic can be done properly. However I am not sure about it. When I work with pointers defined by myself I don't use casts similar to this one. Is there a difference between a self defined pointer and array name?
I have written this code, and at first glance it does what I want, however I am worried that
a) I am overwriting the array that is apssed from chord.getPattern() b) Im getting a memory leak that I want to get rid of, and c) is there generally a /what is the neater way to do it:
Code: uint8_t* ChordBuilder::invert(uint8_t count, Chord chord) { temp = chord.getPattern(); chord.invert(true); //TODO count is how many times to invert. Moves root aswell however
for (uint8_t i = 0; i < count; i++){
[Code] ....
temp is a member variable of ChordBuilder - and is expressed as: Code: uint8_t* temp; I dont want the pattern that chord stores, and passes with getPattern() to change - I fear it is at the moment?
I would rather not use the "new" but I cant think how to get rid of it, however Im not sure where I would need to put the "delete"?