forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

dynamic memory allocation problem, please help

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
johnbo



Joined: 11 Jan 2006
Posts: 8

PostPosted: Wed Jan 11, 2006 12:36 pm    Post subject: dynamic memory allocation problem, please help Reply with quote

Hi can anyone help me please, im trying to make a linked list to store info for a pathfinding algorithm, i have some structs to hold info:

// node info structure
typedef struct
{
int f, g, h; // pathfinding evaluation parameters
MapPoint parent; // parent square
int state; // indicates whether square is on open list or not
}AInode;

// data structure to create linked list
typedef struct
{
AInode node;
struct ListNode* nxt;
}ListNode;

and im trying to create a new instance of the ListNode to be the root of the list:

// Create open list
ListNode* openRoot; // create pointer to root node
ListNode* openPtr; // create pointer to move aronud list

openRoot = new AInode; // make pointer point to reserved memery
openRoot->nxt = 0; // end of chain is NULL

but when i compile I get the following error:

error: 'new' undeclared

does anyone know why this does not work? im pulling my hair out. thanks
Back to top
View user's profile Send private message
danzel



Joined: 04 Nov 2005
Posts: 182

PostPosted: Wed Jan 11, 2006 1:38 pm    Post subject: Reply with quote

Although I haven't coded any c++ for the psp yet, I suspect you are trying to compile with psp-gcc instead of psp-g++.

If you are using the default makefile just rename your source file from .c to .cc and it will automatically use psp-g++
Back to top
View user's profile Send private message
johnbo



Joined: 11 Jan 2006
Posts: 8

PostPosted: Wed Jan 11, 2006 9:37 pm    Post subject: Reply with quote

i think your onto something here, im no expert with gcc+g++ more of a vsnet man. However still having trouble fixing. Changing the extension causes the compiler to not recognise the file and report that there is nothnig to be made.
Also there are several C files to be compiled in the project, i suspect I must change settings of my makefile but I dont know what to change can anyone help my current makefile looks like this:

TARGET = hello
OBJS = main.o graphics.o framebuffer.o

CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)

LIBDIR =
LIBS = -lpspgu -lpng -lz -lm
LDFLAGS =

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Packo Jacko 2005

PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak
Back to top
View user's profile Send private message
johnbo



Joined: 11 Jan 2006
Posts: 8

PostPosted: Wed Jan 11, 2006 9:52 pm    Post subject: Reply with quote

actually changing main.c to main.cpp made it act differently, it didnt complain about new but it couldnt find loads of other files, specifically from graphics.h and graphics.c. do these also have to be cpp to be compiled?
Back to top
View user's profile Send private message
johnbo



Joined: 11 Jan 2006
Posts: 8

PostPosted: Wed Jan 11, 2006 10:25 pm    Post subject: Reply with quote

Ok still having trouble, I have changed all files to cpp insead of c (although i havnt touched .h files) and now i ill compile mostly except for a single error:

main.cpp:(.text+0x6d0): undeclared reference to 'operator new(unsigned int)

my structs now look like this:

typedef struct MapPoint
{
int x,y;
};

typedef struct AInode
{
int f, g, h; // pathfinding evaluation parameters
MapPoint parent; // parent square
int state; // indicates whether square is on open list or not
};


// data structure to create linked list
typedef struct ListNode
{
AInode node;
ListNode* next;
};


and my calls look like this:

// Create open list
ListNode* openRoot; // create pointer to root node
ListNode* openPtr; // create pointer to move aronud list

openRoot = new ListNode; // make pinter point to reserved memery
openRoot->next = 0; // end of chain is NULL


its definately 'new' thats causing trouble, please help me im going mad over this!
Back to top
View user's profile Send private message
urchin



Joined: 02 Jun 2005
Posts: 121

PostPosted: Wed Jan 11, 2006 10:44 pm    Post subject: Reply with quote

If you're including your own C headers from C++, you'll need to wrap the includes with extern "C", e.g.

extern "C" {
#include "[c header file].h"
#include "[other c header file].h"
}

(unless that is already being done inside your header files - see the pspsdk header files for examples of headers that can be used from both C and C++)
Back to top
View user's profile Send private message
johnbo



Joined: 11 Jan 2006
Posts: 8

PostPosted: Wed Jan 11, 2006 11:05 pm    Post subject: Reply with quote

unfortunately that didnt work, ive tried putting everything in a single file and compling and i still get the same error. it doesnt undersand 'new'. whats going on, C++ does use this keyword doesnt it, has it been left out of the pSPSDK or something i really dont get it. Anyone please help, his is really killing me. cheers people
Back to top
View user's profile Send private message
LuMo



Joined: 21 Aug 2005
Posts: 410
Location: Austria

PostPosted: Wed Jan 11, 2006 11:58 pm    Post subject: Reply with quote

do you need a special package for psp and c++?
i do not have this problem, cause i did not write my objects yet
but eg:

Code:

std::stringstream ss;
std::string s;

s.assign("directory/filename");
ss << iNumber;
ss >> s;
ss.clear();
s.append(".extension");

the error msg is something like no string in std namespace

greets
lumo
_________________
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
Back to top
View user's profile Send private message Visit poster's website
johnbo



Joined: 11 Jan 2006
Posts: 8

PostPosted: Thu Jan 12, 2006 12:00 am    Post subject: Reply with quote

ok, so im trying now to use malloc to allocate the memory or the list but its still giving me trouble, this is what im doing:

struct ListNode* lroot; // create pointer to node structure
lroot = malloc(sizeof( struct Listnode)); // reserve memory the size of size ListNode

but it complains saying:

invalid conversion from void* to ListNode*

any ideas
Back to top
View user's profile Send private message
urchin



Joined: 02 Jun 2005
Posts: 121

PostPosted: Thu Jan 12, 2006 12:36 am    Post subject: Reply with quote

just an idea - are you linking against libc++?

and to answer your last question, in C++ you need to cast void pointers back to the required type, e.g.

lroot = (struct Listnode *)malloc(sizeof(struct Listnode));

I would try to get the "new" issue sorted before continuing though.
Back to top
View user's profile Send private message
LuMo



Joined: 21 Aug 2005
Posts: 410
Location: Austria

PostPosted: Thu Jan 12, 2006 12:43 am    Post subject: Reply with quote

urchin wrote:
just an idea - are you linking against libc++?

1) where can i find libc++?
2) if its inluded in pspsdk how do i add it? in makefile -lc++ ?

greets
lumo
_________________
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
Back to top
View user's profile Send private message Visit poster's website
urchin



Joined: 02 Jun 2005
Posts: 121

PostPosted: Thu Jan 12, 2006 1:18 am    Post subject: Reply with quote

Yeah, it should be included. My libs var looks something like this:

LIBRARIES = <insert others here> -lpspdebug -lpspaudio -lpspctrl -lpsphprm -lpspdisplay -lpspgu -lpspge -lpspsdk -lm -lstdc++ -lc -lpsputility -lpspuser -lpspkernel

The ordering is important!
Back to top
View user's profile Send private message
johnbo



Joined: 11 Jan 2006
Posts: 8

PostPosted: Thu Jan 12, 2006 1:19 am    Post subject: Reply with quote

thanks urchin, that got me going, the malloc thing works now, but the libc++thing, does it come with the SDK etc, as LuMo says
Back to top
View user's profile Send private message
PeterM



Joined: 31 Dec 2005
Posts: 125
Location: Edinburgh, UK

PostPosted: Thu Jan 12, 2006 1:20 am    Post subject: Reply with quote

You should just be able to add -lstdc++ to your LIBS variable and have it work magically.

By the way, why not use one of the STL containers instead of mallocing your own?
Back to top
View user's profile Send private message Visit poster's website
urchin



Joined: 02 Jun 2005
Posts: 121

PostPosted: Thu Jan 12, 2006 1:28 am    Post subject: Reply with quote

There should be a "libstdc++.a" file in your $PSPDEV/PSP/LIB directory. If not, then your SDK install sounds a bit flakey.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group