#while i was trying to DELETE a pointer with delete[] operator, it displays an error, why?

48 messages ยท Page 1 of 1 (latest)

topaz condor
#

void sort(float *board, int how_many)
{
for (int j = 0; j < how_many; j++)
{
for (int i = 0; i < how_many - 1; i++)
{
if (board[i] < board[i + 1])
{
float replace = board[i];
board[i] = board[i + 1];
board[i + 1] = replace;
}
}
}
}

int main()
{
srand(time(NULL));
int how_many;

std::cout << "How many random numbers?" << std::endl;
std::cin >> how_many;

float* board = new float[how_many];

for (int i = 0; i < how_many; i++)
{
    //std::cin >> board[i];
    board[i] = rand() % 100 + 1;
}

sort2(board,how_many);

std::cout << std::endl;

for (int i = 0; i < how_many; i++)
{
    std::cout << *board << std::endl;
    board++;
}
delete[] board;

}

sonic mossBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

noble olive
#

What error?

topaz condor
#

@noble olive

#

i code in visual studio

noble olive
#

You have changed the board pointer.

topaz condor
#

so that now i cant delete it?

sonic mossBOT
#
void sort(float* board, int how_many) {
  for (int j = 0; j < how_many; j++) {
    for (int i = 0; i < how_many - 1; i++) {
      if (board[i] < board[i + 1]) {
        float replace = board[i];
        board[i] = board[i + 1];
        board[i + 1] = replace;
      }
    }
  }
}

int main() {
  srand(time(NULL));
  int how_many;

  std::cout << "How many random numbers?" << std::endl;
  std::cin >> how_many;

  float* board = new float[how_many];

  for (int i = 0; i < how_many; i++) {
    // std::cin >> board[i];
    board[i] = rand() % 100 + 1;
  }

  sort2(board, how_many);

  std::cout << std::endl;

  for (int i = 0; i < how_many; i++) {
    std::cout << *board << std::endl;
    board++;
  }
  delete[] board;
}
ManaHakaPl
noble olive
topaz condor
#

aaa okay'

#

so

#

i cant make any operation on this pointer to delete it?

plain zephyr
#

learn to use std::vector<float> please

#

using new/delete like that is a terrible idea

topaz condor
#

okay

noble olive
hollow wadi
plain zephyr
hollow wadi
plain zephyr
noble olive
hollow wadi
hollow wadi
#

oh and also tell me, tomorrow when we manage compilcated resources like database connections, how are you gonna use new when that is not how the resource is created?

#

or other complicated handles

plain zephyr
noble olive
#

But really it's not that good of an analogy anyway.

hollow wadi
plain zephyr
#

and also new/delete isn't taught correctly anyway, because it is almost always taught in a way that is simply wrong in the presence of exceptions

hollow wadi
hollow wadi
hollow wadi
noble olive
#

It should be explained, but the best time for that is later.

#

But really users of other languages don't even flippin' bother.

hollow wadi
#

horrendous mess

noble olive
#

Not sure how that's related.

hollow wadi
thorn verge
#

I partially agree with kolio here, knowing how to manage memory is great, especially because after suffering with memory management so much you'll appreciate STL 10x more

topaz condor
topaz condor
hollow wadi
lofty rock
#

Hey guy,what is the "cin" function mean

noble olive
#

It's a "character input" variable.

distant gale
# lofty rock Hey guy,what is the "cin" function mean

A) Don't post in other people's threads. Either create your own or for such small questions you can just ask in #cpp-help-text
B) It's not cin, it's std::cin. This differentiation is important, because cin, for all we know, could just be a local variable in your code.
C) std::cin is not a function, it's a variable of type std::ostream. The reason why it allows you to print to the console when used like this: std::cin << something is because the << operator is overloaded like so:

std::ostream& operator<<(std::ostream& o, const type_of_something& something) { ... }
```and whenever you do `std::cin << something`, that function there is being ran, which then handles the output.