Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old May 23rd, 2006, 10:45 AM
voidtwerp
Guest
 
Posts: n/a
Default how dodgy are these examples of char array use

Hi,

showing my extreme ignorance I would like comments how the char arrays
are used here, ie are these valid or dangerous uses (the reason I ask
is because constructs like these occur in some code I am looking at).

#include <iostream>
#include <stdlib.h>

using namespace std;

class C
{
public:
C();
void f1();
private:
char* f2();
void f3();
};


C::C(){}
void C::f1()
{
char * p;
int i=3;
while(i--)
{
try
{
switch (i)
{
case 2:
p="is this dodgy?";
throw 1;
break;
case 1:
p=f2();
throw 2;
break;
case 0:
f3();
break;
default:
cout << "ERK!" << endl;
}
}
catch(char * v)
{
cout << v << endl;
}
catch(int e)
{
cout << p << endl;
}
catch(...)
{
cout << "ERK2!!" << endl;
}
}
}

char* C::f2()
{
char * v = "how dodgy is this?";
return v;
}

void C::f3()
{
char *v="is this any better/worse?";
throw v;
}

int main(int argc, char *argv[])
{
C c;
c.f1();
system("PAUSE");
return 0;
}

  #2  
Old May 23rd, 2006, 11:05 AM
Jim Langston
Guest
 
Posts: n/a
Default Re: how dodgy are these examples of char array use


"voidtwerp" <voidtwerp@gmail.com> wrote in message
news:1148377618.436559.241610@38g2000cwa.googlegro ups.com...[color=blue]
> Hi,
>
> showing my extreme ignorance I would like comments how the char arrays
> are used here, ie are these valid or dangerous uses (the reason I ask
> is because constructs like these occur in some code I am looking at).
>
> #include <iostream>
> #include <stdlib.h>
>
> using namespace std;
>
> class C
> {
> public:
> C();
> void f1();
> private:
> char* f2();
> void f3();
> };
>
>
> C::C(){}
> void C::f1()
> {
> char * p;
> int i=3;
> while(i--)
> {
> try
> {
> switch (i)
> {
> case 2:
> p="is this dodgy?";[/color]

p is pointing to a constant character array. As long as you don't try to
change it or delete it, it should be okay.
[color=blue]
> throw 1;
> break;
> case 1:
> p=f2();[/color]

Same thing here, it's just pointing to a constant character array defined in
f2()
[color=blue]
> throw 2;
> break;
> case 0:
> f3();[/color]

This really doesn't do anything. p never gets changed since f3 assigns a
local varaible then throws.
[color=blue]
> break;
> default:
> cout << "ERK!" << endl;
> }
> }
> catch(char * v)
> {
> cout << v << endl;
> }
> catch(int e)
> {
> cout << p << endl;
> }
> catch(...)
> {
> cout << "ERK2!!" << endl;
> }
> }
> }
>
> char* C::f2()
> {
> char * v = "how dodgy is this?";
> return v;
> }
>
> void C::f3()
> {
> char *v="is this any better/worse?";
> throw v;
> }
>
> int main(int argc, char *argv[])
> {
> C c;
> c.f1();
> system("PAUSE");
> return 0;
> }
>[/color]


  #3  
Old May 23rd, 2006, 11:55 AM
Daniel T.
Guest
 
Posts: n/a
Default Re: how dodgy are these examples of char array use

In article <1148377618.436559.241610@38g2000cwa.googlegroups. com>,
"voidtwerp" <voidtwerp@gmail.com> wrote:
[color=blue]
> showing my extreme ignorance I would like comments how the char arrays
> are used here, ie are these valid or dangerous uses (the reason I ask
> is because constructs like these occur in some code I am looking at).[/color]

All three are somewhat "dodgy" because 'p' is a char* when it should be
a const char*. Also, the exceptions are quite "dodgy" IMO unless they
denote actual error conditions (ie something you couldn't have checked
beforehand.)
[color=blue]
> #include <iostream>
> #include <stdlib.h>
>
> using namespace std;
>
> class C
> {
> public:
> C();
> void f1();
> private:
> char* f2();
> void f3();
> };
>
>
> C::C(){}
> void C::f1()
> {
> char * p;
> int i=3;
> while(i--)
> {
> try
> {
> switch (i)
> {
> case 2:
> p="is this dodgy?";
> throw 1;
> break;
> case 1:
> p=f2();
> throw 2;
> break;
> case 0:
> f3();
> break;
> default:
> cout << "ERK!" << endl;
> }
> }
> catch(char * v)
> {
> cout << v << endl;
> }
> catch(int e)
> {
> cout << p << endl;
> }
> catch(...)
> {
> cout << "ERK2!!" << endl;
> }
> }
> }
>
> char* C::f2()
> {
> char * v = "how dodgy is this?";
> return v;
> }
>
> void C::f3()
> {
> char *v="is this any better/worse?";
> throw v;
> }
>
> int main(int argc, char *argv[])
> {
> C c;
> c.f1();
> system("PAUSE");
> return 0;
> }[/color]
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.