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


ADVERTISEMENT

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 :: 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++ :: 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 :: 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++ :: 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

C++ :: Pointer Incrementing Using Dynamic Memory Allocation?

Feb 13, 2013

I am using a pair of pthreads that call a pair of functions for ping-pong dma data transfer that are used in a loop for data transfer from an acquisition board. For a large # of waveforms, I ultimately run out of PC memory and the program stops. At the end of each function I use the delete[] command to clear memory for reuse, but the pointer appears to advance by the array size used for the transfer until the location exceeds the 2 GB I have for memory. I can see this happening using the Task Manager performance button time plot and window of total memory used continuing to increase to the limit. The culprit for one of the functions (2nd) is:

unsigned char* dataBuffer2 = (unsigned char *) (pci_buffer2.UserAddr);

where pci_buffer1 and 2 have been set up and allocated in main. I also had the following line in each function process:

double* Rin = new double[length];

and it used up memory twice as fast. When I transferred the last line to an area just prior to main and used a constant 1024 for length, the program ran twice as far before exceeding system memory, so it appears that both lines were forcing new memory assignments and moving the pointers accordingly. In addition to using the delete[] command to free memory unsucessfuly at the end of each function procedure, I ended up closing the memory at the end of each procedure, then reallocating it again with the idea that the pointer would be set back to the original value, but it still seems to icrement along. So, neither approach appears to allow reuse of the memory because the pointer continues to march along. Using Visual C++ 6.0 to compile.

View 1 Replies View Related

C++ :: Checking Dynamic Memory Cross-platform?

Feb 10, 2014

Suppose:

cin >> number;
pointer = new type[number + (rand()%number);

So, I wont know the memory allocated for pointer. How can I check it in all OS?

View 4 Replies View Related

C++ :: Fill Value Of Dynamic Array Of Dynamic Arrays?

Jan 4, 2013

I'm writing a program in which I have to use a matrix to represent a file in code. because it's a file, the size of the matrix is undefined, and therefore the matrix has to be dynamic. I found out that my compiler doesn't like dynamic multidimensional arrays, so I was thinking of this matrix as a dynamic (monodimensional) array of other dynamic (monodimensional) arrays. My program (and thus this example) uses unsigned chars.

unsigned char variable=something;
unsigned char**matrix=new unsigned char*[lenghtOfMainArray];
for(int rowNumber=0;rowNumber<lenghtOfArray;rowNumber++)
{

[Code].....

View 2 Replies View Related

C++ :: Two Dimensional Multiplication Table - Dynamic Memory Allocation

Jun 17, 2013

This is the question; Write a function that builds a two-dimensional multiplication table with arbitrary sizes for the
two dimensions.

This is what I have done. I have allowed the user to input whatever size table they want by arbitrarily choosing what value they can input. However I cannot get the board to have blank squares. I thought the char would do it.

Code: #include <iostream>
using namespace std;
char SQAURE_CHAR = {' '};
const int Board_Size = 14;

[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 :: Dynamic Memory Allocation - Resize Array Of Pointers

May 23, 2013

Suppose I wished to reallocate memory (resize) an array of pointers. Why does the following not work?(The program runs, yet yields a faulty segmentation error message. Why?):

Code: char **ptrarr = (char**) malloc(sizeof(char*))

Code: ptrarr = (char**) realloc(ptrarr, (capacity) * sizeof(char*));

View 14 Replies View Related

C :: War Card Game With Linked Lists And Dynamic Memory

Apr 23, 2013

I am struggling to finish up my game of War. There are a few problems I have ran in to. My shuffle function seems to work fine. I believe my problem lies within my game rules. When I run my code, the game usually plays all the way through but almost always ends in 'War'. I am struggling to get the game to restart after running once. I am not sure how to clear the list and start over.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <Windows.h>

[Code] ....

View 4 Replies View Related

C++ :: How To Use Dynamic Memory Allocation And Pointers To Iterate Through The Arrays

Dec 21, 2014

I need to use dynamic memory allocation and use pointers to iterate through the arrays that I have already in this program. I am lost, nothing I do works and where to use the pointers. I am just looking for a push in the right direction so I can finish this project and how I can implement pointers in my program.

#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <stdio.h>
using namespace std;

[Code]...

View 1 Replies View Related

C++ :: I/O Text File Handling And Dynamic Memory Allocation

May 19, 2014

I am working on an OOP assignment (text handler) which part of its description is:

Some lines of text as a story which the delimiter between each story is %%%%%

Each text loaded should only occupy the space needed for the text to fit.

It's required to use dynamic variables of type char*. To be more detailed, the text-handler must contain a vector of such char-pointers (i.e. c-strings), and the parameter in the constructor indicates how many pointers (c-strings) to be contained in the vector. Each loaded text will be represented by a number, so that the first text in the file gets number 0 and the next one gets number 1 ... etc. When you want to access a text, you request the text with a certain number, and then get a pointer in return that may be used to output the text on the screen.

My problem is first to allocate a dynamic memory like char** without defining the number of array elements (Each text loaded should only occupy the space needed for the text to fit. )and then store each story from text file (comprise of a few lines of text) into that dynamically located memory(char **)to be able to do some operation on it later.

View 15 Replies View Related

C :: Dynamic Memory Allocation - User To Choose Size Of Array

Dec 2, 2013

Dynamic memory allocation in array in c programming. I am trying to make the user to choose the size of array they want to engage in the game.

However, i have remove the global variable which contribute the error to my code previously. Now I assigned all the arr individually but not using the global variable. However, i still not get the desired board i want. i still keep getting 9x9 array board.

And i also need limit the board size only from 4 to 9. And how do i do that.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <malloc.h>
#include <ctype.h>
#include <stdbool.h>

[Code] .....

View 3 Replies View Related

C++ :: Append Comma To A String - Dynamic Memory Allocation Error

May 16, 2012

Trying to append a comma to a string. Getting "Segmentation Error" on Solaris when the function is entered the second time.

Code:

// Appends a comma to the given string
void appendComma(char* instring) {
if (instring == NULL) {
instring = realloc(NULL, strlen(","));
strcpy(instring,",");

[Code] .....

View 14 Replies View Related

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 View Related

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/C++ :: 2D Dynamic Arrays - How To Create And Give Values

Apr 23, 2014

I cant get with part of it is wrong, i want to sum every row, too.

// Color Number.cpp : Defines the entry point for the console application.
//  
#include "stdafx.h"
#include<conio.h>
#include<iostream>

[Code].....

View 1 Replies View Related







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