473,386 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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;
}

May 23 '06 #1
2 1701

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: PengHQ | last post by:
int compare(const void *x,const void *y) { strcmp(*(char **)x,*(char **)y); } why not code like these: int compare(const void *x,const void *y) { strcmp(x,y); }
4
by: S Kemplay | last post by:
Hi all, I am after any pointers you may have to improve these two functions. They both work but are part of an assignment - I am looking for feedback. The first one reads records from a file...
15
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I'm trying to initialize an array of error messages, so that I can print out an error message by using the 'nth string in an array, e.g. printf("%s\n", messages); I'm still hazy on arrays of...
12
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
8
by: Duncan Winn | last post by:
I am new to VC++7. I am using a method GetPrivateProfileString that requires an LPTSTR. I have defined this as a: char * data_name; I am then trying to convert this to an LPOLESTR and I...
5
by: Stephen Cawood | last post by:
I'm trying to use a C++ .lib from C# (I tried the Interop group will no results). I have a working wrapper DLL (I can get back simple things like int), but I'm having issues dealing with an array...
3
by: ZMan | last post by:
The following code won't compile with gcc version 3.4.2 (mingw-special). How come? Error: cannot convert `char (*)' to `char**' /**********************************************************/...
4
by: alisbub | last post by:
1. Write an interactive program that asks the user to input the length and width of a rectangular lawn. The dimensions should be in yards. Your program should compute the area of the lawn in square...
34
by: arnuld | last post by:
what is the difference between these 2: char name = "hackers"; char* name = "hackers";
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.