absr007's blog

By absr007, history, 7 months ago, In English

In a program if i use /* int *a = new int [n] / (and using delete at the end), the compiler throws sigbart error but if i use / int a[n] */ it compiles successfully ,Why ?

program : https://ideone.com/e.js/lZfJuL

 
 
 
 
  • Vote: I like it
  • +10
  • Vote: I do not like it

»
7 months ago, # |
  Vote: I like it +1 Vote: I do not like it

True statement is : typename name[size of array];

»
7 months ago, # |
  Vote: I like it 0 Vote: I do not like it

If anyone knows the reason, I would also like to know. I have also faced this problem many times. Nowadays, I am using vector everywhere to prevent all array shit in c++.

  • »
    »
    7 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Is there a reason to use manual dynamic memory allocation (as in not the standard containers which handle that for you) at all in competition programming?

»
7 months ago, # |
  Vote: I like it +6 Vote: I do not like it

If you use new[] to allocate memory, you must use delete[] (not delete) to free it.

  • »
    »
    7 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    https://ideone.com/e.js/4vTJHE

    it is still giving sigabrt or am i doing something wrong ?

    • »
      »
      »
      7 months ago, # ^ |
        Vote: I like it +5 Vote: I do not like it

      There are many out of bound error in your code.

      For example, at line 10: cin >> a[i] can crash your program when i = n.

      Please remember that index in C++ is start from zero.

      • »
        »
        »
        »
        7 months ago, # ^ |
        Rev. 3   Vote: I like it 0 Vote: I do not like it

        ok i get it .thanks

        I figured it out and program is compiling fine now +1