C++ :: How To Use New And Delete Keywords
Jul 25, 2013
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.
View 6 Replies
ADVERTISEMENT
Mar 11, 2014
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.
ie
something.h
Code:
#include <iostream>
namespace game{
class character{
public:
std::string getName(){return "character";}
};
}
main.cpp
Code:
#include <iostream>
#include <vector>
#include "something.h"
using std::vector
class protagonist : game::character{
public:
virtual std::string getName(){return "protagonist";} };
[Code]...
View 5 Replies
View Related
Nov 17, 2014
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.
View 1 Replies
View Related
Dec 9, 2014
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.
Code:
typedef struct bookingList{
tSpecialty specialty;
struct nodo* intro;
} tList;
[Code].....
View 7 Replies
View Related
Apr 18, 2014
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?
View 3 Replies
View Related
Jun 11, 2014
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)
View 14 Replies
View Related
Dec 1, 2013
My delete is not working. Why?
Code:
struct MatchName{
MatchName(string& searchName) : s_(searchName) {}
bool operator()(const clsStudent* student) const {
return student->getName() == s_;
[Code] .....
View 4 Replies
View Related
May 7, 2013
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?
View 14 Replies
View Related
Nov 10, 2013
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];
allSales.clear(); //Is this step necessary?
View 5 Replies
View Related
Apr 17, 2014
How I can delete a parameter in a function .
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;
View 4 Replies
View Related
Jan 4, 2015
void myfuncion(){
int *a = new int[10];
int b[10];
int *p = b;
delete a
delete p
}
I cant delete b and/or p but how can i check it if i can use delete or not?
i want to check if the pointer is pointing on function temp variable ( those what gets deleted after function ends .
View 5 Replies
View Related
Jan 9, 2015
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.
View 3 Replies
View Related
Sep 2, 2014
int * ptr = new int[5];
p += 2; //p now stores address of 3rd element in array
delete [] p;
what will be deleted??? all 5 elements or just 3rd, 4th and 5th elements of array(in result making a memory leak)
View 3 Replies
View Related
Jun 14, 2014
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?
View 2 Replies
View Related
Jul 25, 2013
I have something like this:
class A {
};
class B : public A {
};
class C : public A {
};
B*b1;
B*b2;
C*c1;
C*c2;
vector<A*>vec;
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.
View 7 Replies
View Related
Oct 9, 2014
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.
#include <iostream>
#include <cstdlib>
#include <string>
[Code].....
View 2 Replies
View Related
Apr 28, 2013
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?
View 4 Replies
View Related
May 1, 2014
How do i delete the first character in a character array?
I want output to be
void DeleteFirst (char S[ ])
{
for( int i = 1; i < MAX_LENGTH; i++)
cout << S[i];
}
i am just writing the wrest of the Array without deleting the first. how do i delete the first?
View 7 Replies
View Related
Nov 23, 2013
I have a deposit of containers and i must delete the cats container. How i can do that?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Cat {
string name;
int age;
[Code] .....
View 2 Replies
View Related
Apr 9, 2014
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
View 6 Replies
View Related
Feb 3, 2015
"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;
[Code].....
View 18 Replies
View Related
Apr 15, 2013
let's say I have class LinkedList:
class LinkedList
{
public:
class Node
[Code]....
Do delete newL:
1) (*newL).~LinkedList
2) delete newL
3) delete[] newL
4) none of the above ;)
If the answer is 4, what should I do?
View 5 Replies
View Related
Apr 19, 2015
Here is the Method Code:
public void DeleteCustomer(string firstName, string lastName,
string address1, string address2, string city,
string state, string phoneNumber, int customerID)
{
[Code]....
.... And here is the response I get:
Error Deleting customer, please check form data. Syntax error in FROM clause.
View 13 Replies
View Related
Nov 23, 2014
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
[Code]....
View 3 Replies
View Related
Nov 16, 2014
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;
[code].....
View 1 Replies
View Related
May 7, 2014
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++
View 1 Replies
View Related