• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Psychopathetica

Empty Array In Structures

19 posts in this topic

Hey there. I wanna be able to have some way of being able to use an empty array inside a structure to be reinitialized later on. The reason why is cause it will have an unknown number of elements until a file is loaded, and then I can reinitialize the array to an appropriate size and start storing data. The only thing I can think of for a solution since in C++ you cant have empty arrays in structures is to make a pointer to a variable in the structure:

#include <iostream>
using namespace std;

struct Test_Structure
{
     unsigned char *myarray;
};

Test_Structure test;

int main()
{
	unsigned char some_other_array[10];
	test.myarray = new unsigned char[10];
	cout << "test.myarray: " << sizeof(test.myarray) << endl;
	cout << "some_other_array: " << sizeof(some_other_array) << endl;
	system("pause");
	return 0;
}

However if you were to run that, it only shows the size of the pointer rather than the whole array. Can I still the pointer as an array? Or is there another way around using empty arrays from structures?

1

Share this post


Link to post
Share on other sites

You can have an empty array at the end of a structure like:

struct Test_Structure {
   const int32_t some_data_0;
   const uint32_t array_of_some_sort[];
};
I find these can be useful when using memory mapped files.  In many file formats you'll have a header to a block of data, and then a variable number of some item following the header.  These sorts of trailing empty arrays can make traversing the files very easy I found.  These sorts of things are good when the memory is allocated through some other means.  As Hodgman pointed out, if you intend to allocate the memory your self, use a std::vector or similar container.

 

I would take note that const makes you unable to change the data of this structure, so it would need to be initialized in a constructor initializer list, and also that uint32_t array[] is equal to typing uint32_t* array, but the latter makes it a bit more clear as to what the variable is: a pointer, as there's no such thing as an empty array in C++. Using such a construct is fine in C, but it'd personally avoid it in C++, considering there's a safe way to do it which avoids accidental memory leaks.

I failed at reading the specific use case, as is explained in later posts, which now makes sense to me. Learn something new everyday!

Edited by Strewya
-1

Share this post


Link to post
Share on other sites
Ryan_001 already explicitly stated his use case for these structs (namely when working on data provided through memory mapped files). In this context, const makes a lot of sense.

I'd also like to point out that an open array and a pointer are not the same thing. The open array once again works perfectly for his use case, a pointer would not work at all.
2

Share this post


Link to post
Share on other sites

You can have an empty array at the end of a structure like:

struct Test_Structure {
   const int32_t some_data_0;
   const uint32_t array_of_some_sort[];
};
I find these can be useful when using memory mapped files.  In many file formats you'll have a header to a block of data, and then a variable number of some item following the header.  These sorts of trailing empty arrays can make traversing the files very easy I found.  These sorts of things are good when the memory is allocated through some other means.  As Hodgman pointed out, if you intend to allocate the memory your self, use a std::vector or similar container.

 

Yep. I'm using this frequently in resource management, rendering jobs, and similar tasks, because at that level things are just blobs with a specific header. Only certain sub-systems are able to interpret the blob correctly.

0

Share this post


Link to post
Share on other sites

Thanks for the solutions. I tried using vector but theres a problem. If you use a variable declared from vector, you wont be able to use that variable to extract data from a file because you cant convert a vector(unsigned char) to (char *). I am using ifstream file and file.read to get the data.

Edited by Psychopathetica
0

Share this post


Link to post
Share on other sites

you could first get the filesize, then preallocate enough space in the vector and push back chunks of the file that you read into a temporary buffer.

 

Edit: Because I haven't programmed in c++ for a while and your problem has piqued my interest. Here is the shortest form I think:

//... constructed before
std::vector<char> content();

//...load data
std::ifstream inputFile("input.dat", std::ios::binary);
content.assign(std::istreambuf_iterator<char>(inputFile)), std::istreambuf_iterator<char>());
Edited by Madhed
0

Share this post


Link to post
Share on other sites

Such a royal pain to comprehend on why something so simple can be so cryptic. Back when I was learning VB6 14 years ago, doing empty arrays in structures to be redeclared later wasnt so hard:

Private Type MyStruct
     MyArray() As Byte
End Type

Dim Test As MyStruct

Private Sub Form_Load()
     ReDim Test.MyArray(10) As Byte
End Sub

Even with VB.Net theres no issue:

Public Class Form1

    Private Structure MyStruct
        Dim MyArray() As Byte
    End Structure

    Dim Test As MyStruct

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ReDim Test.MyArray(10)
    End Sub
End Class

And although itll work in C++ to use a pointer and make it an array later (I think), I still kinda find it funny that you have to go through including the library vector, using vector<your data type here>, Resizing the vector, store the address of the vector into a pointer buffer to be used as an array by static_casting<char *>, load the data into that buffer and possibly put it back into the vector array. Mega fail Microsoft on not keeping it simple.

-3

Share this post


Link to post
Share on other sites

And although itll work in C++ to use a pointer and make it an array later (I think), I still kinda find it funny that you have to go through including the library vector, using vector<your data type here>, Resizing the vector, store the address of the vector into a pointer buffer to be used as an array by static_casting<char *>, load the data into that buffer and possibly put it back into the vector array. Mega fail Microsoft on not keeping it simple.


The point of the vector is to simplify memory management. As Ryan_001 already pointed out, if existing memory just needs to be interpreted in that specific way an empty array works and is simple.

Furthermore, std::vector has absolutely nothing to do with Microsoft or Windows. It's a part of the C++ standard library. It exists on Linux, Macs and smartphones and every other platform with a C++ compiler in the same way.
1

Share this post


Link to post
Share on other sites

Such a royal pain to comprehend on why something so simple can be so cryptic. Back when I was learning VB6 14 years ago, doing empty arrays in structures to be redeclared later wasnt so hard:

Private Type MyStruct
     MyArray() As Byte
End Type

Dim Test As MyStruct

Private Sub Form_Load()
     ReDim Test.MyArray(10) As Byte
End Sub
Even with VB.Net theres no issue:
Public Class Form1

    Private Structure MyStruct
        Dim MyArray() As Byte
    End Structure

    Dim Test As MyStruct

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ReDim Test.MyArray(10)
    End Sub
End Class

And although itll work in C++ to use a pointer and make it an array later (I think), I still kinda find it funny that you have to go through including the library vector, using vector<your data type here>, Resizing the vector, store the address of the vector into a pointer buffer to be used as an array by static_casting<char *>, load the data into that buffer and possibly put it back into the vector array. Mega fail Microsoft on not keeping it simple.

 
Ya C++ is funny like that.  Simple stuff tends to be harder than it should.  There's two main problems I personally run into a lot when working with files in C++.
 
The 1st is that the fundamental types are poorly defined.  Short can be 16 bits, or 32, 1024, who knows, a char might be 8 bits or might be 9000.  Which is why I always use the <cstdint> header and types when working with files or networking.  You can't be sure unsigned char is what you want, but you can be sure that a uint8_t is.
 
The second is that I'm not a fan of the C++ stream model.  I use it when necessary, but find it unnecessarily cumbersome.  I wrote a small wrapper around the Win32 file mapping routines and haven't looked back since.  Far cleaner, faster, easier to use, and easier to make bug free code.  That said its not portable so I understand its not a solution for everyone.  To my knowledge boost also has a memory mapping library, but I've never used it so I can't comment on it. Edited by Ryan_001
0

Share this post


Link to post
Share on other sites

 

The second is that I'm not a fan of the C++ stream model.

I've been using C++ professionally for over a decade and have never seen the standard stream library in use. I've only ever used it as part of "hello world" learning exercises biggrin.png

I still kinda find it funny that you have to go through including the library vector, using vector, Resizing the vector, store the address of the vector into a pointer buffer to be used as an array by static_casting, load the data into that buffer and possibly put it back into the vector array.

C/C++ are systems programming languages, that only make the computer do exactly what you tell it to do. C was originally designed to be a "portable assembly language" -- i.e. a language that translates almost directly down to native machine instructions.
 
If you want to use simpler abstractions (rather than being extremely verbose and precise in your code), then yes, a higher level language like VB is a far better choice for you. C/C++ are for solving a different class of problems than VB, and are designed accordingly.

 

 

What do you use for I/O in C++ then?  Do you have a go-to portable library, or do you just use something developed in house?

0

Share this post


Link to post
Share on other sites

Or the long overdue C++11 way:
 
char* buffer = Vec.data();
 

 

Nice, learned something new today.

Edited by Madhed
0

Share this post


Link to post
Share on other sites

Well I just figured out how to load data from a file and store it into a vector declared array that is from a structure:

File.read(reinterpret_cast<char *>(Test.Myarray.data()), Test.Myarray.size());
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0