C++ :: Memory Allocation While Initialize Matrix
Aug 7, 2013
I'm about to solve least-squares problems within a C++-Program. (Equations like inverse(transpose(A)*A) * b and so on)
So I use cmatrix as library and there is the following problem:
#include <cmatrix>
typedef techsoft::matrix<double> dMatrix;
dMatrix A(nDataPoints, numCol);
dMatrix z(numCol, 1);
dMatrix b(nDataPoints, 1);
dMatrix (nDataPoints, nDataPoints);
nDataPoints and numCol are globally defined integers which get the values 120 and 13 (just about the size of those matrices).
So, A is not a problem but as soon as it gets to z and b there is a dialog "Out of Memory" and it stops at the line
return HeapAlloc(_crtheap, 0, size ? size : 1);
(in malloc.c).
This program debugged without any problem with same code - now I just edited the size of the matrices. And I tried the same with another library called "Eigen" and I get the same problem - so I guess there is a problem with the heap and I have to do some kind of memory allocation...
View 4 Replies
ADVERTISEMENT
Jun 21, 2013
I have written a program, in which I create a NxN-matrix. As I need some different samples (some different such NxN-matrices), I try to create a new matrix in each recall of a while-loop. The first recall of the while-loop works without any problems, but the second recall crashes, but I get any errors from the compiler. Here is my quell code. The problem should be with line 44:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
[Code].....
View 5 Replies
View Related
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
Oct 17, 2014
Code:
int *p, ar[100];
p = (int *)malloc(sizeof ar);
.. *p is a pointer variable, but what means another * here --> (int *)?
View 3 Replies
View Related
Jan 21, 2014
I am trying to allocate a memory to vector but while running the program,my window appear and PC get halt state then i have to force fully shutdown my PC I found that due to the vector i am getting this problem.
#define SCOPEPLOT_MAXNUM_SAMPLES (1000000)
QHash<int, std::vector<double>*> m_sampleHash;
m_sampleHash.insert(1, new std::vector<double>(SCOPEPLOT_MAXNUM_SAMPLES));
std::vector<double>* vec_p = m_sampleHash.value(1);
for(int k = 0;k < numSamples;k++)
{
count++;
vec_p->at(k) = data_p[k];
}
View 5 Replies
View Related
Apr 15, 2014
vector<Type> vect; //allocates vect on stack and each of the Type (using std::allocator) also will be on the stack
vector<Type> *vect = new vector<Type>; //allocates vect on heap and each of the Type will be allocated on stack
vector<Type*> vect; //vect will be on stack and Type* will be on heap.
What I would like to know is, are all of the above statements true?
View 3 Replies
View Related
Jul 13, 2014
1) int *a=new int[10]
2) int a[10]
What are the exact differences in these two types of methods of allocating memory for an array ? When does 1st method is useful and when does 2nd ?I also read somewhere that in Ist method memory is allocated from heap but i don't know from where memory is allocated in 2nd method and what difference these memory allocations causes.
View 3 Replies
View Related
Dec 18, 2013
Here is my code:
Code:
class Base {
};
class Derived1 : public Base {
};
class Derived2 : public Base {
} class Bar {
public:
void SomeFunc();
[code].....
MSVC2010 throws out compiler error which says:
Code:
no operator found which takes a right-hand operand of type 'Derived *' (or there is no acceptable conversion)
What I don't understand is why? The pointer is an address of 0 element of an array. So what is the problem? I can eliminate the error by using double pointer but it will be an overkill.
View 8 Replies
View Related
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
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
Jul 11, 2013
Small code to show overflow...But when I compile and run - buffer_one is not being overwritten when the byte size of buffer_two overflows .
Code:
#include <stdio.h>#include <string.h>
int main(int argc, char *argv[]) {
int value = 5;
char buffer_one[8], buffer_two[8];
strcpy(buffer_two, "two"); //Put "one" into buffer_one
strcpy(buffer_one, "one"); //Put "two" into buffer_two
}
[code]....
View 3 Replies
View Related
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
Dec 27, 2014
I've been working on a matrix class and I ran into a problem in writing my matrix class. I keep getting 0 as a determinant and with some debugging, I found that I was losing allocated data or something similar. This algorithm I'm pretty sure works because I used this same algorithm in a function I made in python. [URL] .... That's where I found the algorithm.
/*template <class T>
double Matrix<T>::det(T* array, size_t dim, bool recursion)*/
double det(T* array = NULL, size_t dim = 0, bool recursion = false) {
if (recursion == false) {
if (m != n) {
return 0;
[Code] .....
View 2 Replies
View Related
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
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
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
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
Dec 8, 2014
I am trying to initialize a 2D char array of strings into POSIX shared memory to be shared between 3 other processes. There are plenty of tutorials on how to use a pointer to share a single string or an integer between processes, but I could find no examples on how to initialize 1D or 2D arrays using mmap(). I have posted what I have so far below. It is the first program, which creates the shared memory object and initialize the array char files[20][2][100] with the value files[0][0][0] = '