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 

C++ dynamic array, new[]/delete[] not working properly.

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



Joined: 20 Sep 2005
Posts: 11

PostPosted: Thu Sep 29, 2005 3:07 pm    Post subject: C++ dynamic array, new[]/delete[] not working properly. Reply with quote

Hey, I can’t seam to get this to work properly. Consider the following code (striped down, but complete):

main.cpp
Code:

#include <pspkernel.h>

extern "C" {

PSP_MODULE_INFO("Test", 0, 1, 1);

class TestClass {
    public:
        TestClass(int h, int w);
        ~TestClass();
    private:
        int** testArray;
        int   height;
        int   width;
};

TestClass::TestClass(int h, int w):height(h), width(w) {
    testArray = new int*[h];
    for (int x = 0; x<h ;x++) {
        testArray[x] = new int[w];
    }
}
TestClass::~TestClass() {
    for (int x = 0; x<height ;x++) {
        delete [] testArray[x];
    }
    delete [] testArray;
}

int main(void) {
   
    TestClass tC(10,10);
   
    sceKernelExitGame();
    return 0;
   
}


}


Makefile:
Code:

TARGET = main
OBJS = main.o

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

LIBDIR =
LDFLAGS =

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Test

PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak


When I try to compile I get “undefined reference to ‘operator new[](unsigned int)’” and “undefined reference to ‘operator delete[](void*)’” errors. I’ve updated PSPSDK and still get the errors, is there something I’m doing wrong or a way around this?

Thanks.
Back to top
View user's profile Send private message
CyberBill



Joined: 26 Jul 2005
Posts: 86
Location: Redmond, WA

PostPosted: Thu Sep 29, 2005 4:57 pm    Post subject: Reply with quote

Code:

extern "C" {



new and delete are not valid C operators. They only work in C++. Be sure your file is named .cpp, and not .c.
Back to top
View user's profile Send private message AIM Address MSN Messenger
ddgFFco



Joined: 20 Sep 2005
Posts: 11

PostPosted: Thu Sep 29, 2005 5:29 pm    Post subject: Reply with quote

CyberBill wrote:
Code:

extern "C" {



new and delete are not valid C operators. They only work in C++. Be sure your file is named .cpp, and not .c.


It is named .cpp. PSPSDK seams to require “extern "C" { … }“ for C++. Tried removing it with no luck either.
Back to top
View user's profile Send private message
chp



Joined: 23 Jun 2004
Posts: 313

PostPosted: Thu Sep 29, 2005 5:54 pm    Post subject: Reply with quote

PSPSDK uses psp-gcc to link with, which does not include certain C++ libraries that contains the new/delete operators. Add -lstdc++ to your libraries that you link with, or roll your own, it's not that hard:
Code:

void* operator new(size_t s)
{
 return malloc(s);
}

void operator delete(void* p)
{
 free(s);
}

void* operator new[](size_t s)
{
 return malloc(s);
}

void operator delete[](void* p)
{
 free(s);
}


This thread can be a good reference:
http://forums.ps2dev.org/viewtopic.php?t=561
_________________
GE Dominator
Back to top
View user's profile Send private message
ddgFFco



Joined: 20 Sep 2005
Posts: 11

PostPosted: Thu Sep 29, 2005 6:00 pm    Post subject: Reply with quote

Done and working, thanks.
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