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

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

Moderators: cheriff, TyRaNiD

Post Reply
ddgFFco
Posts: 11
Joined: Tue Sep 20, 2005 5:35 pm

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

Post by ddgFFco »

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

main.cpp

Code: Select all

#include <pspkernel.h>

extern "C" &#123;

PSP_MODULE_INFO&#40;"Test", 0, 1, 1&#41;;

class TestClass &#123;
    public&#58;
        TestClass&#40;int h, int w&#41;;
        ~TestClass&#40;&#41;;
    private&#58;
        int** testArray;
        int   height;
        int   width;
&#125;;

TestClass&#58;&#58;TestClass&#40;int h, int w&#41;&#58;height&#40;h&#41;, width&#40;w&#41; &#123;
    testArray = new int*&#91;h&#93;;
    for &#40;int x = 0; x<h ;x++&#41; &#123;
        testArray&#91;x&#93; = new int&#91;w&#93;;
    &#125;
&#125;
TestClass&#58;&#58;~TestClass&#40;&#41; &#123;
    for &#40;int x = 0; x<height ;x++&#41; &#123;
        delete &#91;&#93; testArray&#91;x&#93;;
    &#125;
    delete &#91;&#93; testArray;
&#125;

int main&#40;void&#41; &#123;
    
    TestClass tC&#40;10,10&#41;;
    
    sceKernelExitGame&#40;&#41;;
    return 0;
    
&#125;


&#125;
Makefile:

Code: Select all

TARGET = main
OBJS = main.o

INCDIR = 
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;

LIBDIR =
LDFLAGS =

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Test

PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
include $&#40;PSPSDK&#41;/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.
CyberBill
Posts: 86
Joined: Tue Jul 26, 2005 3:53 pm
Location: Redmond, WA

Post by CyberBill »

Code: Select all

extern "C" &#123;

new and delete are not valid C operators. They only work in C++. Be sure your file is named .cpp, and not .c.
ddgFFco
Posts: 11
Joined: Tue Sep 20, 2005 5:35 pm

Post by ddgFFco »

CyberBill wrote:

Code: Select all

extern "C" &#123;

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.
chp
Posts: 313
Joined: Wed Jun 23, 2004 7:16 am

Post by chp »

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

void* operator new&#40;size_t s&#41;
&#123;
 return malloc&#40;s&#41;;
&#125;

void operator delete&#40;void* p&#41;
&#123;
 free&#40;s&#41;;
&#125;

void* operator new&#91;&#93;&#40;size_t s&#41;
&#123;
 return malloc&#40;s&#41;;
&#125;

void operator delete&#91;&#93;&#40;void* p&#41;
&#123;
 free&#40;s&#41;;
&#125;
This thread can be a good reference:
http://forums.ps2dev.org/viewtopic.php?t=561
GE Dominator
ddgFFco
Posts: 11
Joined: Tue Sep 20, 2005 5:35 pm

Post by ddgFFco »

Done and working, thanks.
Post Reply