#167510 - intel3 - Mon Mar 16, 2009 11:15 am
I am new to c++ programming, here is a basic question I have
my struct:
typedef struct {
s32 radius;
Mysprite sprites[30];
}ImageInfo;
Can I declare the size of the Mysprite array dynamically whenever I create a new ImageInfo stuct pointer?
If so how...
Thanks
#167511 - sgeos - Mon Mar 16, 2009 11:24 am
Use a constructor that takes the size of the array you need. Allocate that much memory dynamically and clean up in the destructor.
#167512 - intel3 - Mon Mar 16, 2009 11:46 am
Thanks for the quick replay
sgeos wrote: |
Use a constructor that takes the size of the array you need. Allocate that much memory dynamically and clean up in the destructor. |
I am not sure what to do here is what I think.
Create a Image.cpp like below
#include <nds.h>
#include "Mysprite.h"
ImageInfo * info;
Image::Image(u8 size) {
ImageInfo * info = new ImageInfo();
info -> sprites[size];
}
Image::~Image() {
//destory
}
Image::getInfo(){
return info;
}
So when i want to get the pointer reference in the main function it would be
Image test = new Image(5);
ImageInfo * info = test.getInfo();
Previous I was thinking to use it like
ImageInfo * image = new ImageInfo();
image->radius = 3;
So the different is instead of getting a pointer to the typedef struct, now i wrap the struct inside a class constructor and then use function call to get that struct.
Is this correct?
#167514 - Miked0801 - Mon Mar 16, 2009 4:23 pm
No. More like this:
Code: |
struct // or public class, which I'd use as this is no longer a POD
{
s32 radius;
Mysprite *sprites;
}
Image::Image(int size)
{
radius = kDefaultValueForRadius;
sprites = new Mysprite[size];
}
Image::~Image()
{
delete[] sprites;
}
|
Then the next step is:
Code: |
class Image
{
public:
Image(int size);
~Image(){delete[] sprites;}
Mysprite *getSpritePtr(int index);
int radius(){return mRadius;}
private:
Mysprite *sprites;
int mSpriteArraySize;
int mRadius;
}
Image::Image(int size)
{
mSpriteArraySize = size;
radius = kDefaultValueForRadius;
sprites = new Mysprite[size];
}
Mysprite *Image::getSpritePtr(int index)
{
assert(sprites != null);
assert(index < mMaxSpriteIndex);
return sprites[index];
}
|
Which then begs the question of why we aren't using a Vector<Mysprite> to handle dynamic allocation/deallocation for us in the first place as it has all the safety checks and more.
#167519 - albinofrenchy - Mon Mar 16, 2009 10:21 pm
Might be something you know already, but C++ treats structs different than C treats them. They become C++ classes.
I agree with Miked0801; It is worth while to learn the STL classes. They are quite handy and documentation on them is everywhere. I've never run into any performance issues, but I doubt they carry massive amounts of overhead. I will say though, that if the number of sprites in the struct will NEVER change after you create the class, go ahead and make it an array.
Also make sure you delete the thing instead of free it.
#167520 - Miked0801 - Mon Mar 16, 2009 11:58 pm
Yep. struct = public default class. I had that in my original post, but it got lost somewhere in the editing :)
The reason I was suggesting the class was because he moved away from a POD as soon as he needed a constructor.
And there is a valid argument for using Vector<> even with a fixed size given the extra bounds/error checking it gives.
#167522 - albinofrenchy - Tue Mar 17, 2009 6:39 am
Does it really give extra bounds checking? Is that only with the at() function and not the [] operator?
#167523 - elyk1212 - Tue Mar 17, 2009 7:55 am
As far as I know, the #at function provides the checks (throws exception), while [] opp assumes you know what you're doing.
http://www.cplusplus.com/reference/stl/vector/operator%5B%5D.html
You'd run into the same issue with iterators I'm fairly certain.
BTW, as far as I can tell (at least within DevkitArm??) I could not link in STL containers etc, for GBA. So... I guess in that case maybe you could consider rolling your own or something.... ?
#167534 - Miked0801 - Tue Mar 17, 2009 5:26 pm
Or use vector for the added future flexibility :)
Or just use arrays and assert the bounds.
#167585 - albinofrenchy - Thu Mar 19, 2009 6:10 am
Code: |
BTW, as far as I can tell (at least within DevkitArm??) I could not link in STL containers etc, for GBA. So... I guess in that case maybe you could consider rolling your own or something.... ? |
Really? I'm using STL stuff with no problem in devkitarm for DS stuff, I don't know why it would work on ds and not the gba.
#167586 - elyk1212 - Thu Mar 19, 2009 7:09 am
Yeah, I don't really know either. I just assumed it was a resource limitation, and maybe someone forcefully undefined symbols that would cause an issue (such as parts of STL?).
Here's a small example of what I get:
Code: |
//*****************************************************************
int main()
{
vector<int> test;
|
Notice...
-I/home/kyle2/Development/Games/Gameboy/devkitARM/devkitARM/include/c++/4.1.1
Code: |
arm-eabi-g++ -MMD -MP -MF /home/kyle2/Desktop/GBASvn/build/main.d -g -Wall -O3 -mcpu=arm7tdmi -mtune=arm7tdmi -fomit-frame-pointer -ffast-math -mthumb -mthumb-interwork -I/home/kyle2/Desktop/GBASvn/"include" -I/home/kyle2/Desktop/GBASvn/build -I/home/kyle2/Desktop/GBASvn/sound -I/home/kyle2/Desktop/GBASvn/Maps/test/ -I/home/kyle2/Development/Games/Multi_System_Compilers/DevikitPro/libgba/include -I/home/kyle2/Development/Games/Multi_System_Compilers/DevikitPro/Krawall//include -I/home/kyle2/Desktop/GBASvn/build -I/home/kyle2/Development/Games/Gameboy/devkitARM/devkitARM/include/c++/4.1.1 -fno-rtti -fno-exceptions -fno-rtti -c /home/kyle2/Desktop/GBASvn/source/main.cpp -o main.o
/home/kyle2/Desktop/GBASvn/source/main.cpp: In function 'void handleBallCollision(Ball*, Vector2D)':
/home/kyle2/Desktop/GBASvn/source/main.cpp:149: warning: passing 'double' for argument 1 to 'Vector2D::Vector2D(int, int)'
/home/kyle2/Desktop/GBASvn/source/main.cpp: In function 'void init_map_test()':
/home/kyle2/Desktop/GBASvn/source/main.cpp:760: warning: comparison between signed and unsigned integer expressions
/home/kyle2/Desktop/GBASvn/source/main.cpp:852: warning: unused variable 'pse'
/home/kyle2/Desktop/GBASvn/source/main.cpp: In function 'int main()':
/home/kyle2/Desktop/GBASvn/source/main.cpp:930: error: 'vector' was not declared in this scope
/home/kyle2/Desktop/GBASvn/source/main.cpp:930: error: expected primary-expression before 'int'
/home/kyle2/Desktop/GBASvn/source/main.cpp:930: error: expected `;' before 'int'
make[1]: *** [main.o] Error 1
make: *** [build] Error 2
|
And a ls of the following shows vector... (which is a non empty file that looks correct).
Code: |
[kyle2@localhost GBASvn]$ ls /home/kyle2/Development/Games/Gameboy/devkitARM/devkitARM/include/c++/4.1.1
algorithm bitset cfloat cmath cstdarg cstring cxxabi.h exception_defines.h iomanip istream locale numeric sstream string valarray
arm-eabi/ cassert ciso646 complex cstddef ctime debug/ ext/ ios iterator map ostream stack tr1/ vector
backward/ cctype climits csetjmp cstdio cwchar deque fstream iosfwd limits memory queue stdexcept typeinfo
bits/ cerrno clocale csignal cstdlib cwctype exception functional iostream list new set streambuf utility
|
A cat of the vector header, gives what I would expect:
Code: |
// <vector> -*- C++ -*-
// Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// As a special exception, you may use this file as part of a free software
// library without restriction. Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License. This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.
/*
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Hewlett-Packard Company makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
*
* Copyright (c) 1996
* Silicon Graphics Computer Systems, Inc.
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Silicon Graphics makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/
/** @file vector
* This is a Standard C++ Library header.
*/
#ifndef _GLIBCXX_VECTOR
#define _GLIBCXX_VECTOR 1
#pragma GCC system_header
#include <bits/functexcept.h>
#include <bits/stl_algobase.h>
#include <bits/allocator.h>
#include <bits/stl_construct.h>
#include <bits/stl_uninitialized.h>
#include <bits/stl_vector.h>
#include <bits/stl_bvector.h>
#ifndef _GLIBCXX_EXPORT_TEMPLATE
# include <bits/vector.tcc>
#endif
#ifdef _GLIBCXX_DEBUG
# include <debug/vector>
#endif
#endif /* _GLIBCXX_VECTOR */
|
Any ideas? Anyone used STL on GBA?
#167589 - albinofrenchy - Thu Mar 19, 2009 8:39 am
I almost hate to ask this; but I assume you have the ole' "#include <vector>" with the ole "using namespace std" in there at the top?
#167595 - elyk1212 - Thu Mar 19, 2009 3:44 pm
Oh... you know what, Doh! You're right. I just pushed that example out quick to show you, and forgot the resolution operator std::vector...
Compiles fine :P Just goes to show when I get something in my head, I make assumptions.
I swore I got a linker error in the past though, using slightly older DevkitArm release. Maybe I am crazy :O.
I hadn't tried since, and since I asked the GBA_with_STL question so many times, with no clear response, I thought for sure it wouldn't work.
Well, that's something to try out now :)
Ohhhh.... priority queues and algorithms and such with no need to recreate the wheel. Yay STL C++.