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 

dynamicly sized arrays

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



Joined: 12 Jan 2006
Posts: 6

PostPosted: Wed Feb 01, 2006 7:54 am    Post subject: dynamicly sized arrays Reply with quote

I recently started trying to program some psp stuff and I'm having trouble with a few things. Most importantly is dynamicly sized arrays. What I normaly do in C++ is something like this:
Code:
int size = 10;
char test[];
test = new char[size];

I know you can't do that in C code which is what the samples seem to be writen in. If I rename the file with .cpp it looks like it uses psp-g++ but I learned C++ using Visual Studio 6.0 so I'm not used to make files and such. It gives me an error saying that there is no size defined for test. I actually haven't programmed in C++ for a while, so maybe I'm just doing it wrong all together. Can I just not do somthing like that or is there a C equivilant?
Back to top
View user's profile Send private message
jsgf



Joined: 12 Jul 2005
Posts: 254

PostPosted: Wed Feb 01, 2006 8:07 am    Post subject: Reply with quote

Code:
char test[size];
should work. "size" needs to be pretty small, because it gets allocated on the stack, and implicitly freed when the function returns (ie, its like any other local). If you want to allocate it on the heap, use
Code:
char *test = malloc(size); /* C */
char *test = new char[size]; // C++
Back to top
View user's profile Send private message Visit poster's website
Masamune



Joined: 12 Jan 2006
Posts: 6

PostPosted: Wed Feb 01, 2006 8:31 am    Post subject: Reply with quote

Code:
psp-g++ -I. -I/usr/local/pspdev/psp/sdk/include -O2 -G0 -Wall -I. -I/usr/local/p
spdev/psp/sdk/include -O2 -G0 -Wall -fno-exceptions -fno-rtti   -c -o main.o mai
n.cpp
main.cpp: In function 'int main()':
main.cpp:66: warning: unused variable 'test1'
psp-gcc -I. -I/usr/local/pspdev/psp/sdk/include -O2 -G0 -Wall  -L. -L/usr/local/
pspdev/psp/sdk/lib   main.o  -lpspdebug -lpspdisplay -lpspge -lpspctrl -lpspsdk
-lc -lpspnet -lpspnet_inet -lpspnet_apctl -lpspnet_resolver -lpsputility -lpspus
er -lpspkernel -o kptest.elf
main.o: In function `main':
main.cpp:(.text+0xd8): undefined reference to `operator new[](unsigned int)'
collect2: ld returned 1 exit status
make: *** [kptest.elf] Error 1

That's the error I get when I use this in code:
Code:
int size = 10;
char *test1 = new char[size];

I'm using the kprintf example from the samples, I just rename it to main.cpp and add the above lines to the main function. Do I need to change the make file at all for C++?

Edit: It compiles fine as C if I use malloc(). I'd much rather get the C++ way working, but this works for now. Thanks for the help :)
Back to top
View user's profile Send private message
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Wed Feb 01, 2006 8:42 am    Post subject: Reply with quote

The reason for this is you are using gcc to link, you need to add -lstdc++ to your libraries in order to use C++. As you are using the pspsdk makefiles then in your apps makefile add the line LIBS=-lstdc++ and it _should_ work ;)
Back to top
View user's profile Send private message
Masamune



Joined: 12 Jan 2006
Posts: 6

PostPosted: Wed Feb 01, 2006 9:28 am    Post subject: Reply with quote

Sweet! that worked, 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