0

Coming from a Java, PHP background, I am trying to get into C++. I would like to store an array in a struct. My problem is specifying the size of the array after initialising the struct.

Here's my code for the struct:

struct SpriteAnimation {
    // ...
    int parts;                  // total number of animation-parts
    unsigned int textures[];    // array to store all animation-parts
    // ...
};

And here for the main function:

SpriteAnimation bg_anim;
bg_anim.parts = 3; 
unsigned int *myarray = new unsigned int[bg_anim.parts];
bg_anim.textures = myarray;

What do I need to change to fix this?

4 Answers 4

9

In modern C++, you would use a dynamic container for the inner "array":

struct SpriteAnimation {
  std::vector<unsigned int> textures;    // array to store all animation-parts
  size_t num_parts() const { return textures.size(); }
};

This is by far safer and more modular than anything you could try with manually allocated storage. Usage:

SpriteAnimation x;
x.textures.push_back(12);  // add an element
x.textures.push_back(18);  // add another element

SpriteAnimation y = x;     // make a copy

std::cout << "We have " << x.num_textures() << " textures." std::endl; // report
Sign up to request clarification or add additional context in comments.

3 Comments

Downvoter, care to explain your objection? I understand that this isn't the "C array way" of doing things, but the question asks for a C++ solution, and the idiomatic and moral way to do C++ is to use (exception-safe and resource-managing and single-responsibility) containers as building blocks.
How does this know the size of the array (or vector - could you explain the difference, please)? Does push_back() resize it by creating a new one each time it is called? That would be horribly inefficient compared to John's solution, or am I misunderstanding something?
@Ben: Explaining the full workings of vector is beyond this comment, but do read it up -- yes, push_back does append a new element, but this is amortized O(1) thanks to magic. In fact, most of the C++ standard library can only be described as such.
0
struct SpriteAnimation {
    // ...
    int parts;                  // total number of animation-parts
    unsigned int * textures;    // array to store all animation-parts
    // ...
};

You can use type name[] syntax only if you declare members inline.

3 Comments

Is it correct to then use it with bg_anim.textures = new unsigned int[bg_anim.parts]; ?
What happens when you copy your struct? What happens when there's an exception?
@Ben yes that works correctly, in fact it wasn't correct before, as texture is a dynamically allocated array and should be shown as a pointer. You should lookup classes if you wish to use C++ as you are using c style syntax. Also remember to delete[] the array at the end
0

Size of a struct must be known at a compilation time.

1 Comment

He was meaning to use a pointer and the type unsigned int * is a static size
0

I worked around the issue through following code.It might be having design issues so please look it up the following code works for me.

#include <iostream>
using namespace std;
struct lol {
  // ...
  int parts;                  // total number of animation-parts
  unsigned int *texture;   // array to store all animation-parts
  // ...
};

int main() {
  // your code goes here
  lol bg_anim;
  bg_anim.parts = 3; 
  unsigned int *myarray = new unsigned int[bg_anim.parts];
  bg_anim.texture = myarray;
  return 0;
 }

Forgive me for using lol instead of your specified name.Do tell me any issues.And help me if there are other issues in my code. Thank you !! :)

1 Comment

by the way a struct is nothing but a class with everything public.A better solution would have been to use classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.