C++ :: Dynamic Memory - Create Multiple Instances Without Explicitly Call Delete

Mar 28, 2014

look at this code:

#include <iostream>
using namespace std;
class teste

[Code]....

Each time i call "x = new teste();" the previous object is deleted? Does this code cause any sort of memory leak?

View 1 Replies


ADVERTISEMENT

C++ :: Dynamic Memory (array Of Char) - Delete All White Spaces In Text File

Feb 8, 2014

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()) {

[Code] ....

View 3 Replies View Related

C :: How To Use Malloc Or Calloc To Create Array In Dynamic Memory

Mar 10, 2014

What is wrong with my function why does it spit out huge numbers? And how do i use malloc or calloc to create an array in dynamic memory, and return a pointer to this array

Code:

#include <stdio.h>#include <stdlib.h>
int fibonacci(int n)
{
int i;
long int fib[40];
fib[0]=0;
fib[1]=1;
for(i=2;i<n;i++){
fib[i] = fib[i-1] + fib[i-2];

[Code]....

View 12 Replies View Related

C :: Making Fork Call And Have The Instances Run Every Five Minutes

Dec 18, 2014

Understanding Fork() calls. I have to write a C program in UNIX machine, and make two Fork Calls Inside it and have these two instances run every five minutes all round the clock. How to achieve this.

View 1 Replies View Related

C/C++ :: Create Local Instances Of A Global Variable?

Oct 4, 2012

is it possible to have a global variable pointing to a different address depending on the thread?

Imagine one would like to use threads with the loop:

for (i=0;i<n;i++){
globalPointerVariable=getAddress(i);
DoThingsUsingThe_globalPointerVariable();
}

View 4 Replies View Related

C/C++ :: Create Class Instances From Text File Stream

Nov 10, 2014

I am trying to do some exercises but am struggling at the moment. The first task was to create a class (Planet), then allow the user to edit their entry or view it.

The second one is to create class instances from a text file. The file contains a new planet on each line in the form: 'id x y z' where x/y/z are its coordinates. As the file can have more then one lines, it has to dynamically create an undefined amount of class instances.

To do this I used 'new' and it works ok - it prints each one out to the screen as you go so you can see it working. However... I'm trying to get into good habits here and am encapsulating the class which is where I am getting stuck. I can read from the class but cannot put the values from the file into the class.. ..using the member functions I have created anyway.

My code so far is:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Planet {
private:
int id=0;
float x_coord=0.0, y_coord=0.0, z_coord=0.0;
public:
int GetID(){return id;}

[code]....

If I change the SetID etc to just p->id, p->x_coord etc it works fine. But I'd rather find a way to do it that keeps the encapsulation. Using p->z_coord etc requires that you change the class variables from private to public.

The question I have been given is this:

Define and implement a function, generate planet, that takes a stream argument that has already been connected to a file (i.e. the argument is istream& fin). The function must create a new instance of planet using new and read its details from the next line in the file.Each line of the file is in the format id x y z.The function must return the newly created planet.

Also, how would you go about 'viewing' one specific class instance once they've been created? So say the file had 5 lines, line three was '4 6 2 6'. How would I go about viewing that planet afterwards? I don't think thats required but... I'm just wondering Although I'm also wondering, are we actually creating a new class instance for each line here? Or just destroying the previous one?

View 14 Replies View Related

C++ :: Dynamic Memory Is Affecting Non-dynamic Values

Oct 7, 2014

i'm implementing a playerclass for a game.. in the game there are multiple player types, weapons ect.. i just wanted to turn my players weapons into a dynamically allocated c_str. once i added my: Destructor, Copy Constructor and Overloaded Assignment Operator. My initial values became corrupted and i cannot fix them.

Player2.cpp
#include <string.h>
#include <iostream>
#include <cstdlib>
#include <iostream>
#include "player2.h"
#include "dice.h"
#include "gamespace.h"

[code]....

View 1 Replies View Related

C :: Create Function To Create A Dynamic Array That Fill With Randomly Generated Integers From 0 To 50

Oct 26, 2013

I have a struct called Array and I'm to create a function to create a dynamic array that's fill with randomly generated integers from 0 to 50 (inclusive) and a function to destroy the array for freeing its memory. Below the code that I have written so far.

Code:

* Struct */
typedef struct {int *pArray; //the dynamic array
int length; //the size of the dynamic array}Array;
/* Function to create a dynamic array */
Array *initializeArray (int length) {int i;
}

[code]....

View 7 Replies View Related

C/C++ :: Pointers - Scope Of Dynamic Allocation And Delete

Apr 13, 2014

I have the following code here. My questions are:

1. Pointers can be used as pass by reference. When I dynamically allocated memory for array[50] in the run function, does that mean I am changing the size of the pArray in main as well? Or does the scope of array[50] ends with the function run? if so, should I do a delete [] Array inside the run function?

2. When I do delete[] pArray in main, what does it delete? memory for array[50]? or array[100]?

#include <iostream>
using namespace std;
void run(int* Array, int& s) {
s = 50;
Array = new int[s];

[Code] ....

View 10 Replies View Related

C++ :: How To Delete A Memory Leak

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

Visual C++ :: Delete Earlier Dynamic Controls And Redraw In Dialog

Apr 6, 2015

I've created a dialog based MFC app which input from user to create that many dynamic controls(Used checkbox to be created dynamically).

After user input that many checkboxes controls are created but if try to click again then not able to delete previous controls and again new no. of controls.

Code snippet to add controls on user input.

RECT rctA;
rctA.top = 51;
rctA.bottom = 80;
CString temp;
m_Edit.GetWindowText(temp);
int noOfControls = _tstoi(temp);

[Code] ....

View 1 Replies View Related

C :: Dynamic Memory Allocation

Jul 14, 2013

I have declared a global variable as pointer. The program performs certain number of iterations. After every iteration, the size of memory required for the pointer changes and this pointer variable is to be accessed by different functions. Now, here is my doubt:If I allocate the memory for this global variable in a function, will the contents of the memory be lost once I exit that function. In my opinion, it should not be the case as the dynamic memory allocation takes place in "heap" and should not be affected by the call of functions.

View 4 Replies View Related

C++ :: Dynamic Memory Errors

Feb 15, 2013

I am new to C++ language and I am still learning.I'm doing basic stuff to better understand dynamic memory. I was wondering why I keep getting memory issues.*/

#define SIZE 15
class word {
char* str;
public:

[code]....

View 1 Replies View Related

C# :: Arrays / How To Delete Multiple Entries

Nov 2, 2014

I have to create a program that holds three arrays one for the persons last name one for the points scored and one for the player number, now i've got all the arrays and things done but i'm confused as to how i'm going to delete an entry so far the options i've got working are create player, list player, update player, retrieve player and exit.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PlayerSystem6 {
class Program {
static void Main(string[] args){

[code].....

View 4 Replies View Related

C++ :: Dynamic Memory Allocation (arrays)

Feb 24, 2014

I have recently bought a copy of "Jumping into C++" and have come to chapter 14 ( dynamic memory location) and have a question.

On page 153-154 an example of dynamic allocation is given for array's of int. How would the code look like for strings or structs ?

The allocation was given by:

Code:
int *growArray (int* p_values, int *size)
{
*size *= 2;
int *p_new_values = new int[ *size ];
for ( int i = 0; i < *size; ++i )
{
p_new_values[ i ] = p_values[ i ];
}
delete [] p_values;
return p_new_values;
}

Sample Code I tried to use this for an array of structs but failed completely....

I used the following struct Code:

struct user{
int days;
string name;
};

and the allocation function (which does not work):

Code:
struct user *growarray (struct user *p_values, int *size) {
*size *= 2;
struct user *p_new_values = new struct user[ *size ];
for ( int i = 0; i < *size; ++i )

[Code].....

View 8 Replies View Related

C++ :: Connect 4 Using Dynamic Memory Allocation?

Jul 3, 2013

I am creating a connect 4 game using dynamic memory allocation the question is;

Write a two-player game of Connect Four where the user can set the width and height of the board and each player gets a turn to drop a token into the slot. Display the board using + for one side, x for the other, and _ to indicate blank spaces.

I have created the board. However I am unsure as how to make a start on getting the players to make moves.

Code:
#include <iostream>
using namespace std;
char **create_table(int width, int height, char blank) {
char **p_p_connect4 = new char*[height];
for(int i = 0; i < height; i++) {
p_p_connect4[i] = new char [width];

[Code]....

View 1 Replies View Related

C++ :: Dynamic Memory Allocation In A Class

Apr 17, 2014

I'm having problems with this code:

#include <iostream>
using namespace std;
class Foo {
public:
Foo( int n );// Constructor
~Foo();// Destructor
int *ptr;
int N;

[Code] ....

I'm using Visual C++ 2008 version. The problem arises at the end, after the sentence 'system("pause")' is reached, which makes me think that the problem happens when calling the destructor. The destructor is called twice, the first time it's called is in the function print. The problem seems to be that the destructor can only be called once.

I know I can avoid this situation by defining the function print like this:

void print ( const Foo &f )
...

but I would like to know if there is some way I can do this keeping the definition that I've provided.

View 2 Replies View Related

C++ :: Freeing Dynamic Arrays From Memory

Sep 27, 2013

So I'm trying to wrap my head around dynamic arrays, and I've finally figured out how to build one, using

int arraysize = randomnumber;
int* arrayname = new int[arraysize];

Now, how to delete the array. Can I free the whole array from any scope in my program using a simple line like delete arrayname;?

Also, since I'm working in heap, I can't rely on the debugger to show me when I'm doing something wrong, If I create a struct such as :

struct twoints {int one, int two};

and then try to create a dynamic array of said struct using

int arraysize = 5;
twoints* arrayname = new twoints[arraysize];

Am I basically creating an array of 5 two ints?

//is it the same as doing this?

twoints arrayname[5];

View 8 Replies View Related

C++ :: Dynamic Memory Limit With Operator New?

May 13, 2014

In my platform, Windows 7 Ultimate 64 bits with Service Pack 1 over a x86-64 AMD microprocessor, AMD Phenom II 1090T X6, with a total of 4 GBytes of RAM memory), one C++ program can only allocate up to 2 GBytes of dynamic memory (using operator new, so Heap memory).

In am using the IDE Microsoft Visual C++ 2010 Express Edition to compile my C++ program and generate the executable file (for Win32).

In my system, there are much more available memory than 2 GBytes (yet remain 1 GByte of RAM memory and the virtual memory), so, Why can't my C++ program allocate more than 2 GBytes of memory? Do I need to configure something in the IDE to allow more memory to the program?

View 5 Replies View Related

C++ ::  Multiple Declaration Of Global Delete Operator

Jul 14, 2013

How can I declare global 'delete' operation multiple times? Like, I've an implementation of global 'delete' operation in a file 'x' but I want a different behavior in file 'y'. However, if I override it again in file 'y' I get multiple definition error.

View 3 Replies View Related

C++ :: Allocate Memory In A Function And Call From Main

Aug 30, 2013

So my assignment is to create a program that calls for a function in main that dynamically allocates an array[3] and then have pointers with multiple levels of indirection and pass them by reference so they are not lost after the function. Here is my code:

#include <iostream>
#include <array>
#include <iomanip>
#include <string>

[Code]....

Next part is to ask user for two non-negative numbers and then get the length of those numbers and create an array. for the size of each number they input. Then to separate those numbers and add the cross-sums.

View 6 Replies View Related

C++ :: Vector Of Int Class - How To Delete Pointer X In Destructor To Free Memory

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

C :: Sorting Algorithm With Dynamic Memory Allocation

Nov 17, 2013

I am fairly new to dynamic memory allocation and I keep getting a segmentation fault in this code of mine. This is what the method should do:void sort StringsByReversePoints(char **myWords): This function sorts the char* values (i.e. strings) of myWords in descending order of point value by calling getWordPoints as a helper function and comparing adjacent words. This simple (but inefficient) sorting algorithm starts at the beginning of myWords array and sweeps to the end comparing adjacent values and swapping if they are out of order. After N (length of the array) sweeps the array is fully sorted. Note that efficiency can be improved by a factor of 2 by shortening each successive sweep by one, since the first sweep will have guaranteed the minimum point value word is the last element of the array, the next sweep guarantees the last two elements are correct, and so on....Additionally, if a given sweep results in zero swaps then the array is sorted and you can return immediately.

View 5 Replies View Related

C :: Double Pointers And Dynamic Memory Allocation?

Nov 16, 2013

we are currently covering double pointers and memory allocation. Currently getScrabbleWords is not working. when I compile with commented code (Main() works fine) I get a segmentation fault.

This is the purpose of getScrabbleWords:

char **getScrabbleWords(char **allWords, char letters[]):

This function takes an array of char* values (i.e. strings) representing all the words read from wordlist.txt. Each of these words is tested by callingcanWeMakeIt as a helper function, and pointers to the words that can be made are put into an array, myWords. Note, copies of the words are not made! In order to indicate the end of myWords, we terminate with a NULL pointer. Thus, if N words can be made from letters then myWords should have length N+1.

View 6 Replies View Related

C++ :: Assign Values To A Dynamic Memory Structure?

Dec 27, 2014

I create some code that assign values to a dynamic memory structure. I'm not sure why one code works and the other crashes when it assigns a value.

Working Code

#include <iostream>
using namespace std;
struct WorldOjectCollisionMap
{

[Code].....

View 6 Replies View Related

C++ :: Why Cannot Dynamic Memory Allocation Work With References

May 5, 2013

Why cant a dynamic memory allocation work with references? I was told that references work with const pointers deep down so shouldn't this be legal code?

int &&a=new int;

My compiler says that a entity of int* cannot be used to initialize a entity of int&&?

Does that mean that the compiler thinks of them as different types except deep down a reference is implemented with a pointer? Is this right?

View 14 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved