In this sgx-app,am I make some mistakes about new short[]? - visual-studio-2017

I'm running an app with sgx. It's memory usage is less than 128M, I'm sure about that.
0x752EEB22 (KernelBase.dll)处(位于 test-sgx-align.exe 中)引发的异常: 0xA1A01EC1 (参数:0x13B1CDA8)。
0x79021168 (sgx_urts.dll)处(位于 test-sgx-align.exe 中)引发的异常: 0xC000001D: Illegal Instruction。
0x79021168 (sgx_urts.dll) (test-sgx-align.exe 中)处有未经处理的异常: 0xC000001D: Illegal Instruction。
程序“[25268] test-sgx-align.exe”已退出,返回值为 0 (0x0)。
As you see.
sign the enclave
The required memory is 0x18b000.
<EnclaveConfiguration>
<ProdID>0</ProdID>
<ISVSVN>0</ISVSVN>
<StackMaxSize>0x40000</StackMaxSize>
<HeapMaxSize>0x100000</HeapMaxSize>
<TCSNum>1</TCSNum>
<TCSPolicy>1</TCSPolicy>
Succeed.
Maybe I should change the and .I have done it in my compiler(vs2017_pro), but it seems that the setting doesn't work.
Here is the problem code. The numbers n1 and n2 are between 100 and 10000.
paths = new short*[n1 + 1];
scores = new short*[n1 + 1];
for (int i = 0; i < n1 + 1; i++)
{
paths[i] = new short[n2 + 1]();
scores[i] = new short[n2 + 1]();
}
I am sorry that there are some Chinese words in my question.
Can anyone help me? Thanks.

Related

Refactoring: Move (F6) in CLion - why does it work like that?

I have in my main.cpp this function:
double median(double values[], int count) {
int i, j, n = count, t;
for (i = 1 ; i <= n - 1 ; i++)
{
for (j = 1 ; j <= n - i ; j++)
{
if (values[j] <= values[j + 1])
{
t = (int) values[j];
values[j] = values[j + 1];
values[j + 1] = t;
}
}
}
if ( n % 2 == 0)
return (values[n / 2] + values[n / 2 + 1]) / 2.0 ;
else
return values[n / 2 + 1];
}
and I want to move that function into another file. So, I click on this function and then click F6 and then I write some name of the file eg. MathFunctions and then I end up with:
MathFunctions.h:
#ifndef PROJECT_NAME_MATHFUNCTIONS_H
#define PROJECT_NAME_MATHFUNCTIONS_H
#endif //PROJECT_NAME_MATHFUNCTIONS_H
MathFunctions.cpp:
#include "MathFunctions.h"
double median(double values[], int count) {
int i, j, n = count, t;
for (i = 1 ; i <= n - 1 ; i++)
{
for (j = 1 ; j <= n - i ; j++)
{
if (values[j] <= values[j + 1])
{
t = (int) values[j];
values[j] = values[j + 1];
values[j + 1] = t;
}
}
}
if ( n % 2 == 0)
return (values[n / 2] + values[n / 2 + 1]) / 2.0 ;
else
return values[n / 2 + 1];
}
I'm a beginner in C++ and I don't understand why this is working like that. I'd rather expect it to put declarations of methods like:
double median(double values[], int count);
In *.h file and the interior of the method in *.cpp file and then include the *.h file in my main.cpp like that:
#include "MathFunctions.h"
Can someone explain me why this is working like that? What am I supposed to do with the files created by CLion? Should I include MathFunctions.cpp instead of header file in my main.cpp?
Unfortunately this is a CLion bug, tracked by https://youtrack.jetbrains.com/issue/CPP-9329, please create a Jetbrains account and vote for this issue.
I am not sure what you mean by "workaround". To make your code working, you need to
Create the function declaration for double median(double values[], int count); in the MathFunctions.h include file.
In your main.cpp, add #include "MathFunctions.h as you mention
In CMakeLists.txt (and this is not CLion specific, you need to know the basics of CMake), add file MathFunctions.cpp to the list of source files for your executable.
For example, assuming you have a CLion-created CMakeLists.txt, you should have something similar to
set(SOURCE_FILES main.cpp MathFunctions.cpp)
add_executable(foo ${SOURCE_FILES})
Some other random comments:
median() is not a "method", it is a "function", or more precisely a "free-standing function" (that is, a function that is not part of a class).
median() is not really C++, is C, since it uses a low-level C array. You may want to use a std::vector<double> instead.
C and C++ are low-level and, depending from which languages you are coming from, can be confusing :-) I suggest picking a good, recent book on modern C++. Modern C++ refers to at least C++11 and a style of programming where you almost never perform direct memory allocation/deallocation (that is, you don't use new and delete). Good luck in your journey!
Edit
Changed link to Jetbrains bug to the first one reporting the issue.

Error when attempting to use the heap

I've written a program that works for most input, but if I ask it to make a increase precision by using a larger array (about 320x320 was when I started seeing trouble) it crashes. I searched for my issue online and found this similar problem and this tutorial on what to do about it. The problematic part of my original code is below - I had precision=320 and holepop=770.
double spacing = 2.0/(precision+1);
int lattice_int[precision][precision];
for (i=0; i<precision; ++i){
for (ii=0; ii<precision; ++ii){
mindist_sq = 2.0;
lattice_int[i][ii] = 0;
for (iii=0; iii<holepop; ++iii){
xdist = abs(xcoord[iii] + 1.0 - spacing/2 - spacing*i);
ydist = abs(ycoord[iii] - 1.0 + spacing/2 + spacing*ii);
thisdist_sq = xdist*xdist+ydist*ydist;
if (thisdist_sq < mindist_sq){
lattice_int[i][ii] = dint[iii];
mindist_sq = thisdist_sq;
}
}
}
}
I tried to fix it with this change in the first two lines:
int * lattice_int;
double spacing = 2.0/(precision+1);
lattice_int = new int[precision][precision];
(I also put in "delete lattice_int[][];" at the end.) However, I received this error: 'precision' cannot occur in a constant expression
Is it because I'm trying to work with multiple indices? What can I do to work around my problem? Thank you!
Don't use new[], it'll only cause you pain, suffering, memory leaks, use-after-frees, etc.
You can use std::vector in this respect.
std::vector<std::vector<int>> lattice_int(precision, std::vector<int>(precision));
No memory freeing necessary.
Your lattice_int variable is 2d array. You can allocate it using following code:
int precision = 500;
int ** lattice_int;
double spacing = 2.0/(precision+1);
lattice_int = new int*[precision];
for (int i = 0; i < precision; i++)
{
lattice_int[i] = new int[precision];
}
Same way you have to iterate for deletion of each sub array.
Note: This is pure illustration to use pointers for creating two dimensional array. The better way would be to use vector for this.

Heap Corruption detected

This is the way that i have alocated the memory.
Expression = new char[MemBlock.length()];
VarArray = new char[Variables.length()];
for (unsigned int i = 0; i < MemBlock.length(); i++)
{
Expression[i] = MemBlock.at(i);
}
Expression[MemBlock.length() + 1] = NULL;
for (unsigned int i = 0; i < Variables.length(); i++)
{
VarArray[i] = Variables.at(i);
}
VarArray[Variables.length() + 1] = NULL;
when i try to delete it i get the error...
Logic::~Logic(){
delete[] VarArray; -> happens on this line.
VarArray = NULL;
delete[] Expression;
Expression = NULL;
}
in the entire code i dont not make any changes to the new array yet it tells me i hame some currpution, i cant pin point the problem , any help would be great.
VarArray[Variables.length() + 1] = NULL;
accesses memory you do not own since this array is allocated thus:
VarArray = new char[Variables.length()];
The final element in this array has index Variables.length() - 1.
Running this in a debugger ought be be instructive. Some static analysis tools (eg. lint) would highlight this misuse, I believe.
You could also consider using boost::scoped_array or similar to remove the need for manual deletion. A good lesson to learn early on for C++ is to adopt RAII instead of manual memory management, wherever you can.
VarArray = new char[Variables.length()];
VarArray[Variables.length() + 1] = NULL;
You can´t do that, it´s 2 elements to much.
Same for the other array.
Expression[MemBlock.length() + 1] = NULL;
Is Undefined Behavior. As is
VarArray[Variables.length() + 1] = NULL;
In the 1st case you can only index up to MemBlock.length() - 1, and in the 2nd case Variables.length() - 1.
In both cases you are writing past the end of the allocated array and are probably overwriting the control structures used (by the runtime library) to manage dynamically allocated memory.

C++ Segmentation fault: 11

I am getting Segmentation fault: 11 error when trying to run my program (I'm quite a n00b with c++ so take it easy on me). I know it has something to do with memory allocation but I'm not sure what exactly I am doing wrong. Can anyone please help and spot the problem/s?
Basically I'm trying to chop one vector into many small vectors, and analyse each one separately.
std::vector<double> test::getExactHit(std::vector<double> &hitBuffer, double threshold){
int resolution = 100;
int highestRMSBin = 0;
std::vector<double> exactHit(8192);
double* rmsInEachBin = new double[hitBuffer.size()/resolution];
double highestRMSValue = threshold;
for(int i = 0; i<hitBuffer.size()-resolution; i+=resolution){
std::vector<double>::const_iterator first = hitBuffer.begin() + i;
std::vector<double>::const_iterator last = hitBuffer.begin() + i + resolution;
std::vector<double> hitBufferBin(first, last);
rmsInEachBin[i/resolution] = calcRMS(hitBufferBin);
if(rmsInEachBin[i/resolution]>highestRMSValue){
highestRMSValue = rmsInEachBin[i/resolution];
highestRMSBin = i;
}
}
for(int j = 0 ; j < exactHit.size(); j++) {
exactHit[j]=hitBuffer[j+highestRMSBin];
}
return exactHit;
}
Please deallocate all the memory assigned using new or else it will cause memory leak and other bugs also might get introduced because of this .
http://cs.baylor.edu/~donahoo/tools/gdb/tutorial.html
You can debug using GDB , It will be handy to know a debugger if you are programming in C++ .
Hope this info will help you .

ArrayOutOfBoundsException error

I'm writing a program with java that analyses stock data.
I almost got it working but now it gives me an ArrayOutOfBounds Exception.
int n = closingPrices.size();
double[][] cParray = new double[n][1];
for(int i = 0; i < n; i++)
{
cParray[i][1] = closingPrices.get(i);
}
I hope you can give me help on how to solve this problem..
the size of cParray[i] is 1. It can have only one element with the index [0]
so try cParray[i][0] = closingPrices.get(i)
OR double[][] cParray = new double[n][2]