By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,571 Members | 994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,571 IT Pros & Developers. It's quick & easy.

how dodgy are these examples of char array use

P: n/a
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;
}

May 23 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"voidtwerp" <vo*******@gmail.com> wrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
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?";
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.
throw 1;
break;
case 1:
p=f2();
Same thing here, it's just pointing to a constant character array defined in
f2()
throw 2;
break;
case 0:
f3();
This really doesn't do anything. p never gets changed since f3 assigns a
local varaible then throws.
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;
}

May 23 '06 #2

P: n/a
In article <11**********************@38g2000cwa.googlegroups. com>,
"voidtwerp" <vo*******@gmail.com> wrote:
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).
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.)
#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;
}

May 23 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.