| View previous topic :: View next topic |
| Author |
Message |
ddgFFco
Joined: 20 Sep 2005 Posts: 11
|
Posted: Thu Sep 29, 2005 3:07 pm Post subject: C++ dynamic array, new[]/delete[] not working properly. |
|
|
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 |
|
 |
CyberBill
Joined: 26 Jul 2005 Posts: 86 Location: Redmond, WA
|
Posted: Thu Sep 29, 2005 4:57 pm Post subject: |
|
|
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 |
|
 |
ddgFFco
Joined: 20 Sep 2005 Posts: 11
|
Posted: Thu Sep 29, 2005 5:29 pm Post subject: |
|
|
| CyberBill wrote: |
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 |
|
 |
chp
Joined: 23 Jun 2004 Posts: 313
|
Posted: Thu Sep 29, 2005 5:54 pm Post subject: |
|
|
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 |
|
 |
ddgFFco
Joined: 20 Sep 2005 Posts: 11
|
Posted: Thu Sep 29, 2005 6:00 pm Post subject: |
|
|
| Done and working, thanks. |
|
| Back to top |
|
 |
|