1

I have allocated a dynamic memory using int *p = new int[size]; Now when I try to delete it using delete [] p; I'm getting a segmentation fault(core dump) while executing the code.

Initially I was able to enter array elements dynamically and and it was working normally. But, after executing certain number of times, now it says segmentation fault. I have a function where I have allocated memory using new later at the end of the scope of that function I have included delete [] p. Am I supposed to include delete in main function?

#include<iostream>
using namespace std;

void input(){
    int n,d, i= 0, count;
    cout<< "number of variables: "<<" ";
    cin>>n;
    cout<<"enter number of minterms: "<<" ";
    cin>>count;
    int x =  pow(2, n);

    int *p = new int[count] ; //dynamic allocation

    for(i = 0; i<count; i++)
    {   
        cout<< "enter the minterms in decimal: ";
        cin>>d;    
        p[i] = d;
    }

    for( i =0; i< count; i++){
        cout<<p[i]<<" ";
    }

    delete [] p; //(Do I need  to write delete over here or in the main                           
    //func, Right now I've used it here(i.e,the user defined function.)
    cout<<"successfully deallocated";
}

//Main function:
int main(){

    int *p = NULL; //Is this required ?


    input();
    //delete[] p; (Do I need to mention delete over here?)
    return 0;
}

number of variables:  4
enter number of minterms:  8
enter the minterms in decimal: 1
enter the minterms in decimal: 2
enter the minterms in decimal: 3
enter the minterms in decimal: 4
enter the minterms in decimal: 5
enter the minterms in decimal: 6
enter the minterms in decimal: 7
enter the minterms in decimal: 8
1 2 3 4 5 6 7 8 successfully deallocated00000000119614428832765154679997521907-10100852163265911961440643276540008000800080004000-1005...<a lot of numbers>...07370419492536907748609097595Segmentation fault (core dumped)
2
  • 1
    1. Avoid huge output text in the questions if you can. 2. You are using C++ as C. Use smart pointers, use nullptr instead of NULL Mar 31, 2019 at 11:13
  • There is nothing in your code that could produce that output.
    – molbdnilo
    Mar 31, 2019 at 11:20

3 Answers 3

1

This code works fine under clang, g++ and vc++. Example: https://rextester.com/PBN39654

This makes me think that there's either something wrong with your environment that you build the code, or something else is triggering the core dump after main has returned successfully. How are you building this demo?

There are things you can improve however. For example:

  • Don't use DMA manually... Try to favor a std::vector
  • Always initialize your variables. Uninitialized non-static local (scoped) variables such as your n,d and count will receive garbage values.
  • x is not used anywhere. Remove it.
  • int *p = NULL has a couple of flaws, one of them being NULL. NULL is a macro to 0. If you do want to create a pointer that points to nothing, use nullptr. Secondly, int *p has nothing to do with the pointer in the function, so it is useless. Remove it.

Here's a demo of the updated example: https://rextester.com/BUTV13117

0

I have tested your code and for me it runs with no problem.

If you allocate space in main(), then you should also typically deallocate it in main() as well.

0

The pointer p you create in input is independent of the pointer you make in the main. So you can not access the array you make in input when you are back in main.

If that is what you want to do, you "can" not delete p in the input, return it to main, use it in main, and then delete it in main. However splitting new and delete like that is not best coding practices.

If you don't want to use the array in main, you should remove any reference to p in the main function, no need to set it to null, and certainly not delete it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.