Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 5th, 2005, 2:15 PM   #1
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
[solved]Simple problem with C++ dynamic memory allocation: please help.

OK, after hours of debugging my program, I have finally found out where the problem lies. So you don't have to read through my whole program I will offer a simplified case that produces the same error. Please tell me why this error occurs and what I can do to stop it.

//main.cpp
#include "FooClass.h"
main()
{
	int i=30;
	FooClass* pFoo;

	pFoo=(FooClass*)malloc(sizeof(FooClass)*i);
	pFoo[0].fooString="Foobar Quux";
}

//FooClass.h
#pragma once
#include <string>
using namespace std;
class FooClass
{
public:
	string fooString;
	

};

This compiles fine, however it spits out errors when I try to run it. What is the problem?

Last edited by uman; Feb 5th, 2005 at 4:10 PM. Reason: Added [solved] to title
uman is offline   Reply With Quote
Old Feb 5th, 2005, 3:34 PM   #2
Monster
Newbie
 
Join Date: Jan 2005
Posts: 20
Rep Power: 0 Monster is on a distinguished road
main()
{
   int i=30;
   FooClass* pFoo;

   pFoo = new FooClass [i];
   pFoo[0].fooString="Foobar Quux";
   delete[] pFoo;
   return 0;
}

Last edited by Monster; Feb 5th, 2005 at 4:05 PM.
Monster is offline   Reply With Quote
Old Feb 5th, 2005, 4:01 PM   #3
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
Yes, I know about vectors and use them whenever I can, however for what I am doing in my larger program I need arrays.

Thanks anyway for the help though.
uman is offline   Reply With Quote
Old Feb 5th, 2005, 4:08 PM   #4
Monster
Newbie
 
Join Date: Jan 2005
Posts: 20
Rep Power: 0 Monster is on a distinguished road
You beat me... just changed it to an array.
Monster is offline   Reply With Quote
Old Feb 5th, 2005, 4:09 PM   #5
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
Ah, ok I understand now. Thanks a million for the help.
uman is offline   Reply With Quote
Old Feb 5th, 2005, 4:16 PM   #6
Monster
Newbie
 
Join Date: Jan 2005
Posts: 20
Rep Power: 0 Monster is on a distinguished road
Another example. In the first example you create an array with 30 FooClass objects. In this example you create an array with 30 pointers to FooClass objects. To add a FooClass, you need to allocate space for the class first.

main()
{
   int i=30;
   FooClass** pFoo;

   pFoo = new FooClass *[i]; // create array with 30 pointers
   pFoo[0] = new FooClass(); // create a new FooClass
   pFoo[0]->fooString="Foobar Quux";
   delete pFoo[0]; // delete the FooClass
   delete[] pFoo; // delete the array
   return 0;
}
Monster is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:34 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC