0

所以我试图从向量结构中释放内存。当向量为 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;
}
4

3 回答 3

1

如果你分配它,[]那么new Elem [x]你应该取消分配它

delete [] myVector->vectorArray;

 

您可以使用它std::vector来使您的编码更容易。甚至std::unique_ptr

std::unique_ptr<Elem[]> vectorArray(new Elem[x]);
于 2013-11-12T20:57:51.790 回答
0

添加一个构造函数来struct初始化成员。vectorArray最初指向随机内存位置,删除这些将导致未定义的行为。

struct Vector
{
  Vector() : size(0), vectorArray(NULL) {}

  unsigned int size;
  Elem *vectorArray;
};
于 2013-11-12T21:02:49.773 回答
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; 
}

对于浮动,只需将 atoi 替换为 atof (以及适当的限制)。请注意,如果输入文本, atoi 和 atof 返回 0,因此您可能需要检查是否 >0

于 2013-11-12T21:17:16.400 回答