Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #19026 > unrolled thread

C/C++ question about dynamic "static struct"

Started bygus gassmann <gus@nospam.com>
First post2012-10-17 06:49 -0300
Last post2012-10-19 07:37 -0400
Articles 20 on this page of 153 — 30 participants

Back to article view | Back to comp.lang.c++


Contents

  C/C++ question about dynamic "static struct" gus gassmann <gus@nospam.com> - 2012-10-17 06:49 -0300
    Re: C/C++ question about dynamic "static struct" Noob <root@127.0.0.1> - 2012-10-17 13:08 +0200
      Re: C/C++ question about dynamic "static struct" James Kuyper <jameskuyper@verizon.net> - 2012-10-17 08:19 -0400
      Re: C/C++ question about dynamic "static struct" Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-10-17 15:19 +0000
      Re: C/C++ question about dynamic "static struct" gus gassmann <gus@nospam.com> - 2012-10-17 18:47 -0300
        Re: C/C++ question about dynamic "static struct" Ike Naar <ike@iceland.freeshell.org> - 2012-10-17 22:46 +0000
          Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-18 07:13 +0000
        Re: C/C++ question about dynamic "static struct" James Kuyper <jameskuyper@verizon.net> - 2012-10-17 18:58 -0400
          Re: C/C++ question about dynamic "static struct" Stuart <DerTopper@web.de> - 2012-10-18 09:55 +0200
            Re: C/C++ question about dynamic "static struct" jacob navia <jacob@spamsink.net> - 2012-10-18 10:13 +0200
              Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-18 08:23 +0000
                Re: C/C++ question about dynamic "static struct" jacob navia <jacob@spamsink.net> - 2012-10-18 11:17 +0200
                  Re: C/C++ question about dynamic "static struct" Ian Collins <ian-news@hotmail.com> - 2012-10-18 23:37 +1300
                  Re: C/C++ question about dynamic "static struct" Rui Maciel <rui.maciel@gmail.com> - 2012-10-18 15:35 +0100
                    Re: C/C++ question about dynamic "static struct" Öö Tiib <ootiib@hot.ee> - 2012-10-19 11:40 -0700
              Re: C/C++ question about dynamic "static struct" Ian Collins <ian-news@hotmail.com> - 2012-10-18 21:45 +1300
              Re: C/C++ question about dynamic "static struct" Rui Maciel <rui.maciel@gmail.com> - 2012-10-18 15:03 +0100
            Re: C/C++ question about dynamic "static struct" James Kuyper <jameskuyper@verizon.net> - 2012-10-18 07:18 -0400
              Re: C/C++ question about dynamic "static struct" Stuart <DerTopper@web.de> - 2012-10-18 17:43 +0200
            Re: C/C++ question about dynamic "static struct" William Ahern <william@wilbur.25thandClement.com> - 2012-10-18 13:52 -0700
              Re: C/C++ question about dynamic "static struct" Ian Collins <ian-news@hotmail.com> - 2012-10-19 21:34 +1300
              Re: C/C++ question about dynamic "static struct" Stuart <DerTopper@web.de> - 2012-10-19 11:03 +0200
                Re: C/C++ question about dynamic "static struct" "BartC" <bc@freeuk.com> - 2012-10-20 14:03 +0100
              Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-19 09:27 +0000
                Re: C/C++ question about dynamic "static struct" Stuart <DerTopper@web.de> - 2012-10-19 13:43 +0200
                  Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-19 12:46 +0000
                    Re: C/C++ question about dynamic "static struct" ImpalerCore <jadill33@gmail.com> - 2012-10-19 10:14 -0700
                    Re: C/C++ question about dynamic "static struct" Paavo Helde <myfirstname@osa.pri.ee> - 2012-10-19 14:53 -0500
                      Re: C/C++ question about dynamic "static struct" Öö Tiib <ootiib@hot.ee> - 2012-10-19 13:52 -0700
                        Re: C/C++ question about dynamic "static struct" Öö Tiib <ootiib@hot.ee> - 2012-10-19 14:13 -0700
                        Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-19 21:24 +0000
                          Re: C/C++ question about dynamic "static struct" Ian Collins <ian-news@hotmail.com> - 2012-10-20 11:47 +1300
                      Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-19 21:22 +0000
                Re: C/C++ question about dynamic "static struct" William Ahern <william@wilbur.25thandClement.com> - 2012-10-19 11:21 -0700
                  Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-19 21:29 +0000
                    Re: C/C++ question about dynamic "static struct" James Kuyper <jameskuyper@verizon.net> - 2012-10-19 18:53 -0400
                      Re: C/C++ question about dynamic "static struct" Ian Collins <ian-news@hotmail.com> - 2012-10-20 12:13 +1300
                        Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-20 05:55 +0000
                          Re: C/C++ question about dynamic "static struct" Stuart <DerTopper@web.de> - 2012-10-20 13:24 +0200
                            Re: C/C++ question about dynamic "static struct" Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-20 12:54 +0100
                            Re: C/C++ question about dynamic "static struct" Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-10-20 06:45 -0700
                              Re: C/C++ question about dynamic "static struct" Ian Collins <ian-news@hotmail.com> - 2012-10-21 09:20 +1300
                              Re: C/C++ question about dynamic "static struct" Dombo <dombo@disposable.invalid> - 2012-10-20 22:28 +0200
                              Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-21 07:05 +0000
                                Re: C/C++ question about dynamic "static struct" Les Cargill <lcargill99@comcast.com> - 2012-10-21 02:23 -0500
                                  Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-21 07:32 +0000
                                    Re: C/C++ question about dynamic "static struct" "BartC" <bc@freeuk.com> - 2012-10-21 11:56 +0100
                                    Re: C/C++ question about dynamic "static struct" Les Cargill <lcargill99@comcast.com> - 2012-10-21 12:54 -0500
                                      Re: C/C++ question about dynamic "static struct" ptyxs <kerloch@gmail.com> - 2012-10-22 13:43 +0200
                                        Re: C/C++ question about dynamic "static struct" gwowen <gwowen@gmail.com> - 2012-10-26 03:37 -0700
                                          Re: C/C++ question about dynamic "static struct" Les Cargill <lcargill99@comcast.com> - 2012-10-26 07:33 -0500
                                    Re: C/C++ question about dynamic "static struct" Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-10-27 05:14 -0700
                                Re: C/C++ question about dynamic "static struct" Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-10-27 05:08 -0700
                          Re: C/C++ question about dynamic "static struct" Les Cargill <lcargill99@comcast.com> - 2012-10-20 13:44 -0500
                            Re: C/C++ question about dynamic "static struct" Ian Collins <ian-news@hotmail.com> - 2012-10-21 09:09 +1300
                              Re: C/C++ question about dynamic "static struct" Dombo <dombo@disposable.invalid> - 2012-10-20 22:31 +0200
                              Re: C/C++ question about dynamic "static struct" Les Cargill <lcargill99@comcast.com> - 2012-10-21 02:11 -0500
                                Re: C/C++ question about dynamic "static struct" Ian Collins <ian-news@hotmail.com> - 2012-10-21 20:28 +1300
                                  Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-21 07:40 +0000
                                  Re: C/C++ question about dynamic "static struct" Les Cargill <lcargill99@comcast.com> - 2012-10-21 13:31 -0500
                                    Re: C/C++ question about dynamic "static struct" Ian Collins <ian-news@hotmail.com> - 2012-10-22 08:41 +1300
                                      Re: C/C++ question about dynamic "static struct" Les Cargill <lcargill99@comcast.com> - 2012-10-21 16:24 -0500
                                        Re: C/C++ question about dynamic "static struct" Öö Tiib <ootiib@hot.ee> - 2012-10-21 15:05 -0700
                                        Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-22 07:52 +0000
                                          Re: C/C++ question about dynamic "static struct" Les Cargill <lcargill99@comcast.com> - 2012-10-22 14:02 -0500
                                            Re: C/C++ question about dynamic "static struct" gwowen <gwowen@gmail.com> - 2012-10-26 03:41 -0700
                                              Re: C/C++ question about dynamic "static struct" Les Cargill <lcargill99@comcast.com> - 2012-10-26 07:53 -0500
                                                Re: C/C++ question about dynamic "static struct" James Kuyper <jameskuyper@verizon.net> - 2012-10-26 09:51 -0400
                                                  Re: C/C++ question about dynamic "static struct" James Kuyper <jameskuyper@verizon.net> - 2012-10-26 13:48 -0400
                                                Re: C/C++ question about dynamic "static struct" ImpalerCore <jadill33@gmail.com> - 2012-10-26 07:35 -0700
                                                  Re: C/C++ question about dynamic "static struct" Paavo Helde <myfirstname@osa.pri.ee> - 2012-10-26 18:42 -0500
                                                    Re: C/C++ question about dynamic "static struct" ImpalerCore <jadill33@gmail.com> - 2012-10-27 04:21 -0700
                                                      Re: C/C++ question about dynamic "static struct" Öö Tiib <ootiib@hot.ee> - 2012-10-27 05:04 -0700
                                                      Re: C/C++ question about dynamic "static struct" Paavo Helde <myfirstname@osa.pri.ee> - 2012-10-27 08:06 -0500
                                                        Re: C/C++ question about dynamic "static struct" ImpalerCore <jadill33@gmail.com> - 2012-10-27 06:50 -0700
                                                          Re: C/C++ question about dynamic "static struct" Paavo Helde <myfirstname@osa.pri.ee> - 2012-10-27 11:13 -0500
                                                            Re: C/C++ question about dynamic "static struct" ImpalerCore <jadill33@gmail.com> - 2012-10-27 12:02 -0700
                                                              Re: C/C++ question about dynamic "static struct" Paavo Helde <myfirstname@osa.pri.ee> - 2012-10-27 15:46 -0500
                                                                Re: C/C++ question about dynamic "static struct" ImpalerCore <jadill33@gmail.com> - 2012-10-27 20:24 -0700
                                                      Re: C/C++ question about dynamic "static struct" Dombo <dombo@disposable.invalid> - 2012-10-27 20:11 +0200
                                                Re: C/C++ question about dynamic "static struct" Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-10-27 05:43 -0700
                                                  Re: C/C++ question about dynamic "static struct" Les Cargill <lcargill99@comcast.com> - 2012-10-27 13:52 -0500
                                                  Re: C/C++ question about dynamic "static struct" Les Cargill <lcargill99@comcast.com> - 2012-10-27 14:40 -0500
                                        Re: C/C++ question about dynamic "static struct" google@ianshome.com - 2012-10-23 12:03 -0700
                                Re: C/C++ question about dynamic "static struct" Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-10-27 05:35 -0700
                                  Re: C/C++ question about dynamic "static struct" Les Cargill <lcargill99@comcast.com> - 2012-10-27 14:24 -0500
                                Re: C/C++ question about dynamic "static struct" Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-10-27 05:36 -0700
                            Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-21 07:08 +0000
                              Re: C/C++ question about dynamic "static struct" Les Cargill <lcargill99@comcast.com> - 2012-10-21 02:51 -0500
                                Re: C/C++ question about dynamic "static struct" Ian Collins <ian-news@hotmail.com> - 2012-10-21 21:09 +1300
                                Re: C/C++ question about dynamic "static struct" Rui Maciel <rui.maciel@gmail.com> - 2012-10-21 12:16 +0100
                            Re: C/C++ question about dynamic "static struct" Rui Maciel <rui.maciel@gmail.com> - 2012-10-21 11:55 +0100
                              Re: C/C++ question about dynamic "static struct" Les Cargill <lcargill99@comcast.com> - 2012-10-21 13:47 -0500
                                Re: C/C++ question about dynamic "static struct" Ian Collins <ian-news@hotmail.com> - 2012-10-22 08:46 +1300
                                Re: C/C++ question about dynamic "static struct" Rui Maciel <rui.maciel@gmail.com> - 2012-10-22 03:02 +0100
                                Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-22 08:04 +0000
                              Re: C/C++ question about dynamic "static struct" Tobias Müller <troplin@bluewin.ch> - 2012-10-21 18:54 +0000
                                Re: C/C++ question about dynamic "static struct" Ian Collins <ian-news@hotmail.com> - 2012-10-22 08:49 +1300
                                  Re: C/C++ question about dynamic "static struct" Tobias Müller <troplin@bluewin.ch> - 2012-10-21 20:07 +0000
                                    Re: C/C++ question about dynamic "static struct" Öö Tiib <ootiib@hot.ee> - 2012-10-21 13:53 -0700
                                    Re: C/C++ question about dynamic "static struct" Ian Collins <ian-news@hotmail.com> - 2012-10-22 09:54 +1300
                                    Re: C/C++ question about dynamic "static struct" Rui Maciel <rui.maciel@gmail.com> - 2012-10-22 02:41 +0100
                                      Re: C/C++ question about dynamic "static struct" Tobias Müller <troplin@bluewin.ch> - 2012-10-22 06:12 +0000
                                      Re: C/C++ question about dynamic "static struct" Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-10-22 20:52 +0000
                                        Re: C/C++ question about dynamic "static struct" Öö Tiib <ootiib@hot.ee> - 2012-10-22 16:30 -0700
                                          Re: C/C++ question about dynamic "static struct" Stuart <DerTopper@web.de> - 2012-10-23 11:22 +0200
                                            Re: C/C++ question about dynamic "static struct" Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-10-23 16:58 +0000
                                Re: C/C++ question about dynamic "static struct" Rui Maciel <rui.maciel@gmail.com> - 2012-10-22 02:22 +0100
                            Re: C/C++ question about dynamic "static struct" Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-10-27 05:30 -0700
                        Re: C/C++ question about dynamic "static struct" James Kuyper <jameskuyper@verizon.net> - 2012-10-20 09:06 -0400
                          Re: C/C++ question about dynamic "static struct" Öö Tiib <ootiib@hot.ee> - 2012-10-20 07:43 -0700
                          Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-21 07:11 +0000
                            Re: C/C++ question about dynamic "static struct" Rui Maciel <rui.maciel@gmail.com> - 2012-10-21 11:56 +0100
                            Re: C/C++ question about dynamic "static struct" James Kuyper <jameskuyper@verizon.net> - 2012-10-21 07:43 -0400
                              Re: C/C++ question about dynamic "static struct" Öö Tiib <ootiib@hot.ee> - 2012-10-21 10:36 -0700
                              Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-22 08:13 +0000
                                Re: C/C++ question about dynamic "static struct" Steve Thompson <stevet810@gmail.com> - 2012-10-22 15:08 +0000
                                Re: C/C++ question about dynamic "static struct" James Kuyper <jameskuyper@verizon.net> - 2012-10-22 14:42 -0400
                          Re: C/C++ question about dynamic "static struct" Rui Maciel <rui.maciel@gmail.com> - 2012-10-21 10:12 +0100
                    Re: C/C++ question about dynamic "static struct" Les Cargill <lcargill99@comcast.com> - 2012-10-20 13:24 -0500
                      Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-21 07:16 +0000
                        Re: C/C++ question about dynamic "static struct" Les Cargill <lcargill99@comcast.com> - 2012-10-21 13:57 -0500
                          Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-22 08:17 +0000
                Re: C/C++ question about dynamic "static struct" Fritz Wuehler <fritz@spamexpire-201210.rodent.frell.theremailer.net> - 2012-10-20 22:41 +0200
                  Re: C/C++ question about dynamic "static struct" Dombo <dombo@disposable.invalid> - 2012-10-21 01:04 +0200
                  Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-21 07:23 +0000
              Re: C/C++ question about dynamic "static struct" Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-10-21 19:33 +0000
                Re: C/C++ question about dynamic "static struct" Greg Martin <greg@softsprocket.com> - 2012-10-21 13:20 -0700
                  Re: C/C++ question about dynamic "static struct" Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-10-23 15:44 +0000
                    Re: C/C++ question about dynamic "static struct" Dombo <dombo@disposable.invalid> - 2012-10-23 20:01 +0200
                      Re: C/C++ question about dynamic "static struct" Stuart <DerTopper@web.de> - 2012-10-24 12:01 +0200
                        Re: C/C++ question about dynamic "static struct" Dombo <dombo@disposable.invalid> - 2012-10-24 20:31 +0200
            Re: C/C++ question about dynamic "static struct" Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-10-20 06:13 -0700
              Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-21 07:25 +0000
                Re: C/C++ question about dynamic "static struct" Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-10-27 05:54 -0700
            Re: C/C++ question about dynamic "static struct" Old Wolf <oldwolf@inspire.net.nz> - 2012-11-11 14:58 -0800
              Re: C/C++ question about dynamic "static struct" gof@somewhere.invalid (Adam Wysocki) - 2012-11-12 10:33 +0000
        Re: C/C++ question about dynamic "static struct" Ian Collins <ian-news@hotmail.com> - 2012-10-18 12:19 +1300
      Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-18 07:06 +0000
        Re: C/C++ question about dynamic "static struct" Noob <root@127.0.0.1> - 2012-10-18 11:36 +0200
          Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-19 09:28 +0000
            Re: C/C++ question about dynamic "static struct" Noob <root@127.0.0.1> - 2012-10-19 12:07 +0200
              Re: C/C++ question about dynamic "static struct" gus gassmann <gus@nospam.com> - 2012-10-19 08:03 -0300
                Re: C/C++ question about dynamic "static struct" Fritz Wuehler <fritz@spamexpire-201210.rodent.frell.theremailer.net> - 2012-10-20 07:14 +0200
            Re: C/C++ question about dynamic "static struct" James Kuyper <jameskuyper@verizon.net> - 2012-10-19 06:58 -0400
    Re: C/C++ question about dynamic "static struct" James Kuyper <jameskuyper@verizon.net> - 2012-10-17 08:29 -0400
    Re: C/C++ question about dynamic "static struct" Richard Damon <news.x.richarddamon@xoxy.net> - 2012-10-17 08:35 -0400
      Re: C/C++ question about dynamic "static struct" James Kuyper <jameskuyper@verizon.net> - 2012-10-17 08:49 -0400
      Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-18 07:10 +0000
        Re: C/C++ question about dynamic "static struct" Keith Thompson <kst-u@mib.org> - 2012-10-18 12:11 -0700
          Re: C/C++ question about dynamic "static struct" Juha Nieminen <nospam@thanks.invalid> - 2012-10-19 09:30 +0000
            Re: C/C++ question about dynamic "static struct" Keith Thompson <kst-u@mib.org> - 2012-10-19 03:05 -0700
            Re: C/C++ question about dynamic "static struct" James Kuyper <jameskuyper@verizon.net> - 2012-10-19 07:37 -0400

Page 1 of 8  [1] 2 3 4 5 6 7 8  Next page →


#19026 — C/C++ question about dynamic "static struct"

Fromgus gassmann <gus@nospam.com>
Date2012-10-17 06:49 -0300
SubjectC/C++ question about dynamic "static struct"
Message-ID<k5lv0a$orl$1@Kil-nws-1.UCIS.Dal.Ca>
Hi,

I have a static struct, defined in a C header file as something like

  typedef struct
SomeStruct {	
		char *name;
		int kind;
		int value;
	 }

and I need to supply (in C++) values to an array of this type. I 
understand that I can set *static* values by

static SomeStruct
myStruct[] = {
		{"name1", 1, 17},
		{"name2", 7, 25},
		{"name3", 3,  6},
		{"name4", 0,117}
	      };

However, I need to set dynamic values along these lines (I know that the 
code does not work):

static SomeStruct
myStruct[] = {
		{name[0], kind[0], val[0]},
		{name[1], kind[1], val[1]},
		...	
		{name[n], kind[n], val[n]},
	      };

where both the values and the number of values are only known at 
runtime. I cannot change the definition in the header file; I am not 
even sure that what I want to do is possible. Any ideas?

Thanks

gus

[toc] | [next] | [standalone]


#19027

FromNoob <root@127.0.0.1>
Date2012-10-17 13:08 +0200
Message-ID<k5m3ik$lll$1@dont-email.me>
In reply to#19026
gus gassmann wrote:

First of all, it is not clear what "C/C++" means.
There is C, and there is C++.

> I have a static struct, defined in a C header file as something like
> 
>   typedef struct
> SomeStruct {	
> 		char *name;
> 		int kind;
> 		int value;
> 	 }

Nit: you should copy/paste the EXACT code. The above does not make sense.
(warning: useless storage class specifier in empty declaration)

I think you meant:

  typedef struct { char *name; int kind; int value; } SomeStruct;

which defines an anonymous struct, and defines "SomeStruct"
as a synonym. (AFAIU, this is done "automatically" in C++)

> and I need to supply (in C++) values to an array of this type. I 
> understand that I can set *static* values by
> 
> static SomeStruct
> myStruct[] = {
> 		{"name1", 1, 17},
> 		{"name2", 7, 25},
> 		{"name3", 3,  6},
> 		{"name4", 0,117}
> 	      };
> 
> However, I need to set dynamic values along these lines (I know that the 
> code does not work):
> 
> static SomeStruct
> myStruct[] = {
> 		{name[0], kind[0], val[0]},
> 		{name[1], kind[1], val[1]},
> 		...	
> 		{name[n], kind[n], val[n]},
> 	      };
> 
> where both the values and the number of values are only known at 
> runtime. I cannot change the definition in the header file; I am not 
> even sure that what I want to do is possible. Any ideas?

If the size of the array is not known at compile-time, then you
need dynamic memory allocation.

In C, you could write:

#include <stdlib.h>
typedef struct { char *name; int kind; int value; } SomeStruct;
static SomeStruct *array;
void array_init(int n, char **name, int *kind, int *value)
{
  int i;
  array = malloc(n * sizeof *array);
  for (i = 0; i < n; ++i)
  {
    SomeStruct temp = { name[i], kind[i], value[i] };
    array[i] = temp;
  }
}

In C++, they use new and new[] instead of malloc.

[toc] | [prev] | [next] | [standalone]


#19028

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-10-17 08:19 -0400
Message-ID<k5m7nn$cj5$1@dont-email.me>
In reply to#19027
On 10/17/2012 07:08 AM, Noob wrote:
> gus gassmann wrote:
> 
> First of all, it is not clear what "C/C++" means.
> There is C, and there is C++.

In context, it means mixed-language programming. He has a C header file
declaring a struct type, and an array of that type which will be filled
in by calling a C++ function. The C++ standard defines how mixing C and
C++ works; the C standard does not, so this is fundamentally a C++ question.
-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#19032

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2012-10-17 15:19 +0000
Message-ID<slrnk7tj3g.1d3.grahn+nntp@frailea.sa.invalid>
In reply to#19027
On Wed, 2012-10-17, Noob wrote:
...
> In C++, they use new and new[] instead of malloc.

Actually, we usually use std::vector or other containers, so we don't
have to manage the memory manually.  (It's unclear though if the OP is
allowed to to that or if he needs to keep a raw C array.)

/Jorgen

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .     .
\X/     snipabacken.se>   O  o   .

[toc] | [prev] | [next] | [standalone]


#19034

Fromgus gassmann <gus@nospam.com>
Date2012-10-17 18:47 -0300
Message-ID<k5n916$je0$1@Kil-nws-1.UCIS.Dal.Ca>
In reply to#19027
On 17/10/2012 8:08 AM, Noob wrote:
> gus gassmann wrote:
>
> First of all, it is not clear what "C/C++" means.
> There is C, and there is C++.
>
>> I have a static struct, defined in a C header file as something like
>>
>>    typedef struct
>> SomeStruct {	
>> 		char *name;
>> 		int kind;
>> 		int value;
>> 	 }
>
> Nit: you should copy/paste the EXACT code. The above does not make sense.
> (warning: useless storage class specifier in empty declaration)
>
> I think you meant:
>
>    typedef struct { char *name; int kind; int value; } SomeStruct;
>
> which defines an anonymous struct, and defines "SomeStruct"
> as a synonym. (AFAIU, this is done "automatically" in C++)
>
>> and I need to supply (in C++) values to an array of this type. I
>> understand that I can set *static* values by
>>
>> static SomeStruct
>> myStruct[] = {
>> 		{"name1", 1, 17},
>> 		{"name2", 7, 25},
>> 		{"name3", 3,  6},
>> 		{"name4", 0,117}
>> 	      };
>>
>> However, I need to set dynamic values along these lines (I know that the
>> code does not work):
>>
>> static SomeStruct
>> myStruct[] = {
>> 		{name[0], kind[0], val[0]},
>> 		{name[1], kind[1], val[1]},
>> 		...	
>> 		{name[n], kind[n], val[n]},
>> 	      };
>>
>> where both the values and the number of values are only known at
>> runtime. I cannot change the definition in the header file; I am not
>> even sure that what I want to do is possible. Any ideas?
>
> If the size of the array is not known at compile-time, then you
> need dynamic memory allocation.
>
> In C, you could write:
>
> #include <stdlib.h>
> typedef struct { char *name; int kind; int value; } SomeStruct;
> static SomeStruct *array;
> void array_init(int n, char **name, int *kind, int *value)
> {
>    int i;
>    array = malloc(n * sizeof *array);
>    for (i = 0; i < n; ++i)
>    {
>      SomeStruct temp = { name[i], kind[i], value[i] };
>      array[i] = temp;
>    }
> }
>
> In C++, they use new and new[] instead of malloc.

Thanks to all who responded. As Jorgen Grahn surmised, I am not allowed 
to change the C header file. I like Noob's solution, because it seems 
simple and uses an idiom that I am familiar with. I therefore tried to 
incorporate it into my file, making some adjustments I thought 
necessary, and ran into trouble I cannot fathom. I would be grateful for 
further assistance.

First, the C header file test.h. (I tried to simplify as much as 
possible, while being faithful to the functionality I will need.)

#ifndef test_processed
#define test_processed

  typedef struct
MyStruct {	
		char *name;
		int kind;
		int nextra;
		} MyStruct;

#endif

Then the C++ code.

#include "test.h"
#include <sstream>

std::string get_str();
int         get_dim();
int         get_int();


int main(){
             int     ndim;
             MyStruct *myStruct = 0;

             ndim = get_dim();

             myStruct = new MyStruct[ndim];
             for (int i=0; i < ndim; i++)
             {
                 myStruct[i] = {{(const)(get_str().c_str()), 0, 
get_int()}};
             }
             return 0;
}

std::string get_str()
{
     return "test";
}

int         get_dim()
{
     return 7;
}

int         get_int()
{
     return 2;
}

This gives the following error messages:

test.cpp: In function ‘int main()’:
test.cpp:18:34: error: ISO C++ forbids declaration of ‘type name’ with 
no type
test.cpp:18:58: error: cast from ‘const char*’ to ‘const int’ loses 
precision
test.cpp:18:74: warning: extended initializer lists only available with 
-std=c++0x or -std=gnu++0x
test.cpp:18:74: warning: extended initializer lists only available with 
-std=c++0x or -std=gnu++0x
test.cpp:18:74: error: no match for ‘operator=’ in ‘*(myStruct + ((long 
unsigned int)(((long unsigned int)i) * 16ul))) = {{((const int)((long 
int)get_str()().std::basic_string<_CharT, _Traits, _Alloc>::c_str [with 
_CharT = char, _Traits = std::char_traits<char>, _Alloc = 
std::allocator<char>]())), 0, get_int()}}’
test.h:2:10: note: candidate is: MyStruct& MyStruct::operator=(const 
MyStruct&)

The first two have me confused. (Not that the others are any clearer, 
but there I do not even understand what the error message means.) Isn't 
'name' defined in the header file? And why does the compiler think I am 
trying to cast to 'const int'?
BTW, I first tried

void main()

but the compiler complained that main() must return an integer, so I 
changed it. I thought a void main was the proper thing.

Can anyone help me? (As you see, I no speaka da C++ so good, so please 
be gentle.)

gus

[toc] | [prev] | [next] | [standalone]


#19035

FromIke Naar <ike@iceland.freeshell.org>
Date2012-10-17 22:46 +0000
Message-ID<slrn3vfsk7uda1.307.ike@iceland.freeshell.org>
In reply to#19034
On 2012-10-17, gus gassmann <gus@nospam.com> wrote:
> Thanks to all who responded. As Jorgen Grahn surmised, I am not allowed 
> to change the C header file. I like Noob's solution, because it seems 
> simple and uses an idiom that I am familiar with. I therefore tried to 
> incorporate it into my file, making some adjustments I thought 
> necessary, and ran into trouble I cannot fathom. I would be grateful for 
> further assistance.
>
> First, the C header file test.h. (I tried to simplify as much as 
> possible, while being faithful to the functionality I will need.)
>
> #ifndef test_processed
> #define test_processed
>
>   typedef struct
> MyStruct {	
> 		char *name;
> 		int kind;
> 		int nextra;
> 		} MyStruct;
>
> #endif
>
> Then the C++ code.
>
> #include "test.h"
> #include <sstream>

Nothing from <sstream> is used, so why include it?
Something from <string> is used, so why not include it?

> std::string get_str();
> int         get_dim();
> int         get_int();
>
> int main(){
>              int     ndim;
>              MyStruct *myStruct = 0;
>              ndim = get_dim();
>              myStruct = new MyStruct[ndim];

Get rid of the bogus initializations, and simply write:

        int ndim = get_dim();
        MyStruct *myStruct = new MyStruct[ndim];

>              for (int i=0; i < ndim; i++)
>              {
>                  myStruct[i] = {{(const)(get_str().c_str()), 0, get_int()}};

Here you're casting away const, and that is often a danger sign.
In this case, myStruct[i].name is set to point to a temporary value that
no longer exists after this statement.

>              }
>              return 0;
> }
>
> std::string get_str()
> {
>      return "test";
> }
>
> int         get_dim()
> {
>      return 7;
> }
>
> int         get_int()
> {
>      return 2;
> }
>
> This gives the following error messages:
>
> test.cpp: In function ?int main()?:
> test.cpp:18:34: error: ISO C++ forbids declaration of ?type name? with 
> no type
> test.cpp:18:58: error: cast from ?const char*? to ?const int? loses 
> precision
> test.cpp:18:74: warning: extended initializer lists only available with 
> -std=c++0x or -std=gnu++0x
> test.cpp:18:74: warning: extended initializer lists only available with 
> -std=c++0x or -std=gnu++0x
> test.cpp:18:74: error: no match for ?operator=? in ?*(myStruct + ((long 
> unsigned int)(((long unsigned int)i) * 16ul))) = {{((const int)((long 
> int)get_str()().std::basic_string<_CharT, _Traits, _Alloc>::c_str [with 
> _CharT = char, _Traits = std::char_traits<char>, _Alloc = 
> std::allocator<char>]())), 0, get_int()}}?
> test.h:2:10: note: candidate is: MyStruct& MyStruct::operator=(const 
> MyStruct&)
>
> The first two have me confused. (Not that the others are any clearer, 
> but there I do not even understand what the error message means.) Isn't 
> 'name' defined in the header file? And why does the compiler think I am 
> trying to cast to 'const int'?

It's not clear which line is line 34, but the first error may have to
do with std::string being undeclared because <string> was not included.

> BTW, I first tried
> void main()
>
> but the compiler complained that main() must return an integer, so I 
> changed it. I thought a void main was the proper thing.

It's not. What made you think so?

[toc] | [prev] | [next] | [standalone]


#19040

FromJuha Nieminen <nospam@thanks.invalid>
Date2012-10-18 07:13 +0000
Message-ID<k5oa6j$i1t$3@speranza.aioe.org>
In reply to#19035
In comp.lang.c++ Ike Naar <ike@iceland.freeshell.org> wrote:
> Get rid of the bogus initializations, and simply write:
> 
>        int ndim = get_dim();
>        MyStruct *myStruct = new MyStruct[ndim];

What exactly would be the reason to use 'new' explicitly here rather than,
say, std::vector?

The above code makes the function error-prone and exception-unsafe.

[toc] | [prev] | [next] | [standalone]


#19036

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-10-17 18:58 -0400
Message-ID<507F380B.2060402@verizon.net>
In reply to#19034
On 10/17/2012 05:47 PM, gus gassmann wrote:
...
> First, the C header file test.h. (I tried to simplify as much as 
> possible, while being faithful to the functionality I will need.)
> 
> #ifndef test_processed
> #define test_processed
> 
>   typedef struct
> MyStruct {	
> 		char *name;
> 		int kind;
> 		int nextra;
> 		} MyStruct;
> 
> #endif
> 
> Then the C++ code.
> 
> #include "test.h"
> #include <sstream>
> 
> std::string get_str();
> int         get_dim();
> int         get_int();
> 
> 
> int main(){
>              int     ndim;
>              MyStruct *myStruct = 0;
> 
>              ndim = get_dim();
> 
>              myStruct = new MyStruct[ndim];
>              for (int i=0; i < ndim; i++)
>              {
>                  myStruct[i] = {{(const)(get_str().c_str()), 0, 
> get_int()}};

That (const) is a syntax error. Perhaps you intended
const_cast<char*>(get_str().c_str())? That would, at least, be correct
syntax, and it would disable the warning message you would otherwise get
from assigning a const char* value in a char* object. However, it's not
a good idea to disable that warning message. Your "program shall not
alter any of the values stored in the character array" pointed at by the
pointer returned by c_str() (C++ 21.4.7.1p3), so I would not recommend
storing it in a char* variable. If your code (or any other code you pass
this struct to) makes the mistake of trying to write through
myStruct[i].name, your compiler won't warn you that you're making a
mistake.

That pointer may become invalid if at any time the std::string object
from which it cames gets modified (C++ 21.4.1p6). That's a general
warning that applies to any situation in which you call c_str(), though
it hardly matters in this case. This situation is much worse than that.
That's because the relevant string is the one returned by the call to
get_str(). Since that function returns the string by value, it is a
temporary object, whose lifetime will end as soon as the full-expression
containing that function call is finished being evaluated. In other
words, the pointer myStruct[i].name becomes invalid immediately after
being initialized.

You need to store the string returned by get_str(), allocate sufficient
memory for a copy of the contents of that string, and then copy the
string into that memory. I'm primarily a C programmer, so I may be
missing out some elegant C++ way of doing that, but the following
inelegant (and untested) code should do the job:

                   std::string s = get_str();
                   char *p = new char[s.size()];
                   s.copy(p, s.begin(), s.size());

Since you had to allocate this memory, you should make sure that it gets
deallocated by using the delete operator when you're done with it;
otherwise, you'll have a memory leak.

What Noob suggested corresponds to following this up with the following
code:
	
                   MyStruct temp = {s, 0, get_int()};
                   myStruct[i] = temp;

You tried to simplify by eliminating temp, but that's going too far with
simplification. The whole point of the temporary variable is that
initialization syntax can only be used when defining an object. It can't
be used as the right-hand-side (RHS) of an an assignment expression.

...
> This gives the following error messages:
...
> The first two have me confused. (Not that the others are any clearer, 
> but there I do not even understand what the error message means.) Isn't 
> 'name' defined in the header file? And why does the compiler think I am 
> trying to cast to 'const int'?

It's probably trying to interpret your use of "(const)". I'm not sure
what it made of that syntax error, but there's no point in wondering
about it. Not only is the syntax wrong, but what you were attempting to
do using that syntax was also wrong. Just fix along the lines indicated
above.

> BTW, I first tried
> 
> void main()
> 
> but the compiler complained that main() must return an integer, so I 
> changed it. I thought a void main was the proper thing.

No, int main() is the proper thing; void main() is a popular extension
to both C and C++, but neither language guarantees that it is supported.
On an implementation where that extension is not supported, you'll
probably get a warning message, but in principle your entire program
could malfunction if main() is declared that way.

> Can anyone help me? (As you see, I no speaka da C++ so good, so please 
> be gentle.)

Your code involved several nasty misconceptions. I tried to explain the
problems as precisely as possible; I hope that didn't seem excessively
rough.

[toc] | [prev] | [next] | [standalone]


#19041

FromStuart <DerTopper@web.de>
Date2012-10-18 09:55 +0200
Message-ID<k5ocm1$m8d$1@dont-email.me>
In reply to#19036
On 10/18/12 James Kuyper wrote:
[snip]
> I'm primarily a C programmer, so I may be
> missing out some elegant C++ way of doing that, but the following
> inelegant (and untested) code should do the job:
[snip]

I'm intrigued. I have never ever met someone who described himself as a 
C programmer. May I ask what kind of business you are in? My guess is 
your line of work includes either device drivers, embedded devices, 
kernel code or something else that forces you to write C instead of C++. 
In all these years I have never met someone who used C instead of C++ 
unless he was forced to do so.

@OP: Sorry for interrupting your thread.

Regards,
Stuart

[toc] | [prev] | [next] | [standalone]


#19042

Fromjacob navia <jacob@spamsink.net>
Date2012-10-18 10:13 +0200
Message-ID<k5odnu$rdf$1@speranza.aioe.org>
In reply to#19041
Le 18/10/12 09:55, Stuart a écrit :
> On 10/18/12 James Kuyper wrote:
> [snip]
>> I'm primarily a C programmer, so I may be
>> missing out some elegant C++ way of doing that, but the following
>> inelegant (and untested) code should do the job:
> [snip]
>
> I'm intrigued. I have never ever met someone who described himself as a
> C programmer.

C++ people think that their extremely complex language is the best of 
all worlds. They have so little consideration for other views that
they really think, as you say:


 > I have never met someone who used C instead of C++
 > unless he was forced to do so.

I have never used C++ unless I was forced to.

C++ is a mistake. It has produced a language that nobody understands,
not even Stroustrup himself.


But let's come back to reality. I will not convince you that simplicity
outperforms complexity in software engineering.

So, be assured that C++ is the best invention since sliced bread.

[toc] | [prev] | [next] | [standalone]


#19043

FromJuha Nieminen <nospam@thanks.invalid>
Date2012-10-18 08:23 +0000
Message-ID<k5oeap$sh0$1@speranza.aioe.org>
In reply to#19042
In comp.lang.c++ jacob navia <jacob@spamsink.net> wrote:
> C++ is a mistake. It has produced a language that nobody understands,
> not even Stroustrup himself.

To make such an inflammatory post on a C++ group you are either incredibly
stupid or the worst kind of troll. (And that's an inclusive 'or'.)

[toc] | [prev] | [next] | [standalone]


#19045

Fromjacob navia <jacob@spamsink.net>
Date2012-10-18 11:17 +0200
Message-ID<k5ohed$5p4$1@speranza.aioe.org>
In reply to#19043
Le 18/10/12 10:23, Juha Nieminen a écrit :
> In comp.lang.c++ jacob navia <jacob@spamsink.net> wrote:
>> C++ is a mistake. It has produced a language that nobody understands,
>> not even Stroustrup himself.
>
> To make such an inflammatory post on a C++ group you are either incredibly
> stupid or the worst kind of troll. (And that's an inclusive 'or'.)
>
To say in a C group (comp.lang.c)

 > I have never met someone who used C instead of C++
 > unless he was forced to do so.

is NOT inflammatory, of course.

I did NOT treat anyone of "stupid" or "troll" Mr Nieminen.

Just some facts to put this discussion in a less polemic tone:

1) Mr Stroustrup gave up on "concepts" after several YEARS of work. He 
could not (as he said) understand all the implications that the change 
could have, nor could the people in the standards committee so they just
balked out.

For me, in my stupid little mind, it means that nobody could really 
understand the consequences of a modification of the language and that 
the languagve has gotten too complex for humans to understand. You need 
a machine to understand c++

2) Further proof that C++ is a language humans can't fully understand is 
the fact that the specifications for operator overloading (for instance) 
go for pages and pages of the C++ standard and require in the last 
resort a topological sort of all possible classes involved. This means 
that it can only be done in a moderately complex application by a 
machine (i.e. a compiler) that will do the sort and produce its result 
that can be (maybe) followed by humans in  a super human effort but 
can't be done in a daily basis by the C++ programmers that can only 
GUESS that their code is right.

3) And yet another proof is that when somebody asks something in the
C++ group the answer is in MOST cases

"I compiled it with compiler XXX and the answer was YYY"

and NOT

"The correct answer should be YYY because the standard specifies ..." etc.

People give as a ROUTINE answer the answer the machine proposes them,
not the answer they deduced since understanding a statement can be
an impossible task.

The language has grown SO complex that nobody knows ALL the subtleties 
of it. Most programmers know a small subset that they use (30-40%), and 
ignore the rest. Experts may know 60-70%, but nobody really understands 
it ALL.

Is it necessary for humans to understand completely a machine language?

I do not know. Yes, my message was polemic but it was an ANSWER to 
previous polemic.

Yours sincerely

jacob

[toc] | [prev] | [next] | [standalone]


#19047

FromIan Collins <ian-news@hotmail.com>
Date2012-10-18 23:37 +1300
Message-ID<aea4ftF2v9rU3@mid.individual.net>
In reply to#19045
On 10/18/12 22:17, jacob navia wrote:
>
> The language has grown SO complex that nobody knows ALL the subtleties
> of it. Most programmers know a small subset that they use (30-40%), and
> ignore the rest. Experts may know 60-70%, but nobody really understands
> it ALL.

You never seem to get the point that there is nothing wrong with cherry 
picking language features.  A project team should agree which features 
they want to use and stick with them until they are happy to embrace 
more.  Maybe they never will, that fine, the choice is theirs.  At least 
they are able to make a choice.

Even you recognise some of the benefits C++ can offer by including C++ 
like extensions to your compiler.

You may call be biased, but I was a C programmer for over a decade 
before I learned C++ and two decades later I still work on C as well as 
C++ projects.  But I really miss some C++ features when I'm working in 
C, RAII more than anything else.  I guess its much the same as missing 
the 1st class menu when flying economy!

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#19053

FromRui Maciel <rui.maciel@gmail.com>
Date2012-10-18 15:35 +0100
Message-ID<k5p42l$ltc$1@speranza.aioe.org>
In reply to#19045
jacob navia wrote:

> Le 18/10/12 10:23, Juha Nieminen a écrit :
>> In comp.lang.c++ jacob navia <jacob@spamsink.net> wrote:
>>> C++ is a mistake. It has produced a language that nobody understands,
>>> not even Stroustrup himself.
>>
>> To make such an inflammatory post on a C++ group you are either
>> incredibly stupid or the worst kind of troll. (And that's an inclusive
>> 'or'.)
>>
> To say in a C group (comp.lang.c)
> 
>  > I have never met someone who used C instead of C++
>  > unless he was forced to do so.
> 
> is NOT inflammatory, of course.
> 
> I did NOT treat anyone of "stupid" or "troll" Mr Nieminen.

It is, nevertheless, provocative.  You have to admit that.


> Just some facts to put this discussion in a less polemic tone:
> 
> 1) Mr Stroustrup gave up on "concepts" after several YEARS of work. He
> could not (as he said) understand all the implications that the change
> could have, nor could the people in the standards committee so they just
> balked out.
> 
> For me, in my stupid little mind, it means that nobody could really
> understand the consequences of a modification of the language and that
> the languagve has gotten too complex for humans to understand. You need
> a machine to understand c++

You are referring to a change which, although it was proposed and considered 
by the standards committee, wasn't made a part of the language.  That means 
that you aren't really referring to C++'s complexity as it stands, you are 
referring to the complexity of a specific proposal.  Bolting on a formal 
verification system on a programming language isn't a trivial thing to pull, 
particularly on a metaprogramming system.  Would you claim that nobody 
understood the C programming language if no one succeeds in bolting on a 
formal verification system to it?


> 2) Further proof that C++ is a language humans can't fully understand is
> the fact that the specifications for operator overloading (for instance)
> go for pages and pages of the C++ standard and require in the last
> resort a topological sort of all possible classes involved. This means
> that it can only be done in a moderately complex application by a
> machine (i.e. a compiler) that will do the sort and produce its result
> that can be (maybe) followed by humans in  a super human effort but
> can't be done in a daily basis by the C++ programmers that can only
> GUESS that their code is right.
> 
> 3) And yet another proof is that when somebody asks something in the
> C++ group the answer is in MOST cases
> 
> "I compiled it with compiler XXX and the answer was YYY"
> 
> and NOT
> 
> "The correct answer should be YYY because the standard specifies ..." etc.

That is a silly argument to make.  The C programming language wouldn't 
suddenly become a language that nobody understands if comp.lang.c was 
suddenly flooded by people who answered "I compiled it with compiler X and 
the answer was Y".


> People give as a ROUTINE answer the answer the machine proposes them,
> not the answer they deduced since understanding a statement can be
> an impossible task.
> 
> The language has grown SO complex that nobody knows ALL the subtleties
> of it. Most programmers know a small subset that they use (30-40%), and
> ignore the rest. Experts may know 60-70%, but nobody really understands
> it ALL.

That is irrelevant.  Some C++ projects specifically state in their coding 
guidelines that feature X and Y should not be used, which tend to influence 
specialization.  In addition, certainly you are aware that C++'s support for 
metaprogramming was a fluke, and that no one is really required to fully 
understand every obscure consequence of a fluke to be able to employ the 
programming language efficiently.  Would it be fair if people started to 
make the same claim regarding C if a significant number of C programmers 
weren't aware of Duff's device or every obscure alignment requirement 
imposed by the standard?


> Is it necessary for humans to understand completely a machine language?

No, and the same applies to pretty much any technical field.  For example, 
for ages buildings have been built in spite of the science behind them being 
far from fully understood.


Rui Maciel

[toc] | [prev] | [next] | [standalone]


#19095

FromÖö Tiib <ootiib@hot.ee>
Date2012-10-19 11:40 -0700
Message-ID<7e1d5b9f-51c0-43a8-9519-cb04d0ae9a13@googlegroups.com>
In reply to#19053
On Thursday, 18 October 2012 17:35:06 UTC+3, Rui Maciel  wrote:
> jacob navia wrote:
> 
> > Le 18/10/12 10:23, Juha Nieminen a écrit :
> >> In comp.lang.c++ jacob navia <jacob@spamsink.net> wrote:
> >>> C++ is a mistake. It has produced a language that nobody understands,
> >>> not even Stroustrup himself.
> >>
> >> To make such an inflammatory post on a C++ group you are either
> >> incredibly stupid or the worst kind of troll. (And that's an inclusive
> >> 'or'.)
> >>
> > To say in a C group (comp.lang.c)
> > 
> >  > I have never met someone who used C instead of C++
> >  > unless he was forced to do so.
> > 
> > is NOT inflammatory, of course.
> > 
> > I did NOT treat anyone of "stupid" or "troll" Mr Nieminen.
> 
> It is, nevertheless, provocative.  You have to admit that.

Developers should write in the language that they like. Nobody should write programs in language that they hate. Probably Jakob Navia is forced to use C++ for something against his own will and that makes him always so poisonous.

[toc] | [prev] | [next] | [standalone]


#19044

FromIan Collins <ian-news@hotmail.com>
Date2012-10-18 21:45 +1300
Message-ID<ae9tsrF2v9rU1@mid.individual.net>
In reply to#19042
On 10/18/12 21:13, jacob navia wrote:
>
> But let's come back to reality. I will not convince you that simplicity
> outperforms complexity in software engineering.

The complexity is in the problem.  Whether the burden of implementing 
that complexity is shared with the compiler or shouldered solely by the 
programmer is the  programmer's choice.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#19052

FromRui Maciel <rui.maciel@gmail.com>
Date2012-10-18 15:03 +0100
Message-ID<k5p286$h6k$1@speranza.aioe.org>
In reply to#19042
jacob navia wrote:

> Le 18/10/12 09:55, Stuart a écrit :
>> On 10/18/12 James Kuyper wrote:
>> [snip]
>>> I'm primarily a C programmer, so I may be
>>> missing out some elegant C++ way of doing that, but the following
>>> inelegant (and untested) code should do the job:
>> [snip]
>>
>> I'm intrigued. I have never ever met someone who described himself as a
>> C programmer.
> 
> C++ people think that their extremely complex language is the best of
> all worlds. They have so little consideration for other views that
> they really think, as you say:

I don't believe that anyone can really claim, at least with a straight face, 
to know how everyone who ever learned to program in a specific programming 
language thinks.


>  > I have never met someone who used C instead of C++
>  > unless he was forced to do so.
> 
> I have never used C++ unless I was forced to.
> 
> C++ is a mistake. It has produced a language that nobody understands,
> not even Stroustrup himself.

The C++ language is complex, but your claim that "nobody understands" it is 
based on a definition of "understands" that is a bit distorted and 
inflamatory.

Throughout the years, plenty of complex pieces of software were successfully 
implemented in C++, to the point that it seemed that the entire software 
industry was based on C++.  That wouldn't have happened if no one understood 
the C++ programming language.  So, in this aspect, your claim that "nobody 
understands" C++ is patently false.

But then, although that was what you actually said, that certainly wasn't 
what you meant.  You certainly referred to the difficulty of gaining 
complete insight into how each and every tiny bit of the language works and 
interacts with anything else.  The C++ programming language is complex.  
Yet, you failed to mention that the complexity issue isn't exclusive to C++, 
nor it is a significant limitation.


> But let's come back to reality. I will not convince you that simplicity
> outperforms complexity in software engineering.
> 
> So, be assured that C++ is the best invention since sliced bread.

You are the only person suggesting that claim.


Rui Maciel

[toc] | [prev] | [next] | [standalone]


#19048

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-10-18 07:18 -0400
Message-ID<k5ooif$kff$1@dont-email.me>
In reply to#19041
On 10/18/2012 03:55 AM, Stuart wrote:
> On 10/18/12 James Kuyper wrote:
> [snip]
>> I'm primarily a C programmer, so I may be
>> missing out some elegant C++ way of doing that, but the following
>> inelegant (and untested) code should do the job:
> [snip]
> 
> I'm intrigued. I have never ever met someone who described himself as a 
> C programmer. May I ask what kind of business you are in? My guess is 
> your line of work includes either device drivers, embedded devices, 
> kernel code or something else that forces you to write C instead of C++.

I don't work in any of the areas you listed. I'm a scientific computer
programmer. My programs process data sent down from instruments mounted
on satellites (mainly geolocation and calibration of images). They run
24-7 in a production environment with no user interaction; they just
read and write files. The production system itself is controlled by a
GUI which interacts with the operators, but my programs themselves do not.

> In all these years I have never met someone who used C instead of C++ 
> unless he was forced to do so.

Our contract with our client imposes requirements on our delivered code.
Among other things, the permitted languages are C95, Fortan 77, Fortran
90, or Ada. So that part you got right.

I recently started work on an additional project covered by a different
contract with fewer restrictions, and most of the code is in C++. I've
found my extensive knowledge about C++ more useful than I expected, and
my lack of C++ experience less of a problem than I'd feared.
-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#19055

FromStuart <DerTopper@web.de>
Date2012-10-18 17:43 +0200
Message-ID<k5p82s$i63$1@dont-email.me>
In reply to#19048
Am 10/18/12 1:18 PM, schrieb James Kuyper:
> On 10/18/2012 03:55 AM, Stuart wrote:
>> On 10/18/12 James Kuyper wrote:
>> [snip]
>>> I'm primarily a C programmer, so I may be
>>> missing out some elegant C++ way of doing that, but the following
>>> inelegant (and untested) code should do the job:
>> [snip]
>>
>> I'm intrigued. I have never ever met someone who described himself as a
>> C programmer. May I ask what kind of business you are in? My guess is
>> your line of work includes either device drivers, embedded devices,
>> kernel code or something else that forces you to write C instead of C++.
>
> I don't work in any of the areas you listed. I'm a scientific computer
> programmer. My programs process data sent down from instruments mounted
> on satellites (mainly geolocation and calibration of images). They run
> 24-7 in a production environment with no user interaction; they just
> read and write files. The production system itself is controlled by a
> GUI which interacts with the operators, but my programs themselves do not.
>
>> In all these years I have never met someone who used C instead of C++
>> unless he was forced to do so.
>
> Our contract with our client imposes requirements on our delivered code.
> Among other things, the permitted languages are C95, Fortan 77, Fortran
> 90, or Ada. So that part you got right.


Then I would have chosen Ada without a moments hesitation. I fell in 
love with that language right away, and I believe that Ada compilers and 
C++ compilers are equally complicated. It's a pity that Ada did not gain 
the popularity of C++ but that is probably due to the fact that Ada came 
too late (I know that C++ emerged later than Ada, but by that time 
probably too many people were using C already, and the transition from C 
to C++ is more "natural" than the transition from C to Ada, I guess).


> I recently started work on an additional project covered by a different
> contract with fewer restrictions, and most of the code is in C++. I've
> found my extensive knowledge about C++ more useful than I expected, and
> my lack of C++ experience less of a problem than I'd feared.

When I started programming with C++ I did not want to learn too much, 
especially not that nifty template stuff. My coding style in those days 
can best be described as "C-ish": I wrote the usual procedural code, but 
instead of messing around with char* pointers and strcpy, I used 
std::string (I only realized much later that this is already a templated 
class).

Later I started to write my own classes and was thrilled. OOP! My man, 
this rocked.

On day I stumbled upon a loop that ran really slow: It iterated over the 
pixels of an image. Since my image could be either 8bits or 16bits so I 
had to provide a virtual getPixel method. This was particularly clumsy: 
My algorithm had to invoke a virtual method million of times only to 
discover that it actually invoked the _same_ method each time. It would 
be much better if I could have two versions of the same algorithm for 
two different data types, thus avoiding the million virtual method 
invokations.

It turned out that all I had to do was to make it a template function. 
Easy, peasy, japanesey.

That's how it all started with this template stuff. Ten years later I 
was writing code like this

   template<class t_BaseImpl, class t_CoClass = t_BaseImpl::CoClassType>
   class HAirCushionSwitchHelper : public t_BaseImpl
   {
      ...

even before I had my morning coffee.

Admittedly, I have not had the guts to delve deeper into template 
metaprogramming, but one should never say never. That's what I like 
about C++: There is always a new continent to discover, but only when 
you need it.

Regards,
Stuart

[toc] | [prev] | [next] | [standalone]


#19062

FromWilliam Ahern <william@wilbur.25thandClement.com>
Date2012-10-18 13:52 -0700
Message-ID<8q66l9-lkm.ln1@wilbur.25thandClement.com>
In reply to#19041
In comp.lang.c Stuart <DerTopper@web.de> wrote:
> On 10/18/12 James Kuyper wrote:
> [snip]
> > I'm primarily a C programmer, so I may be
> > missing out some elegant C++ way of doing that, but the following
> > inelegant (and untested) code should do the job:
> [snip]

> I'm intrigued. I have never ever met someone who described himself as a C
> programmer. May I ask what kind of business you are in? My guess is your
> line of work includes either device drivers, embedded devices, kernel code
> or something else that forces you to write C instead of C++. In all these
> years I have never met someone who used C instead of C++ unless he was
> forced to do so.

Well, you have now. I've worked in several shops (both small and billion
dollar companies) which were C only by choice. The consensus was that the
cost/benefit of C++ didn't pan out, particularly for backend, non-GUI work.
When you're writing high availability server software, the fewer moving
parts the better.

C++ can be too big. You can certainly cherry pick features, but different
people cherry pick different features. And you also need someone who
understands _all_ the features to go through and grok those used and unused.
Plus, there's linking, interoperability, etc, which is more of a pain. And
C++'s strict typing can lead to code bloat and/or excessive casting.

There are lots of cool features in C++, but often those features are better
implemented in other languages. It can be easier to use C + [insert other
language], then just C++. So, depending on your perspective, C++'s winning
features can seem middling to someone willing to use more than a single
language.

I'm guessing that you come from a Windows background and/or graduated
college in the past 3 or 4 years. That's usually how it goes, IME. C++
didn't make waves in the Unix world until a few years ago, and then it took
off with Google and Apple**, especially when they picked up their college
recruitment. C++ grew up in Microsoft-land and in Microsoft-friendly
universities which dumped SUN--or never had the history to begin with. Which
isn't a slight, just an observation about the peculiarities of C++ culture.


** I can't even use Valgrind on OS X Mountain Lion because some enterprising
kid over at Apple apparently decided to rewrite the linker, allocator, and
other bits of the runtime in C++, which throws up screenfuls of errors.
Apparently when you're doing so much dynamic allocation who cares about a
few lost objects here-or-there.

[toc] | [prev] | [next] | [standalone]


Page 1 of 8  [1] 2 3 4 5 6 7 8  Next page →

Back to top | Article view | comp.lang.c++


csiph-web