A BIg Problem with dynamic memory allocation
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
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
-
Raviteja.gFor 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
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:yamrajbhallai 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
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]) =
//Assigning some data } for(i = 0; i < 10; i++) { cout<<*(nm[i])< -Pradeep -
ms_csWhat 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) =//Assigning some data
} -
yamrajbhallathnks 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 -
yamrajbhallaand by the way thanks for ur precious time to viewing myne problem
thanks a lotttttt
bro -
pradeep_agrawal
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?yamrajbhallait gives me right output bt after that c++ complier closes and a error occurs
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 -
yamrajbhalla#include
#include
#include
#include
#include
#include
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<- }
}
~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"; -
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 -
Raviteja.gwell 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
can you pls be more specific about the actual code generating from this.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 -
pradeep_agrawal
Yes, C++ have pointers too. And it provides better and more safe ways to handle memory allocations.Raviteja.gbut now i came to know that pointers are there in C++ too.
Am i right?
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.just2rockcan you pls be more specific about the actual code generating from this.
-Pradeep
You are reading an archived discussion.
Related Posts
Want to know everything about how to become an Army officer , This comprehensive 'Insiders' guide will show every step of the way. Visit
Hi,
I can't access Chillax : Chit - Chat from my office because of the some company policies. I think it's because this forum heading contains the "Chat " word....
I have HP 1000 laserjet attached to win vista & i shared this printer from another win vista... but if i give any print from IE7 its shows error "pls...
Hello guys,
Well I am currently designing and building a small 2-axis filament winding machine.
To ensure that my machine is fail-safe, I need to know what the maximum tensile...
Hello Big K,
this thread is dedicated for the response time from you. I thought this is the only way to get attention😒
P.S I take no responsibility if you...