所以我试图从矢量结构中释放内存。当矢量为3或更小时,它工作正常。但是,当它达到4或更高,并且我尝试调用deallocate函数时,它会崩溃。我不完全确定我是否正在进行正确的解构,我需要提示一下它在哪里出错......
void dealloc_vec(Vector * myVector)
{
myVector->size = 0;
//Delete the array of Vectors.
delete(myVector->vectorArray);
//Finally delete the entire vector.
delete(myVector);
}
我的结构就是这个
struct Vector
{
unsigned int size;
Elem *vectorArray;
};
Elem是一个浮动。每当创建大于3的大小时,它会在退出之前崩溃程序。我们正在使用程序化c ++。
Vector *alloc_vec(void)
{
//create a vector
Vector *temp_Vector = new Vector();
//Using getInt from above to grab values for the size of vector, if given 0 it will just be a 0 vector.
temp_Vector->size = getInt((char*)"Please enter a value: ");
/*Test to see if it is less than zero, if it is program will halt.
assert(temp_Vector->size >= 0);
No need to check as unsigned int cannot be negative according to Wtype-limits
The size of vectorArray is now initialized from the size parameter of the structure.*/
temp_Vector->vectorArray = new float(temp_Vector->size);
//Loop through each element and assign a value from the user using getFloat (It looks cleaner with having separate functions).
for(unsigned int i = 0; i < temp_Vector->size; i++)
{
printf("Vector Element %d: ",i);
temp_Vector->vectorArray[i] = getFloat((char*)"");
}
//return the new vector.
return temp_Vector;
}
getFloat和getInt
float getFloat(char* promptMessage)
{
assert(promptMessage != NULL);
float myInput;
char size[100];
bool sucessful = false;
do
{
printf("%s", promptMessage);
//Use fgets to get the input from stdin.
fgets(size, 100, stdin);
//Check if value is anything but zero, if it isn't use this.
if(sscanf(size, "%f", &myInput) == 1)
{
myInput = atof(size);
sucessful = true;
}
else
{
printf("\nPlease enter a correct number: ");
}
}while(!sucessful);
return myInput;
}
int getInt(char* promptMessage)
{
assert(promptMessage != NULL);
int myInput;
char size[100];
bool sucessful = false;
do
{
printf("%s", promptMessage);
fgets(size, 100, stdin);
//Get the size using fgets and sscanf
sscanf(size, "%i", &myInput);
//Size cannot be greater than 65535 or less than 0.
if(atoi(size) > 65535)
{
printf("The chosen value is too large!\n");
}
else if(atoi(size) < 0)
{
printf("Error! Value is too small!\n");
}
//If sscanf is anything but a number, don't do this.
else if(sscanf(size, "%i", &myInput) == 1)
{
myInput = atoi(size);
sucessful = true;
}
else
{
printf("\nPlease enter a correct number: ");
}
}while(!sucessful);
return myInput;
}
答案 0 :(得分:1)
如果您按[]
new Elem [x]
分配了它,那么您应该通过
delete [] myVector->vectorArray;
您可以使用std::vector
来简化编码。甚至是std::unique_ptr
std::unique_ptr<Elem[]> vectorArray(new Elem[x]);
答案 1 :(得分:0)
在struct
中添加一个构造函数,用于初始化成员。 vectorArray
最初指向随机内存位置,删除这些位置将导致未定义的行为。
struct Vector
{
Vector() : size(0), vectorArray(NULL) {}
unsigned int size;
Elem *vectorArray;
};
答案 2 :(得分:0)
试试这个
Vector *alloc_vec(void)
{
//create a vector
Vector *temp_Vector = new Vector; // don't use ()
temp_Vector->size = getInt((char*)"Please enter a value: ");
temp_Vector->vectorArray = new float[temp_Vector->size];
for(unsigned int i = 0; i < temp_Vector->size; i++)
{
printf("Vector Element %d: ",i);
temp_Vector->vectorArray[i] = getFloat((char*)"");
}
return temp_Vector;
}
然后在dealloc
void dealloc_vec(Vector * myVector)
{
myVector->size = 0; // there is no need for this
//Delete the array of Vectors.
delete [] myVector->vectorArray; // no need for the ()
//Finally delete the entire vector.
delete myVector;
}
编辑:
你的getInt / getFloat看起来有点不对
sscanf(size, "%i", &myInput);
//Size cannot be greater than 65535 or less than 0.
if(atoi(size) > 65535)
而只是做这样的事情
int getInt(const char* prompt)
{
int n = 0;
char buf[32];
do
{
printf( "%s", prompt );
fgets( buf, sizeof(buf), stdin );
n = atoi(buf); // will ignore the \n
}
while ( n > 65535 );
return n;
}
for float只需用atof替换atoi(如果合适,可以使用限制)。请注意,如果输入文本,atoi和atof将返回0,因此您可能需要检查是否> 0