dynamic memory allocation problem, please help

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
johnbo
Posts: 8
Joined: Wed Jan 11, 2006 12:26 pm

dynamic memory allocation problem, please help

Post by johnbo »

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
danzel
Posts: 182
Joined: Fri Nov 04, 2005 11:03 pm

Post by danzel »

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++
johnbo
Posts: 8
Joined: Wed Jan 11, 2006 12:26 pm

Post by johnbo »

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
johnbo
Posts: 8
Joined: Wed Jan 11, 2006 12:26 pm

Post by johnbo »

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?
johnbo
Posts: 8
Joined: Wed Jan 11, 2006 12:26 pm

Post by johnbo »

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!
urchin
Posts: 121
Joined: Thu Jun 02, 2005 5:41 pm

Post by urchin »

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++)
johnbo
Posts: 8
Joined: Wed Jan 11, 2006 12:26 pm

Post by johnbo »

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
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

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: Select all

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

s.assign("directory/filename");
ss << iNumber;
ss >> s;
ss.clear&#40;&#41;;
s.append&#40;".extension"&#41;;
the error msg is something like no string in std namespace

greets
lumo
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
johnbo
Posts: 8
Joined: Wed Jan 11, 2006 12:26 pm

Post by johnbo »

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
urchin
Posts: 121
Joined: Thu Jun 02, 2005 5:41 pm

Post by urchin »

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.
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

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
urchin
Posts: 121
Joined: Thu Jun 02, 2005 5:41 pm

Post by urchin »

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!
johnbo
Posts: 8
Joined: Wed Jan 11, 2006 12:26 pm

Post by johnbo »

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
PeterM
Posts: 125
Joined: Sat Dec 31, 2005 7:25 pm
Location: Edinburgh, UK
Contact:

Post by PeterM »

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?
urchin
Posts: 121
Joined: Thu Jun 02, 2005 5:41 pm

Post by urchin »

There should be a "libstdc++.a" file in your $PSPDEV/PSP/LIB directory. If not, then your SDK install sounds a bit flakey.
Post Reply