Empty Array In Structures

Started by
18 comments, last by Servant of the Lord 10 years, 2 months ago

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.

Advertisement

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.

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.
The C++ standard does not specify the size of integral types in bytes, but it specifies minimum ranges they must be able to hold.

http://stackoverflow.com/questions/589575/size-of-int-long-etc

"I would try to find halo source code by bungie best fps engine ever created, u see why call of duty loses speed due to its detail." -- GettingNifty

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.

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?

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?

It has been my experience over the last 3 decades that most C++ code is C-with-classes written by C programmers, and more recently by Java programmers. There is a lot of printf()-style stream formatting used in production code.

Part of this is because <iostream> is a string formatting library (not an I/O library), and most production applications these days are either GUI or server so most string formatting is for logging purposes, and the NIH syndrome is particularly apparent in the realm of logging. Most Reuleaux loggers I've seen are either reimplementations of the standard Java classes or wrappers around the standard C calls. There's no reason why the C++ streams can't be used directly for these purposes without great success, other than the prejudice of the developers.

Another reason is that the C++ formatting streams support internationalization only with great difficulty. Most solutions for that serious functional deficit end up looking remarkably like printf (or even Fortran) with formatting strings. A solution like the Boost solution (a localizable format string injected into the iostream but the standard streambufs and operator<<()/operator>>() overloads are used) is the best of both worlds.

Stephen M. Webb
Professional Free Software Developer


Or the long overdue C++11 way:

char* buffer = Vec.data();

Nice, learned something new today.

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());

Alternatively, if not using C++11 and you don't have data(), you can do: &myVector[0]

This topic is closed to new replies.

Advertisement