Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Newbie Question: 'Sizeof' - I'm unclear what it actually measures,Modern equiv of older style reference manual?

Reply
Thread Tools

Newbie Question: 'Sizeof' - I'm unclear what it actually measures,Modern equiv of older style reference manual?

 
 
The Simple Romantic
Guest
Posts: n/a
 
      08-23-2010
On Aug 23, 4:05*pm, Christian Hackl <(E-Mail Removed)> wrote:
> Richard ha scritto:
>
> > Christian Hackl <(E-Mail Removed)> spake the secret code
> > <i4utnk$(E-Mail Removed)-september.org> thusly:

>
> >> In C++, the correct way to create an array of 5 ints and print its size
> >> is as follows:

>
> > std::vector<T> is a dynamic array.

>
> > Fixed-size arrays are perfectly fine in C++ [...]

>
> Well, try to return one from a function. Or pass one by reference.
> That's not exactly "pefectly fine". Arrays are a broken language
> construct, and beginners should just not be exposed to them at all.
>
> --
> Christian Hackl
> (E-Mail Removed)
>
> Milano 2008/2009 -- L'Italia chiamò, sì!


Thank you to everybody who who responded.

I got several good ideas here. And it helps to see what people
disagree about. I'll just keep plugging away, keeping in mind that no
book is going to be perfect - no doubt I will learn some things that
aren't the way I ought to work, which is why I've been going back and
forth between books. At the same time, I have to stick with each book
for awhile or I get nowhere. So if I end up later on saying that
something was worthless, or led me down the wrong path, at least I
went somewhere and learned something.

I just wrote up my revised study plan for how long to continue using
this book before returning to the other two books. I'm doing this one
now because the examples are very short and simple. The examples in
the other ones get complicated quickly. Also I know I need to take a
time-out to just work on developing the object oriented mindset. But I
want to, at least, work through the examples in this more simplistic
book before I do that.

Laurel
 
Reply With Quote
 
 
 
 
Gennaro Prota
Guest
Posts: n/a
 
      08-24-2010
On 24/08/2010 0.23, Laurel Shimer wrote:
> Working to develop comfort with the language,I have been typing up
> and running the code examples, and thinking through each line in each
> example to make sure I know what each command is doing. Then I try to
> make up my own very simple examples in the style of the ones in the
> book.
>
> (actually I have 3 books I go between, trying things out, and figuring
> out my personal study plan. I tend to work within a particular book
> for a couple of weeks. I return back once another author has gotten
> me over a stumbling block - self-study ain't easy)
>
> I'm currently working in the most basic of the 3 books ("Objective -C"
> A Visual Quickstart Guide by Steve Holzner )


Ok, stop here, please This newsgroup is about C++, which is
a different language. It's not that we don't want to help,
really, or nitpick: the issue is that languages may be subtly
different. Most of the replies I've seen in this thread seem to
have missed this.

[...]

> #include <stdio.h>
>
> int main()
> {
> int scores[5] = {92, 72, 57, 98, 89};
>
> printf ("The array is %i elements long.\n", (long) sizeof(scores) /
> (long) sizeof(int));
>
> printf ("sizeof(scores) is %i \n", (long) sizeof(scores) );
>
> printf ("(long) sizeof(int) is %i\n", (long) sizeof(int));
>
> return 0;
>
> }
> Running…
> The array is 5 elements long.
> sizeof(scores) is 20
> (long) sizeof(int) is 4


If this were C++ or C (which is yet another language) the "%i"
in the first argument to printf would tell that you want to
output an int. For a long, you'd have to replace it with "li".
Also, what type the result of sizeof has, could change from one
platform to another (although it would always be unsigned).

Given that you want to know about a different language, I won't
go into further details.

[other questions snipped...]

I searched on my NNTP server and saw that there's a newsgroup
with name comp.lang.objective-c. You should probably ask your
questions there.

About Wikipedia, which you mentioned in another post, it is no
reliable reference in general, as pretty much anything on the
web. However it contains lots of info and, if you are careful,
you can discern the correct and useful stuff from the rest (just
like on newsgroups).

Good luck with your learning.

--
Gennaro Prota | I'm available for your projects.
Breeze (preview): <https://sourceforge.net/projects/breeze/>
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      08-24-2010
Christian Hackl <(E-Mail Removed)> wrote:
> In C++, the correct way to create an array of 5 ints and print its size
> is as follows:

[...]
> std::vector<int> scores;


Says who?

If I say that the "correct" way of create such an array is by using
std::deque, are you going to say that I'm wrong?

Using std::vector is only one possible way of doing that. On what grounds
do you base your claim that it's "the correct way"?

Besides, suggesting using std::vector in all possible cases is
irresponsible. If you want an array of exactly 5 ints, and it will be
an array of 5 ints for the entire duration of the program, then
std::vector is one of the least efficient ways of doing that. This is
especially true if it will be instantiated many times, for example as
the member of a class (which is instantiated and copied around a lot).

If I needed an array of exactly 5 ints in a class, I would very definitely
write it as:

class TheClass
{
int theArray[5]; // The *correct* way
...
};

and very definitely not as:

class TheClass
{
std::vector<int> theArray; // The *incorrect* way
...
};

As said, if an array of exactly 5 ints is needed, using std::vector for
that is an extremely inefficient solution.

> Note that what some other languages usually call "array" is called
> "std::vector" in C++.


Again, says who?

If I used std::deque instead, would you claim I'm wrong? On what do you
base your claim on?

> IOW, it's the language's default container.


Default container? Where does it say that?

> A vector knows its own size and does not require you to cast anything.


Neither does std::deque. Why are you shunning it?

> The thing you have used are "real", or low-level, arrays. You usually do
> not need them in C++, especially when you are a beginner. They will get
> you into a lot of trouble and gain you nothing.


If the array has a fixed size and is small, using an array rather than
std::vector is definitely more efficient (regardless of the context).
How is that "gain you nothing"?
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      08-24-2010
Christian Hackl <(E-Mail Removed)> wrote:
> Well, try to return one from a function. Or pass one by reference.


Arrays themselves cannot be handled "by value" (which makes them a bit
different from all the other constructs), but you can get around that
limitation by embedding the array inside a struct. Then you can handle
it "by value" all you want. You can return it from a function, pass it
to functions by value, and pass references to it.

> That's not exactly "pefectly fine". Arrays are a broken language
> construct, and beginners should just not be exposed to them at all.


Too bad that arrays happen to be the most memory-efficient constructs
in existence (for storing sequential data). They consume the minimum
amount of memory (for raw data; exotic data containers which somehow
use compression techniques for storing the data are a story of their
own) and they are usually extremely fast to instantiate in many situations
(eg. because they are basically the only "data container" that can be
allocated on the stack, alongside POD structs, if you want to consider
them "data containers" as well).
 
Reply With Quote
 
Francesco S. Carta
Guest
Posts: n/a
 
      08-24-2010
Laurel Shimer <(E-Mail Removed)>, on 23/08/2010 15:23:29, wrote:

<questions about managing raw arrays>

I wonder why nobody pointed out that we can simply wrap raw arrays into
a template in order to bring along their sizes, and we can do that quite
easily too.

It took me way more time to come up with the silly test case than to
write the actual wrapper:

//-------
// in some header or hidden in an implementation file
// with fitting explicit instantiations in an appropriate header
// (or maybe exported, if you're lucky)
// this can be, of course, widely extended and improved

template <class T, int SIZE> class Array {
public:
T& operator[](int index) {
return data[index];
}
const T& operator[](int index) const {
return data[index];
}
int size() const {
return SIZE;
}
private:
T data[SIZE];
};

//-------

//-------
// really silly test case

#include <iostream>
#include <iomanip>
#include <cassert>

// include the appropriate header or paste the template here

using namespace std;

template<class T> void MultiplyMatrixIndices(T& matrix) {
for (int y = 0; y < matrix.size(); ++y) {
for (int x = 0; x < matrix[y].size(); ++x) {
matrix[y][x] = x * y;
}
}
}

template<class T> void PrintMatrix(const T& matrix) {
const int W = 3;
cout << right << setw(W) << "x*y";
for (int x = 0; x < matrix[0].size(); ++x) {
cout << setw(W) << x;
}
cout << endl;

for (int y = 0; y < matrix.size(); ++y) {
cout << setw(W) << y;
for (int x = 0; x < matrix[y].size(); ++x) {
cout << setw(W) << matrix[y][x];
}
cout << endl;
}
}

int main() {
const int index1 = 7;
const int index2 = 8;

Array< Array<int, index2>, index1> m;

MultiplyMatrixIndices(m);
PrintMatrix(m);

return 0;
}

//-------


--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      08-24-2010
[Please do not mail me a copy of your followup]

http://www.velocityreviews.com/forums/(E-Mail Removed) spake the secret code
<4c73d8a1$0$6831$(E-Mail Removed)> thusly:

>Laurel Shimer <(E-Mail Removed)>, on 23/08/2010 15:23:29, wrote:
>
><questions about managing raw arrays>
>
>I wonder why nobody pointed out that we can simply wrap raw arrays into
>a template in order to bring along their sizes, and we can do that quite
>easily too.


Boost.Range abstracts away the differences between STL containers,
pairs of iterators and raw arrays. This lets you do things like:

template <typename C, typename I>
bool contains(C & collection, const I & item)
{
return std::find( boost::begin( collection ),
boost::end( collection ), item )
!= boost::end( collection);
}

int p[] = { 5, 4, 3, 2, 1 };
assert(contains(p, 3));

The most common case of using algorithms like std::find is to use
c.begin() and c.end() as the iterator pair, but using Boost.Range I
can factor out that duplication into a "contains" function that will
work on any container including raw arrays.
--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
<http://legalizeadulthood.wordpress.com/the-direct3d-graphics-pipeline/>

Legalize Adulthood! <http://legalizeadulthood.wordpress.com>
 
Reply With Quote
 
Francesco S. Carta
Guest
Posts: n/a
 
      08-24-2010
Richard <(E-Mail Removed)>, on 24/08/2010 15:47:23, wrote:

> [Please do not mail me a copy of your followup]
>
> (E-Mail Removed) spake the secret code
> <4c73d8a1$0$6831$(E-Mail Removed)> thusly:
>
>> Laurel Shimer<(E-Mail Removed)>, on 23/08/2010 15:23:29, wrote:
>>
>> <questions about managing raw arrays>
>>
>> I wonder why nobody pointed out that we can simply wrap raw arrays into
>> a template in order to bring along their sizes, and we can do that quite
>> easily too.

>
> Boost.Range abstracts away the differences between STL containers,
> pairs of iterators and raw arrays. This lets you do things like:
>
> template<typename C, typename I>
> bool contains(C& collection, const I& item)
> {
> return std::find( boost::begin( collection ),
> boost::end( collection ), item )
> != boost::end( collection);
> }
>
> int p[] = { 5, 4, 3, 2, 1 };
> assert(contains(p, 3));
>
> The most common case of using algorithms like std::find is to use
> c.begin() and c.end() as the iterator pair, but using Boost.Range I
> can factor out that duplication into a "contains" function that will
> work on any container including raw arrays.


But not including raw pointers, which is what we get as soon as we try
to pass a raw array around.

I'm just advocating the usefulness of wrapping raw arrays, I am in no
way criticizing your contribution, which I share and support.

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      08-24-2010
Christian Hackl <(E-Mail Removed)> wrote:
> Juha Nieminen ha scritto:
>
>> Christian Hackl <(E-Mail Removed)> wrote:
>>> In C++, the correct way to create an array of 5 ints and print its size
>>> is as follows:

>> [...]
>>> std::vector<int> scores;

>>
>> Says who?
>>
>> If I say that the "correct" way of create such an array is by using
>> std::deque, are you going to say that I'm wrong?

>
> Yes, because it goes against experience and common sense.


Exactly how does it go against "experience and common sense"?

An array is a sequential data container which can be indexed in constant
time. Both std::vector and std::deque fulfill that requirement. You can
traverse an array in linear time, and both std::vector and std::deque
fulfill that requirement. If we are talking about dynamic arrays, where
you can add and remove elements at will, both std::vector and std::deque
fulfill that requirement. They even have the same computational complexity
for doing so (at least when appending data to the end or insertinging data
to the middle). They even have similar iterator invalidation behavior.

The one major difference between the two is that you can insert elements
to the beginning of a std::deque in constant time, while with std::vector
that would be a linear-time operation. However, that hardly disqualifies
std::deque as a valid substitute for raw arrays.

They look pretty much similar on their public interfaces (even if their
internal implementation is quite different, but that should be pretty
inconsequential in most cases).

So, once again: Why exactly is std::vector the "correct" way to create
an array and std::deque the "wrong" way?

Due to their internal implementation there are situations where
std::vector is more suitable and efficient, while in other situations
std::deque is considerably better (either because of its additional
features, its efficiency in certain situations, or both). However,
as a pure array replacement they look pretty much the same.

> std::vector is clearly designed as an array replacement.


As said, std::deque looks pretty much the same from the outside.
What stops it from being a good array replacement?

> There's a reason no one starts teaching STL containers with std::deque.


And what would that reason be? Prejudice?

> Realistically, the next thing he will wish to learn is how to read some
> integer numbers from the command line, and at this point he already has
> to unlearn everything he was taught about efficient fixed-size arrays.


Has to unlearn? That doesn't make any sense. You talk like a person
could only know either about fixed arrays or about std::vector, but not
both at the same time, and thus if he wants to learn about std::vector
he has to "unlearn" fixed arrays.

Obviously a person, even a beginner, can understand *both* constructs
at the same time. How about teaching both at the same time?
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      08-25-2010
On Aug 23, 11:52 pm, (E-Mail Removed) (Richard)
wrote:
> Christian Hackl <(E-Mail Removed)> spake the secret code
> <i4utnk$(E-Mail Removed)-september.org> thusly:


> >In C++, the correct way to create an array of 5 ints and print its size
> >is as follows:


> std::vector<T> is a dynamic array.


> Fixed-size arrays are perfectly fine in C++ and the syntax of
> aggregate initialization from C is also perfectly fine.


Not to mention the fact that a C style array can have fully
static initialization, thus avoiding any order of initialization
problems.

Still, it's for special uses (most of mine are const); for
general work, std::vector is the choice, even if you're not
going to be adjusting the size once it has been constructed.

> sizeof() is what you want to tell you the size of a fixed-size
> array, i.e. sizeof(array_)/sizeof(array_[0])


For that, I use the template function size (which will cause
a compiler error, rather than a wrong value, if invoked on
a pointer.

--
James Kanze
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      08-25-2010
On Aug 24, 3:35 pm, "Francesco S. Carta" <(E-Mail Removed)> wrote:
> Laurel Shimer <(E-Mail Removed)>, on 23/08/2010 15:23:29, wrote:


> <questions about managing raw arrays>


> I wonder why nobody pointed out that we can simply wrap raw arrays into
> a template in order to bring along their sizes, and we can do that quite
> easily too.


You mean std::array, from C++0x (or tr1::array, before that).

Note that any solution involves compromizes. It's far from
trivial to come up with the "correct" solution. (In fact, one
should probably say *a* correct solution, since depending on
what you want, the correct solution might not be the same.)

> It took me way more time to come up with the silly test case than to
> write the actual wrapper:


> //-------
> // in some header or hidden in an implementation file
> // with fitting explicit instantiations in an appropriate header
> // (or maybe exported, if you're lucky)
> // this can be, of course, widely extended and improved


> template <class T, int SIZE> class Array {
> public:
> T& operator[](int index) {
> return data[index];
> }
> const T& operator[](int index) const {
> return data[index];
> }
> int size() const {
> return SIZE;
> }
> private:
> T data[SIZE];
> };


This is interesting. You can't actually instantiate it unless
T has a default constructor, and all of the data are initialized
using the default constructor.

Probably the most important single reason for using a C style
array is the aggregate initialization sequence, and the fact
that it can have fully static initialization. (std::array
preserves this quality.)

--
James Kanze
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Still unclear Irfaan Wahid MCSD 3 02-24-2005 12:14 PM
Scope of news group? Charter unclear Shmuel (Seymour J.) Metz XML 1 07-09-2004 03:41 PM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM
Unclear on the concept... kj XML 2 04-12-2004 02:49 AM
Re: media player 9/windows xp, video&sound unclear Juan Pérez Computer Support 0 08-08-2003 01:21 AM



Advertisments