Wednesday, March 11, 2015

Digest for comp.lang.c++@googlegroups.com - 25 updates in 3 topics

Christian Gollwitzer <auriocus@gmx.de>: Mar 11 08:11PM +0100

Am 11.03.15 um 19:46 schrieb JiiPee:
 
> array<int> a(10);
 
> Which take objects allocation (couple of bytes) plus 10. So C makes
> shorter executable what many want.
 
Are you sure this is correct? My understanding of std::array vs.
std::vector is that the latter gives you dynamic memory with it's
benefits (dyanamic growth etc.) and drawbacks (overhead), while the
former gives you zero overhead compared to a C-style overhead but static
type safety.
 
Christian
JiiPee <no@notvalid.com>: Mar 11 07:14PM

On 11/03/2015 18:56, Mr Flibble wrote:
> "std::array<char> a;". A std::array does not perform any allocations;
> it's storage is inline just like for a C style array.
 
> /Flibble
 
ok sorry, might be.... then it was:
std::vector<int> which takes extra allocation for the object. And now..
dont say this is not true as I have definitely tested it myself!!! :)
 
and in real life we really most of the time need that vector-system. So
dynamical C-array is smaller
JiiPee <no@notvalid.com>: Mar 11 07:21PM

On 11/03/2015 19:11, Christian Gollwitzer wrote:
> former gives you zero overhead compared to a C-style overhead but static
> type safety.
 
> Christian
 
okey, array maybe not, but vector<int> for sure. there is an object overhead
Christopher Pisz <nospam@notanaddress.com>: Mar 11 02:31PM -0500

On 3/11/2015 2:14 PM, JiiPee wrote:
 
> ok sorry, might be.... then it was:
> std::vector<int> which takes extra allocation for the object. And now..
> dont say this is not true as I have definitely tested it myself!!! :)
 
 
did you use std::vector::reserve in your test?
 
> and in real life we really most of the time need that vector-system. So
> dynamical C-array is smaller
 
char a[10]; is not a _dynamic_ array.
 
 
--
I have chosen to troll filter/ignore all subthreads containing the
words: "Rick C. Hodgins", "Flibble", and "Islam"
So, I won't be able to see or respond to any such messages
---
JiiPee <no@notvalid.com>: Mar 11 07:32PM

On 11/03/2015 18:57, Mr Flibble wrote:
>> it's storage is inline just like for a C style array.
 
> Er, I of course meant "std::array<char, 10> a;".
 
> /Flibble
 
Flibble.... i just faced this issue recently when somebody asked me to
change their C-code to C++. Its a music hardware which has only like 64K
memory. He wanted to save every byte. I first said "well, I would use
std::vector.... lets install std stuff". But later I started to agree
with him that plain old C-array takes less space and he does not need
really that vector there (well, was it vector or array i dont remember,
but anyway.. the idea that objects take more space).
 
Although now as am thinking, he created many other classes and objects
willingly, surely they also take extra memory... so its not so obvious
which is best.
Christopher Pisz <nospam@notanaddress.com>: Mar 11 02:33PM -0500

On 3/11/2015 12:29 PM, Lynn McGuire wrote:
> famous naysayers and their profane soliloquies about inelegance dissuade
> you."
 
> Lynn
 
 
Who cares what Linus Torvalds says about anything other than the Linux
Kernel?
 
 
--
I have chosen to troll filter/ignore all subthreads containing the
words: "Rick C. Hodgins", "Flibble", and "Islam"
So, I won't be able to see or respond to any such messages
---
Paavo Helde <myfirstname@osa.pri.ee>: Mar 11 02:35PM -0500

> dont say this is not true as I have definitely tested it myself!!! :)
 
> and in real life we really most of the time need that vector-system. So
> dynamical C-array is smaller
 
You need std::vector in real life because you want dynamic resizing or
maybe because the array is just too large to allocate on stack. In both
cases this can be done in C as well, but then you need an additional
pointer and maybe also another variable to hold the array size - which
again produces about the same overhead than std::vector!
 
In the extreme case where each byte must be conserved, I agree that with C-
style arrays and lots of work one can win some bytes. But as C is a proper
subset of C++, the same code can then be used as C++ code and still have
better type-checking and other goodies.
 
hth
Paavo
JiiPee <no@notvalid.com>: Mar 11 07:37PM

On 11/03/2015 19:35, Paavo Helde wrote:
> better type-checking and other goodies.
 
> hth
> Paavo
 
yes, and thats what I did with one of my clients... we did a lot of C
code, but added type checking with C++ style as much as possible (or at
least I recommend them)
JiiPee <no@notvalid.com>: Mar 11 07:38PM

On 11/03/2015 19:35, Paavo Helde wrote:
> better type-checking and other goodies.
 
> hth
> Paavo
 
like they were using
#define MY_VARIABLE 6
 
and i said "this really must be:
const int MY_VARIABLE = 6;
 
"
 
he first hesitated because he was worried it takes more memory, but he
googled and found that not....so he accepted :)
Ian Collins <ian-news@hotmail.com>: Mar 12 08:39AM +1300

JiiPee wrote:
>> former gives you zero overhead compared to a C-style overhead but static
>> type safety.
 
> okey, array maybe not, but vector<int> for sure. there is an object overhead
 
No more or less than the overhead for a dynamic array in C.
 
--
Ian Collins
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Mar 11 07:50PM

On 11/03/2015 19:21, JiiPee wrote:
 
>> Christian
 
> okey, array maybe not, but vector<int> for sure. there is an object
> overhead
 
The "object overhead" of std::vector<int> is constant (O(1)) so becomes
insignificant as n increases; the space complexity of std::vector,
dynamic C arrays and static C arrays is identical: O(n).
 
If you have lots of vectors of small n you are probably doing it wrong.
 
/Flibble
JiiPee <no@notvalid.com>: Mar 11 07:54PM

On 11/03/2015 19:39, Ian Collins wrote:
 
>> okey, array maybe not, but vector<int> for sure. there is an object
>> overhead
 
> No more or less than the overhead for a dynamic array in C.
 
std::vector<int> a;
std:: cout << sizeof(a) << std::endl;
 
prints 12
 
int* b = nullptr;
std:: cout << sizeof(b) << std::endl;
 
prints 4
 
So an empty vector seems to take 12 bytes, and empty C-array 4 bytes
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Mar 11 07:58PM

On 11/03/2015 19:54, JiiPee wrote:
> std:: cout << sizeof(b) << std::endl;
 
> prints 4
 
> So an empty vector seems to take 12 bytes, and empty C-array 4 bytes
 
to be equivalent you also need to record the size of the C-array which
is an extra 4 bytes and also an extra 4 bytes if you want to support
vector's capacity which makes 12 bytes.
 
/Flibble
JiiPee <no@notvalid.com>: Mar 11 08:05PM

On 11/03/2015 19:58, Mr Flibble wrote:
> is an extra 4 bytes and also an extra 4 bytes if you want to support
> vector's capacity which makes 12 bytes.
 
> /Flibble
 
if the size is the same for different types of variables, then only one
variable would do the job instead of multiple.
like:
 
int size = 10;
int* a = new int[size ];
float* b = new float[size ];
double* c = new double[size ];
 
so in C we need total : 4 bytes overhead
 
in C++:
vector<int> a = ...10);
vector<float> b = ...10);
vector<double> c = ..10).;
 
would need total: 36 bytes of overhead
JiiPee <no@notvalid.com>: Mar 11 08:06PM

On 11/03/2015 19:33, Christopher Pisz wrote:
 
>> Lynn
 
> Who cares what Linus Torvalds says about anything other than the Linux
> Kernel?
 
come on, be careful... he is from my country: Finland!!! :)
Wouter van Ooijen <wouter@voti.nl>: Mar 11 09:10PM +0100

JiiPee schreef op 11-Mar-15 om 8:32 PM:
> change their C-code to C++. Its a music hardware which has only like 64K
> memory. He wanted to save every byte. I first said "well, I would use
> std::vector.... lets install std stuff".
 
Why use a std::vector, which is a flexible array, if you want a fixed
array? You can't blame the language (or rather, the library) for giving
you options that are wrong for your situation.
 
> Although now as am thinking, he created many other classes and objects
> willingly, surely they also take extra memory... so its not so obvious
> which is best.
 
A class does not need to take more memory than the data it stores and
the code that implemnts its methods.
 
Wouter van Ooijen
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Mar 11 08:21PM

On 11/03/2015 20:05, JiiPee wrote:
> vector<float> b = ...10);
> vector<double> c = ..10).;
 
> would need total: 36 bytes of overhead
 
If you pass your dynamic C array "a" to a generic function how does that
function know how many elements there are? You need to couple the
pointer with a size for dynamic arrays (just like vector does) to do
anything useful. If "size" is always fixed at 10 then you don't need
dynamic arrays and you are not comparing like with like when comparing
with vector as vectors can grow.
 
Your examples and analysis of the situation is artificial and naive;
read my other reply regarding the space complexity of the various options.
 
/Flibble
JiiPee <no@notvalid.com>: Mar 11 08:25PM

On 11/03/2015 20:21, Mr Flibble wrote:
 
>> would need total: 36 bytes of overhead
 
> If you pass your dynamic C array "a" to a generic function how does
> that function know how many elements
 
you pass the size-variable with it. (size is 10 here). so where ever you
use a -array you pass size with it.
 
Greg Martin <greg.at.softsprocket@dot.com>: Mar 11 08:26PM


>> Lynn
 
> Who cares what Linus Torvalds says about anything other than the Linux
> Kernel?
 
A lot of people as it happens. Whether they should or not is another
matter.
 
--
http://www.softsprocket.com
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Mar 11 08:31PM

On 11/03/2015 20:25, JiiPee wrote:
>> that function know how many elements
 
> you pass the size-variable with it. (size is 10 here). so where ever you
> use a -array you pass size with it.
 
And passing it with it everywhere is equivalent to it being part of it
.. so 8 bytes of overhead.
 
From my other reply: the "object overhead" as you call it of
std::vector is only significant for small n. If you have lots of
vectors of small n then you are probably doing it wrong.
 
/Flibble
Paavo Helde <myfirstname@osa.pri.ee>: Mar 11 04:15PM -0500

> int* a = new int[size ];
> float* b = new float[size ];
> double* c = new double[size ];
 
This is not exactly equivalent to std::vector because the capacity and
efficient dynamic resizing are missing.
 
 
> so in C we need total : 4 bytes overhead
 
16 bytes, if you want to compare correctly. Each pointer is an overhead.
And if you add capacities, it will make 28 bytes.
 
> vector<float> b = ...10);
> vector<double> c = ..10).;
 
> would need total: 36 bytes of overhead
 
If the vectors are always of the same length, then the solution is clear:
 
struct X {
int a;
float b;
double c;
};
 
std::vector<X> x;
 
Voila: this has 12 bytes overhead, which is 4 bytes less than the C
version, plus it supports efficient dynamic resizing as a bonus, plus it
is not error-prone and exception-unsafe - an even bigger bonus. Q.E.D.
 
Cheers
Paavo
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Mar 11 09:29PM

On 11/03/2015 21:15, Paavo Helde wrote:
 
> Voila: this has 12 bytes overhead, which is 4 bytes less than the C
> version, plus it supports efficient dynamic resizing as a bonus, plus it
> is not error-prone and exception-unsafe - an even bigger bonus. Q.E.D.
 
You could of course have a dynamic array of X also.
 
In my most humble opinion dynamic arrays should only be used for one
thing: allocating uninitialised buffers of char; use std::vector for
everything else..
 
/Flibble
JiiPee <no@notvalid.com>: Mar 11 09:31PM

On 11/03/2015 20:31, Mr Flibble wrote:
>> use a -array you pass size with it.
 
> And passing it with it everywhere is equivalent to it being part of it
> .. so 8 bytes of overhead.
 
it remains 4 bytes like i originally calculated. size is a global
variable (defined in a same place where "a" is defined). Why 8? size is
4 bytes....
 
you mean the function needs another size variable as an argument?
Victor Bazarov <v.bazarov@comcast.invalid>: Mar 11 03:36PM -0400


> class Song // every song has it's own class
> string &name; // full path to song on disk.
> virtual GetRelease(); // With a pointer to any Song class, one
 
virtual WHAT GetRelease() ? What's the return type?
 
> virtual GetFullPath(); / /Artist associated with that song.
 
> class Disc : Song // Each Release has at least 1 disc
> virtual int GetDiscNumber();
 
'Disc' derives from 'Song'? So a 'Disc' IS-A 'Song'? I am not sure I
actually agree. Shouldn't 'Disc' be actually a container of 'Songs'?
 
 
> Looking for suggestions on how one would go about designed a good class
> hierarchy to represent the music of a particular recording artist.
 
> Any suggestion greatly appreciated.
 
OK, here is a suggestion: start from the right end. What you need is a
system that *uses* your objects. From their *use* you can distill their
*interface*. Once you have the interface, the class design becomes a
walk in the park. So, start with describing how your objects are going
to be *used*.
 
And it doesn't really matter what language you are going to use to
implement your system. Don't try designing with any specific language
in mind. Forget 'virtual' or 'array<>' or "pointer to". Use
pseudo-code to describe the relationships. Translating your pseudo-code
into any language of your choice is not important to the design, really.
What if tomorrow you decide to make a prototype of your system in Python?
 
I probably didn't understand the entity relationships in your
explanation, so excuse my reaction. I hope you will see the problem
from my POV.
 
Good luck.
 
V
--
I do not respond to top-posted replies, please don't ask
drew@furrfu.invalid (Drew Lawson): Mar 11 07:13PM

In article <mdpq3k$brm$1@speranza.aioe.org>
>In CGI, the POST-data comes from the standard input,
>so simply use fgets/fread/etc on standard input.
 
The content of the post is on the standard input, but the size in
in the HTTP variable CONTENT_LENGTH. That is retrieved as an
environment variable. The CGI spec does not require end-of-file
at the end of data. I know from experience that Apache 1.3 did not
do that. I've never tested whether Apache 2 does.
 
 
--
Drew Lawson | Pass the tea and sympathy
| for he good old days are dead
| Let's raise a toast to those
| who best survived the life they led
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: