A BIg Problem with dynamic memory allocation

yamrajbhalla

yamrajbhalla

@yamrajbhalla-ZPvBoL Oct 21, 2024
i declared a array of pointer *nm[10];
in class

and i need to input data in it and dynamically allocate memory for this and also i have to show the output


thanks

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Raviteja.g

    Raviteja.g

    @ravitejag-02nJVr Mar 25, 2009

    For dynamic allocation you can use malloc( ) or calloc( ) functions .
    can't you try it.
    and it is one and only way to allocate memory dynamically in C
  • pradeep_agrawal

    pradeep_agrawal

    @pradeep-agrawal-rhdX5z Mar 25, 2009

    yamrajbhalla
    i declared a array of pointer *nm[10];
    in class

    and i need to input data in it and dynamically allocate memory for this and also i have to show the output
    As you have declared the array of pointers in a class (i.e., using C++), so you can use new/delete operators for this. Consider that the array is array of integer pointers, so code will look like:

    int *nm[10] = { 0 };              //Initializing array with all poiters set to NULL
    int i = 0;
    
    for(i = 0; i < 10; i++) {
        nm[i] = new int;              //Dynamic memory allocation
        *(nm[i]) = <some value>       //Assigning some data
    }
    
    for(i = 0; i < 10; i++) {
        cout<<*(nm[i])<<endl;         //Writing data to output stream
        delete nm[i];                 //Deleting allocated memory
    }
     
    -Pradeep
  • ms_cs

    ms_cs

    @ms-cs-Ab8svl Mar 25, 2009

    What error you got?
    For me I have compiled this part.It does not show any error.
    for(i = 0; i < 10; i++) {
    nm = new int; //Dynamic memory allocation
    *(nm) = <some value> //Assigning some data
    }
  • yamrajbhalla

    yamrajbhalla

    @yamrajbhalla-ZPvBoL Mar 26, 2009

    thnks for the fast rpyly i did it bt a prob lm occurs always
    after running the programm
    it gives me right output bt after that c++ complier closes and a error occurs
    will u plz hlp me to sort out this problem
  • yamrajbhalla

    yamrajbhalla

    @yamrajbhalla-ZPvBoL Mar 26, 2009

    and by the way thanks for ur precious time to viewing myne problem
    thanks a lotttttt
    bro
  • pradeep_agrawal

    pradeep_agrawal

    @pradeep-agrawal-rhdX5z Mar 26, 2009

    yamrajbhalla
    it gives me right output bt after that c++ complier closes and a error occurs
    Could you please update which compiler you are using? Is there any error message displayed when the compiler is closed due to error? If yes, what the message?

    Also i had given a code snippet you must have written some code around it. It will be of help in analyzing the issue if you post your code here.

    -Pradeep
  • pradeep_agrawal

    pradeep_agrawal

    @pradeep-agrawal-rhdX5z Mar 27, 2009

    ~bill()
    {delete item;}
    };
    Above part of the code is creating problem.

    This is destructor of the class 'bill' and it is called when the object of this class is destroyed. In your case it will be called when the object 'obj' is destroyed. As the object 'obj' is statically declared in the main function of the class so it gets destroyed when the execution of the main function completes. That's why you are getting error at end of program execution. Now read below for the reason why this particular code segment is causing problem.

    When the "delete item;" statement is executed, it tries to delete the memory statically allocated for the array and hence an exception. "delete" should be done for dynamically allocated memory. In your case, "delete" should be called for elements of array. So the destructor should look like

    ~bill() {
      for(int i = 0; i <= m; i++) {
        delete item[j];
      }
    }
    Let us know if you face the issue after above changes.

    -Pradeep
  • Raviteja.g

    Raviteja.g

    @ravitejag-02nJVr Mar 27, 2009

    well pradeep.
    you did a good job.As i don't know much(almost negligible) about C++ i thought pointers are used only in C.
    so i gave my suggestion to use memory allocation functions which allocates memory dynamically
    but now i came to know that pointers are there in C++ too.
    Am i right?
  • just2rock

    just2rock

    @just2rock-DkmPtO Mar 27, 2009

    pradeep_agrawal
    ~bill()
    {delete item;}
    };
    Above part of the code is creating problem.

    This is destructor of the class 'bill' and it is called when the object of this class is destroyed. In your case it will be called when the object 'obj' is destroyed. As the object 'obj' is statically declared in the main function of the class so it gets destroyed when the execution of the main function completes. That's why you are getting error at end of program execution. Now read below for the reason why this particular code segment is causing problem.

    When the "delete item;" statement is executed, it tries to delete the memory statically allocated for the array and hence an exception. "delete" should be done for dynamically allocated memory. In your case, "delete" should be called for elements of array. So the destructor should look like

    ~bill() {
      for(int i = 0; i <= m; i++) {
        delete item[j];
      }
    }
    Let us know if you face the issue after above changes.

    -Pradeep
    can you pls be more specific about the actual code generating from this.
  • pradeep_agrawal

    pradeep_agrawal

    @pradeep-agrawal-rhdX5z Mar 29, 2009

    Raviteja.g
    but now i came to know that pointers are there in C++ too.
    Am i right?
    Yes, C++ have pointers too. And it provides better and more safe ways to handle memory allocations.

    just2rock
    can you pls be more specific about the actual code generating from this.
    Is there anything specific in the description for which you need more details? I have already mentioned the code snippet of actual code that is causing problem and provided a sample code to fix that.

    -Pradeep
  • yamrajbhalla

    yamrajbhalla

    @yamrajbhalla-ZPvBoL Feb 4, 2023

    #include<iostream.h>
    #include<conio.h>
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    #include<iomanip.h>


    class bill
    {
    char *item[5];
    int m;
    float itmamt[5];
    public:

    void additem(char name[],int i,float prc)
    {
    item=new char[strlen(name) +1];
    strcpy(item,name);
    m=i;
    itmamt=prc;
    }
    void display()
    {
    for(int j=0;j<=m;j++)
    {
    cout<<item[j]<<setw(9)<<itmamt[j];
    }
    }
    ~bill()
    {delete item;}
    };

    void main()
    {float prc;
    clrscr();
    char ch='Y';
    int i=0;
    char *name;
    bill obj;
    do
    {
    cout<<"enter the name of item\n";
    gets(name);
    cout<<"enter the price ";
    cin>>prc;

    obj.additem(name,i,prc);
    ++i;
    cout<<"Enter CH";