Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C/C++ question about dynamic "static struct"

Reply
Thread Tools

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

 
 
Ian Collins
Guest
Posts: n/a
 
      10-17-2012
On 10/18/12 10:47, gus gassmann wrote:
>
> myStruct = new MyStruct[ndim];
> for (int i=0; i< ndim; i++)
> {
> myStruct[i] = {{(const)(get_str().c_str()), 0,
> get_int()}};
> }


As others have correctly pointed out, the mistyped cast from std::string
will end in tears. You need to convert the string to an char array and
keep track of that data for later disposal.

Something like this my help:

struct FromString
{
static std::vector<char*> stash;

char *p;

FromString( const std::string& s )
: p(new char[s.size()])
{
s.copy(p, s.size());
stash.push_back(p);
}

operator char*() const { return p; }
};

std::vector<char*> FromString::stash; // *free these later!!*

Then you could write:

for (int i=0; i < ndim; i++)
{
MyStruct temp = {FromString(get_str()), 0, get_int()};
myStruct[i] = temp;
}

Note I kept the temporary variable to avoid the use C++11 extended
initialiser lists.

--
Ian Collins
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      10-18-2012
In comp.lang.c++ Noob <root@127.0.0.1> wrote:
> If the size of the array is not known at compile-time, then you
> need dynamic memory allocation.


Incorrect. In C you can use a variable-length array, such as:

void foo(int n)
{
SomeStruct s[n];
...
}

However, that's probably not what the OP meant because he explicitly
wanted to use an initializer list to initialize the array. Therefore
he probably didn't mean to imply that the size of the array is only
known at runtime.
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      10-18-2012
In comp.lang.c++ Richard Damon <(E-Mail Removed)> wrote:
> If its size is not know at compile time, then its memory allocation must
> be dynamic. (In C99, but not C++ except as an extension, it could also
> be of storage type auto).


I think you are confusing variable-length arrays (in C) with the keyword
'auto', which has absolutely nothing to do with that (in either language).

(Btw, and I'm not talking about you specifically, but is it just me or
are most of the answers to the OP's question given in this thread
unusually vague, incorrect and outright incompetent?)
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      10-18-2012
In comp.lang.c++ Ike Naar <(E-Mail Removed)> 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.
 
Reply With Quote
 
Stuart
Guest
Posts: n/a
 
      10-18-2012
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

 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      10-18-2012
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.


 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      10-18-2012
In comp.lang.c++ jacob navia <(E-Mail Removed)> 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'.)
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-18-2012
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
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      10-18-2012
Le 18/10/12 10:23, Juha Nieminen a écrit :
> In comp.lang.c++ jacob navia <(E-Mail Removed)> 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

 
Reply With Quote
 
Noob
Guest
Posts: n/a
 
      10-18-2012
Juha Nieminen wrote:

> In comp.lang.c++ Noob wrote:
>
>> If the size of the array is not known at compile-time, then you
>> need dynamic memory allocation.

>
> Incorrect. In C you can use a variable-length array, such as:
>
> void foo(int n)
> {
> SomeStruct s[n];
> ...
> }


VLAs have automatic storage duration. They are therefore not appropriate
for the OP's purpose.

> However, that's probably not what the OP meant because he explicitly
> wanted to use an initializer list to initialize the array. Therefore
> he probably didn't mean to imply that the size of the array is only
> known at runtime.


Right, except for the fact that he wrote:

"I need to set dynamic values [...] where both the values and the number
of values are only known at runtime."

 
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
Array question - How dynamic is dynamic? Ruby Student Ruby 4 04-09-2009 12:59 PM
Dynamic control on aspx page, dynamic location Chris Thunell ASP .Net 3 07-21-2004 04:52 PM
VPN between 2 Cisco routers (1 static, 1 dynamic) with access from stat --> dynamic over ISDN Hans-Peter Walter Cisco 3 01-21-2004 02:12 PM
Does Pix or cisco router support dynamic-to-dynamic IPSec VPN? c Cisco 2 01-13-2004 01:53 AM
Re: Dynamic Table with Dynamic LinkButtons Rick Glos ASP .Net 0 07-08-2003 01:09 PM



Advertisments