I am beginner with C++ and I want to know. When do I use new and delete keywords? And what can I use them to do? Only thing I know about the new keyword and delete is that you can do this:
Code:
int Size = 5;
int *ptr = new int(Size);
cout << *ptr << endl;
delete ptr; And more . . .
But when do you use this keywords? I am just curios about them and want to now if them can be really effective to use.
A quick clarification on virtual methods after reading Jumping int C++ by Alex Allain. If a user wanted to extend a class from someone elses library and override its methods that do not contain virtual methods; how would one call the overridden class if it is referred to by its super type
in other words how would someone override a method from someone elses library that does not have virtual keywords.
I'm attempting to write a program that will respond to me a lot like cleverbot or Siri. I can take care of making the program do the things I want it to do such as shutdown the computer, play a song, or whatever but I do not yet understand how to make it read strings I say, and make sense of them using certain words in that sentence. I could probably make an endless if statement to see if a string entered (using cin on a string[]) matches a certain other string, covering a million different sentences with a million different combos of words and spelling and capitalization but I want to enter a sentence and have the program search for keywords and then act on them. For example, with "shutdown" and "computer" being the keywords that make the program run the shutdown operation, so that whether I say "Yo, program, shutdown this computer for me!" or "Shutdown this computer", it will use the common terms here, "shutdown" and "computer" to know what to do.
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.
int NextPixel(int antIndex, int currentPixel , Ant *ant , IplImage *edgeImg) { int left = currentPixel - 1 ; int right = currentPixel + 1 ; int up = currentPixel - edgeImg->widthStep ; int down = currentPixel + edgeImg->widthStep ;
[[Code] ....
if those lines are uncommented , program gives heap error.Why?
How would I delete a row in a datagridview (and in the database) if the datagridview control does not show the primary key? ie the SELECT statement we used to load the DataGridView does not include the primary key column (since it is not relevant to the user)
I'm writing a sorted vector implementation and trying to do it as simply as possible.
Right now, I'm declaring the sorted vector with a protected subclass of vector, then using the "using" keyword to explicitly inherit all methods that aren't related to adding new elements to the vector (so I can control the order).
Eg:
Code: template<typename T, class Cmp = std::less<T>> class sorted_vector : protected std::vector<T>{ public: typedef typename std::vector<T>::iterator; using std::vector<T>::operator=; using std::vector<T>::assign; using std::vector<T>::get_allocator; using std::vector<T>::at; using std::vector<T>::operator[]; //... };
Obviously, this is annoyingly redundant. So what I'd like to do is something using the new "delete" keyword from C++11. Is there any quick, expressive way of deleting specific methods?
Also, it's pretty annoying to have to typedef base_class::type type to inherit a type from a base class. Is there a shorter way to do that?
here's the problem. I want to delete the objects within a vector, although I'm not sure whether I should clear the vector afterwards. Is it really necessary?
Code:
for (i = 0; i < allSales.size(); i++) delete allSales[i];
int *buildTrail(int antIndex, int start, double *pheromones) { int *trail = new int[tabu]; bool *visited = new bool[tabu]; trail[0] = start; visited[start] = true;
[Code] ....
If I comment all lines includes visited word , no exception occurs , Otherwise , exception throws.
Simply put , How can i delete visited parameter as long as its role has been finished? . . . delete visited ; return trail;
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'm experimenting with a custom memory-pool for my application, and I initially planned to override the global new and delete operators to allocate memory in this pool. But since I'm using QT, this will apply to all the QT-related stuff as well. Should I instead just override the new and delete operators per class?
int main() { vec.push_back(b1); vec.push_back(b2);
[Code] ....
And it don't works at all. all i get (when playing with variations of this stuff until it compiles correctlly) is a memory leak.
For example, let say i have b1 address = 1234
I will effectively free the memory at 1234, but for a strange reason, the memory leak is elsewhere, for example, at 2345, and the memory value at this address is equal to... 1234, the address of the pointer i wanted to delete.
making my delete function work. My program does compile but my delete function doesn't work. I haven't finished my last two functions because I am focusing on the delete but how to Sell a title and print the value of all sold titles would be nice as well.
So, i got a directory full of files. Some files are duplicate and i'd like to delete those duplicate files. The only way to recognize duplicate files is their size. So how would i delete them?
is there a way to delete a memory leak ? like when the pointer is no longer pointing to that memory address, and that allocated memory is not accessible, how can i delete that ?
sample:
int *p = new int; *p = 5; //if i dont't delete p here, is that a way to deallocate the dynamically allocated variable above ? p = new int; //since p is no longer pointing to 5, and there's no way of accessing it *p = 10 delete p; //i know this only deallocates the memory address which holds 10, not the one before
"Type a function that take in a string containing numbers and others characters and print on stdout all the numbers contained into the string, where for number is mean every maximal numerical sequence, of numbers not separed by spaces. Numbers printed on exit must be separated by commas. For example, if i put in the function the string "paje27hg78 2k f562" my function must print:
27, 78, 2, 562 "
So i started my code like so: (I WANT TO NOTICE THAT WE HAVE ONLY USED IOSTREAM AND FSTREAM LIBRARY FOR NOW, SO DON'T USE OTHERS LIBRARY ELSE MY TEACHER WON'T CORRECT MY HOMEWORK!)
#include <iostream> #include <fstream> using namespace std;
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
my program is about user can add new contact,search contact, delete single contact, and display all contact. I can run all of the function except for the delete single contact.
here are my coding.and this is the error for my program..
-in member function 'int PhoneBook::deleteNode(std string)' -prevNode undeclared -currNode undeclared - x undeclared #include<iostream> using namespace std; struct contactInfo //node { string name;
I'm doing a daily time record that record every inputed date on a txt file how will have an option or how will I delete a some of the choosen date? i'm using C++