Need larger array size

Hello,

I am developing a real-time motion controller for multiple DC motors. During my control loop I want to collect statistical data by saving it into an array, and then outputting the entire array to a file after the control loop is complete. My problem is that if I try to allocate my buffer array(float fBuffer[2000][6]) any larger than 2000, I get a memory fault (core dumped) error. Is there any way to make this array larger? I’ve tried allocating memory for the array using “new” command, but it doesn’t seem to let me allocate a 2-D array.

Any help would be great.

Thanks.

How do you allocated it. The array is 48k in size, that’s not a problem, unless you try to create in on the stack. The problem is probably when you try to access it and you are doing it wrong. Could you post some code.

As for new, it doesn’t care for that. I beleive the C language says pointer must be of at least 6 ( i think ) levels, so the language shoud support 6-D array.

typedef int mytype[2000];
mytype *variable = new mytype[6];

Shoud do the trick.

I am writing a fairly large object-oriented controller. I have summarized how I’ve used the array below.

//myClass.h

#define QUEUE_SIZE 2000
class myBaseClass
{
public:
virtual int myFunction(int)
{
return 0;
}
};

class myClass : public myBaseClass
{
private:
float fBuffer[QUEUE_SIZE][6];
public:
int myFunction(int iNumValues);

};

//myClass.cpp
int myClass::myFunction(int iNumValues)
{
for (i=0;i<iNumValues;i++)
{
//Do stuff …

	//Store values in buffer array
	fBuffer[i][0]=fVal0;
	fBuffer[i][1]=fVal1;
	fBuffer[i][2]=fVal2;
	fBuffer[i][3]=fVal3;
	fBuffer[i][4]=fVal4;
	fBuffer[i][5]=fVal5;
	fBuffer[i][6]=fVal6;
}
 return 0;

}

//Main Program
#include “myClass.h”

int main()
{
myBaseClass *m;
myClass p;

m=&p;

m->myFunction(100);

}

Object p is instanciate on the stack, which means it using a good chunk of stack. Although 50k should not be a problem in your example, in a big program you might be running out of stack. That probably explains the crash.

Instead try

myClass *p = new myClass;

That will instanciate the object on the heap instead of on the help. You could also declare it static. Or have the fbuffer allocated dynamicaly.

In your example you have fbuffer[i][6]. Maybe it’s just related to your example here but fbuffer[0][6] is illegal, as [6] access the 7th element, which does not exists ;-)

Oops. The fbuffer[i][6] was just a mistake in my example. I don’t actually do that in the program. :slight_smile:

I will try declaring the fBuffer to be static, or allocate it dynamically.

Thanks!

Declaring the fbuffer static is not necessarely a good idea. That means every instances of myClass object will share the same fbuffer. It’s the myClass object that must be static not the fbuffer array.