C++ :: Check If Can Do Delete Pointer
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
ADVERTISEMENT
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
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
Aug 7, 2014
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?
View 13 Replies
View Related
Feb 22, 2012
environment : qt creator 4.7
code:
#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?
View 7 Replies
View Related
Jul 29, 2013
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?
View 6 Replies
View Related
Apr 17, 2013
I have following:
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.
View 3 Replies
View Related
Dec 3, 2013
I have little problem which causing memory leaks.
Parent > Multiple Child(Parent parent) > Child destructor deleting parent => next Child destructor crash
Example code: without using:
class Parent {
public:
Parent() {
for(int i = 0; i < x; ++i) {
for(int j = 0; j < y; ++j)
childs[i][j] = new Child(this);
[Code] ....
If you read code, on Parent destructor i = 0 & j = 1 its going crash.
Parent will be deleted aswell, but it give me assert: _block_type_is_valid(phead- nblockuse)
View 3 Replies
View Related
Feb 14, 2013
lets say I have a pointer p_unit of type c_unit* (c_unit is an a.b.c.)
I have a function that returns a pointer to a new c_unit object:
c_unit * man_add_unit() {
c_unit * local_p_unit;
unsigned short int local_run_code;
print_man_add_menu();
local_run_code = get_a_run_code(); // this bit just gets user input
switch (local_run_code)
[code]....
I assign that to p_unit, then add it to a vector v_units:
p_unit = man_add_unit();
v_units.push_back(p_unit);
cout << "New unit added.
";
The whole program runs on a loop, and another thing the user can do is to print out data on c_unit objects pointed to by v_units. The problem is, in that function up there ^ I give the user the option to go back to main menu without creating a unit.
Since "local_p_unit" is declared but not assigned an initial value, I'm guessing the function would return a "null" pointer (which is what's hanging me up). If I just let this run with the above code, and go to print out the unit data, the program crashes.
I tried to make an if thing with p_unit == 0 but this always returns false and doesn't catch the "bad" unit that will subsequently cause a crash.
Btw, I have considered assigning a reference to a generic c_unit object to that there local_p_unit so it won't return null, then remove pointers to that object from v_units at the end of the loop.. But I know there's got to be a better way.
View 4 Replies
View Related
Feb 25, 2015
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();
[Code] .....
View 6 Replies
View Related
Dec 22, 2012
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?
View 9 Replies
View Related
Jan 3, 2015
I'm having an issue coming up with an if() statement to check if a word match the one in the value of a pointer's address. So far the best I've come up with only matches the first letter of the words, you'll find it in the code below.
#include"Header.h"
int Colour(struct MyStruct *ArrayPointer, int ArraySize) //ArraySize = 3 for this run. {
int ColourCount = 0;
for (int i = 0; i < ArraySize; i++) {
[Code] ....
An example run you can see in attached pic.
I want to have an if statement that only accepts "Red" and not the occasional "Ravaged_Anus".
I'm using MVS Express 2013, .c source files, and the C++ compiler.
Attached image(s)
View 3 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
Jan 17, 2014
I was having problems changing the value of my head node I passed it as an argument as head which would be the address. The parameter was defined as struct node *head. like this
bool deleteNode(struct node *head, struct node *delptr)
I tried manipultaing pointer values to change head node value but it did not work. I saw some code online which used pointer to pointers(in code below) to change head node value it worked I dont fully understand why. Would like better understanding of why.
Would also like to know why the argument call needed &head instead of just head.
remove = deleteNode(&head,found); opposed to remove = deleteNode(head,found);
#include "stdafx.h"
#include<iostream>
struct node{
[Code].....
View 1 Replies
View Related
Aug 19, 2014
I am attempting to implement function pointers and I am having a bit of a problem.
See the code example below; what I want to be able to do is call a function pointer from another pointer.
I'll admit that I may not be explaining this 100% correct but I am trying to implement the code inside the main function below.
class MainObject;
class SecondaryObject;
class SecondaryObject {
public:
[Code]....
View 10 Replies
View Related
Nov 27, 2013
I have to write a program to print pascals triangle and stores it in a pointer to a pointer , which I am not entirely sure how to do. I also have to write the file and read it, then create a binary file. Assignment is attached. I am not the best with programming and especially with pointers. I will post my code below.
Code:
#include <stdio.h>
#include <stdlib.h>
void writePascalTriangle(char *fileName, int heightOfTriangle, int **triangle) {
FILE *fp;
fp=fopen("writePascalTriangle.txt", "w");
[Code] ....
View 4 Replies
View Related
Apr 19, 2014
I'm making a system like twitter for class called ShoutOut.com I want to be able to get the PublicShoutOut pointer pointed to by the start iterator and assign it to firstShoutOutToDisplay and secondShoutOutToDisplay because I need that in order to pass the pointers to one of my functions. When I step through the debugger the values in start are all default values like "" and so are the values in this->firstShoutOutToDisplay but the message that start points to is being output just fine.
EDIT: got rid of irrelevant code. Am I using the correct syntax to do this?
if (start != finish) {
//getting these because a shoutout needs to be passed to the function that displays
//options for a shoutout
this->firstShoutoutToDisplay = (*start);
[Code] ....
View 2 Replies
View Related
Mar 7, 2013
I have a function:
const void insertStuff(const void *key, const int value){
// I want to convert the void pointer into one
// of three types of pointers(int, string, or double)
switch(value){
case 0:
int *intPtr = key;
[Code] .....
But this causes an error of: "crosses initialization of int*intPtr"
What's the correct way of implementing this?
View 1 Replies
View Related
Dec 25, 2013
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"
Code:
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#define MAX_DATA 100
int find_average(char *iden, ...) {
[Code]...
View 2 Replies
View Related
Jul 16, 2013
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;
[Code] ......
View 1 Replies
View Related
Jun 20, 2014
i really don't know why has a error in my code, that pass a pointer of pointer (name of a matrix with 2 dimensions). Here is the source code of a simple example where appears segmentation fault when execute (but compiles normal):
#include <stdio.h>
#define LINHAS 3
#define COLUNAS 5
float a[LINHAS][COLUNAS];
void zeros(float **p,float m, float n){
int i,j;
for(i=0;i<m;i++)
[Code]...
View 6 Replies
View Related
Mar 4, 2015
I need to make a copy of a string that is defined by char *full and copy it into a different pointer defined by char *duplicate. I have written code to do this however it will not work and i cannot figure it out my code is as follows:
char *duplicate = (char *)malloc(strlen(full) + 1);
strcpy(duplicate, full); /*Make second version of full*/
char *Ptr = strtok(duplicate, " "); /*Split duplicate up*/
I have a full program written but i know this is where the problem is because i have used printf statements to see where the program fails. I get no errors and it compiles successfully but it hits this point of the program and it just stops and windows automatically shuts down the program.
char *full is pointing to:
"To be, or not to be? That is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles,"
I need to duplicate the string because i need to use strtok but i will need the original string later on so i need an unaltered version.
View 9 Replies
View Related
Jan 3, 2013
I have the following code :
Code:
#ifndef TDYNAMICARRAY_H
#define TDYNAMICARRAY_H
namespace Massive {
template<class T>
T **AllocateDynamic2DArray(int nRows,int nCols)
[Code] .....
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.
View 8 Replies
View Related
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
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