473,386 Members | 1,720 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.

possible seg fault

The following program compiles successfully on a Microsoft Visual C++
6.0 compiler but when I execute it..it encounters an unknown
error..may be a because of a seg fault..

#include <string.h>
#include <iostream.h>

int main(int argc, char **argv){
int i;
int len;
char *addressLine[8];

addressLine[1] = "String1";
addressLine[2] = "String2";

for(i = 1; i <= 8; i++){
len = strlen(addressLine[i]);
if (len >= 1){
cout << "Address: " << addressLine[i];
cout << "\n";
}
}
return (0);
}

but when I just use the following without the loop..

cout << "Address: " << addressLine[1];

it just works fine...

Please help!...

Sep 16 '07 #1
11 1176
jh*******@fastermail.com a écrit :
The following program compiles successfully on a Microsoft Visual C++
6.0 compiler but when I execute it..it encounters an unknown
error..may be a because of a seg fault..

#include <string.h>
#include <iostream.h>

int main(int argc, char **argv){
int i;
int len;
char *addressLine[8];

addressLine[1] = "String1";
addressLine[2] = "String2";

for(i = 1; i <= 8; i++){
len = strlen(addressLine[i]);
if (len >= 1){
cout << "Address: " << addressLine[i];
cout << "\n";
}
}
return (0);
}

but when I just use the following without the loop..

cout << "Address: " << addressLine[1];

it just works fine...

Please help!...
You did not initialize entries 3..8 of the addressLine array... their
content is pure garbage...
Sep 16 '07 #2
On Sep 16, 5:27 pm, "Laurent D.A.M. MENTEN"
<laurent.men...@teledisnet.bewrote:
jhagen...@fastermail.com a écrit :


The following program compiles successfully on a Microsoft Visual C++
6.0 compiler but when I execute it..it encounters an unknown
error..may be a because of a seg fault..
#include <string.h>
#include <iostream.h>
int main(int argc, char **argv){
int i;
int len;
char *addressLine[8];
addressLine[1] = "String1";
addressLine[2] = "String2";
for(i = 1; i <= 8; i++){
len = strlen(addressLine[i]);
if (len >= 1){
cout << "Address: " << addressLine[i];
cout << "\n";
}
}
return (0);
}
but when I just use the following without the loop..
cout << "Address: " << addressLine[1];
it just works fine...
Please help!...

You did not initialize entries 3..8 of the addressLine array... their
content is pure garbage...- Hide quoted text -

- Show quoted text -
Thanks! for ur reply..I need to use only 2 entries in the array...How
do I iterate and print by handling the error at the same time..I tried
to use

if(!addressLine[i]){
//Print value..
}

but same problem..

Sep 16 '07 #3
LR
Laurent D.A.M. MENTEN wrote:
jh*******@fastermail.com a écrit :
>The following program compiles successfully on a Microsoft Visual C++
6.0 compiler but when I execute it..it encounters an unknown
error..may be a because of a seg fault..

#include <string.h>
#include <iostream.h>

int main(int argc, char **argv){
int i;
int len;
char *addressLine[8];

addressLine[1] = "String1";
addressLine[2] = "String2";

for(i = 1; i <= 8; i++){
len = strlen(addressLine[i]);
if (len >= 1){
cout << "Address: " << addressLine[i];
cout << "\n";
}
}
return (0);
}

but when I just use the following without the loop..

cout << "Address: " << addressLine[1];

it just works fine...

Please help!...

You did not initialize entries 3..8 of the addressLine array... their
content is pure garbage...
My understanding is that an array of [8] would have valid indicies from
0..7, and that in c++ array indicies start at zero, an array with n
elements will have indicies from 0..n-1. So I think he didn't initialize
element zero and elements 3..7.

Check the loop again.

You may also want to look at std::string.

LR
Sep 16 '07 #4
On Sep 16, 5:38 pm, jhagen...@fastermail.com wrote:
On Sep 16, 5:27 pm, "Laurent D.A.M. MENTEN"

<laurent.men...@teledisnet.bewrote:
jhagen...@fastermail.com a écrit :
The following program compiles successfully on a Microsoft Visual C++
6.0 compiler but when I execute it..it encounters an unknown
error..may be a because of a seg fault..
#include <string.h>
#include <iostream.h>
int main(int argc, char **argv){
int i;
int len;
char *addressLine[8];
addressLine[1] = "String1";
addressLine[2] = "String2";
for(i = 1; i <= 8; i++){
len = strlen(addressLine[i]);
if (len >= 1){
cout << "Address: " << addressLine[i];
cout << "\n";
}
}
return (0);
}
but when I just use the following without the loop..
cout << "Address: " << addressLine[1];
it just works fine...
Please help!...
You did not initialize entries 3..8 of the addressLine array... their
content is pure garbage...- Hide quoted text -
- Show quoted text -

Thanks! for ur reply..I need to use only 2 entries in the array...How
do I iterate and print by handling the error at the same time..I tried
to use

if(!addressLine[i]){
//Print value..
}

but same problem..- Hide quoted text -

- Show quoted text -
I am sorry I meant..

if(addressLine[i]){
//Print value..
}

Sep 16 '07 #5

<jh*******@fastermail.comwrote in message
news:11**********************@r29g2000hsg.googlegr oups.com...
On Sep 16, 5:38 pm, jhagen...@fastermail.com wrote:
On Sep 16, 5:27 pm, "Laurent D.A.M. MENTEN"

<laurent.men...@teledisnet.bewrote:
jhagen...@fastermail.com a écrit :
The following program compiles successfully on a Microsoft Visual C++
6.0 compiler but when I execute it..it encounters an unknown
error..may be a because of a seg fault..
#include <string.h>
#include <iostream.h>
int main(int argc, char **argv){
int i;
int len;
char *addressLine[8];
addressLine[1] = "String1";
addressLine[2] = "String2";
for(i = 1; i <= 8; i++){
len = strlen(addressLine[i]);
if (len >= 1){
cout << "Address: " << addressLine[i];
cout << "\n";
}
}
return (0);
}
but when I just use the following without the loop..
cout << "Address: " << addressLine[1];
it just works fine...
Please help!...
You did not initialize entries 3..8 of the addressLine array... their
content is pure garbage...- Hide quoted text -
- Show quoted text -

Thanks! for ur reply..I need to use only 2 entries in the array...How
do I iterate and print by handling the error at the same time..I tried
to use

if(!addressLine[i]){
//Print value..
}

but same problem..- Hide quoted text -

- Show quoted text -
>I am sorry I meant..
if(addressLine[i]){
//Print value..
}

addressLine[0] = "String1";
addressLine[1] = "String2";
addressLine[2] = NULL;

....

for (i = 0; i < 8; i++)
Sep 16 '07 #6
LR a écrit :
Laurent D.A.M. MENTEN wrote:
>jh*******@fastermail.com a écrit :
>>The following program compiles successfully on a Microsoft Visual C++
6.0 compiler but when I execute it..it encounters an unknown
error..may be a because of a seg fault..

#include <string.h>
#include <iostream.h>

int main(int argc, char **argv){
int i;
int len;
char *addressLine[8];

addressLine[1] = "String1";
addressLine[2] = "String2";

for(i = 1; i <= 8; i++){
len = strlen(addressLine[i]);
if (len >= 1){
cout << "Address: " << addressLine[i];
cout << "\n";
}
}
return (0);
}

but when I just use the following without the loop..

cout << "Address: " << addressLine[1];

it just works fine...

Please help!...

You did not initialize entries 3..8 of the addressLine array... their
content is pure garbage...

My understanding is that an array of [8] would have valid indicies from
0..7, and that in c++ array indicies start at zero, an array with n
elements will have indicies from 0..n-1. So I think he didn't initialize
element zero and elements 3..7.

Check the loop again.

You may also want to look at std::string.

LR
yep, that's true...
Sep 16 '07 #7
On Sep 16, 6:04 pm, "Laurent D.A.M. MENTEN"
<laurent.men...@teledisnet.bewrote:
LR a écrit :


Laurent D.A.M. MENTEN wrote:
jhagen...@fastermail.com a écrit :
The following program compiles successfully on a Microsoft Visual C++
6.0 compiler but when I execute it..it encounters an unknown
error..may be a because of a seg fault..
>#include <string.h>
#include <iostream.h>
>int main(int argc, char **argv){
int i;
int len;
char *addressLine[8];
> addressLine[1] = "String1";
addressLine[2] = "String2";
> for(i = 1; i <= 8; i++){
len = strlen(addressLine[i]);
if (len >= 1){
cout << "Address: " << addressLine[i];
cout << "\n";
}
}
return (0);
}
>but when I just use the following without the loop..
>cout << "Address: " << addressLine[1];
>it just works fine...
>Please help!...
You did not initialize entries 3..8 of the addressLine array... their
content is pure garbage...
My understanding is that an array of [8] would have valid indicies from
0..7, and that in c++ array indicies start at zero, an array with n
elements will have indicies from 0..n-1. So I think he didn't initialize
element zero and elements 3..7.
Check the loop again.
You may also want to look at std::string.
LR

yep, that's true...- Hide quoted text -

- Show quoted text -
That worked by setting rest to NULL...Thanks!!

Sep 16 '07 #8
runner wrote:
>
<jh*******@fastermail.comwrote in message
news:11**********************@r29g2000hsg.googlegr oups.com...
On Sep 16, 5:38 pm, jhagen...@fastermail.com wrote:
>On Sep 16, 5:27 pm, "Laurent D.A.M. MENTEN"

<laurent.men...@teledisnet.bewrote:
jhagen...@fastermail.com a écrit :
The following program compiles successfully on a Microsoft Visual C++
6.0 compiler but when I execute it..it encounters an unknown
error..may be a because of a seg fault..
#include <string.h>
#include <iostream.h>
int main(int argc, char **argv){
int i;
int len;
char *addressLine[8];
addressLine[1] = "String1";
addressLine[2] = "String2";
for(i = 1; i <= 8; i++){
len = strlen(addressLine[i]);
if (len >= 1){
cout << "Address: " << addressLine[i];
cout << "\n";
}
}
return (0);
}
but when I just use the following without the loop..
cout << "Address: " << addressLine[1];
it just works fine...
Please help!...
You did not initialize entries 3..8 of the addressLine array... their
content is pure garbage...- Hide quoted text -
- Show quoted text -

Thanks! for ur reply..I need to use only 2 entries in the array...How
do I iterate and print by handling the error at the same time..I tried
to use

if(!addressLine[i]){
//Print value..
}

but same problem..- Hide quoted text -

- Show quoted text -
>>I am sorry I meant..
> if(addressLine[i]){
//Print value..
}


addressLine[0] = "String1";
addressLine[1] = "String2";
addressLine[2] = NULL;

...

for (i = 0; i < 8; i++)
Did you mean:

for ( i = 0; addressLine[i] != 0; ++i )

or:

for ( i = 0; i < 2; ++i )

But why is 8 used in the first place if only two entries are needed?
Best

Kai-Uwe Bux

Sep 16 '07 #9
jh*******@fastermail.com wrote:
The following program compiles successfully on a Microsoft Visual C++
6.0 compiler but when I execute it..it encounters an unknown
error..may be a because of a seg fault..

#include <string.h>
#include <iostream.h>

int main(int argc, char **argv){
int i;
int len;
char *addressLine[8];

addressLine[1] = "String1";
addressLine[2] = "String2";

for(i = 1; i <= 8; i++){
len = strlen(addressLine[i]);
if (len >= 1){
cout << "Address: " << addressLine[i];
cout << "\n";
}
}
return (0);
}

1. C++ arrays go from 0 to N-1, so addressLine[8] doesn't exist.
2. You never initialize addressLine[0] or addressLine[2 through 7].
3. iostream.h is non-standard, use <iostreamand std::cout
4. You never use string.h. Don't include it. Also, use <cstring>
instead of string.h.

Sep 16 '07 #10

"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:fc**********@murdoch.acc.Virginia.EDU...
runner wrote:

<jh*******@fastermail.comwrote in message
news:11**********************@r29g2000hsg.googlegr oups.com...
On Sep 16, 5:38 pm, jhagen...@fastermail.com wrote:
On Sep 16, 5:27 pm, "Laurent D.A.M. MENTEN"

<laurent.men...@teledisnet.bewrote:
jhagen...@fastermail.com a écrit :

The following program compiles successfully on a Microsoft Visual
C++
6.0 compiler but when I execute it..it encounters an unknown
error..may be a because of a seg fault..

#include <string.h>
#include <iostream.h>

int main(int argc, char **argv){
int i;
int len;
char *addressLine[8];

addressLine[1] = "String1";
addressLine[2] = "String2";

for(i = 1; i <= 8; i++){
len = strlen(addressLine[i]);
if (len >= 1){
cout << "Address: " << addressLine[i];
cout << "\n";
}
}
return (0);
}

but when I just use the following without the loop..

cout << "Address: " << addressLine[1];

it just works fine...

Please help!...

You did not initialize entries 3..8 of the addressLine array... their
content is pure garbage...- Hide quoted text -

- Show quoted text -

Thanks! for ur reply..I need to use only 2 entries in the array...How
do I iterate and print by handling the error at the same time..I tried
to use

if(!addressLine[i]){
//Print value..
}

but same problem..- Hide quoted text -

- Show quoted text -
>I am sorry I meant..
if(addressLine[i]){
//Print value..
}

addressLine[0] = "String1";
addressLine[1] = "String2";
addressLine[2] = NULL;

...

for (i = 0; i < 8; i++)

Did you mean:

for ( i = 0; addressLine[i] != 0; ++i )

or:

for ( i = 0; i < 2; ++i )

But why is 8 used in the first place if only two entries are needed?
Best

Kai-Uwe Bux

1)

I did mean

addressLine[0] = "String1";
addressLine[1] = "String2";
addressLine[2] = NULL;
....
addressLine[7] = NULL;

for (i = 0; i < 8; i++)
if(addressLine[i])
{
//Print value..
}

to show the OP that the array index starts from 0
and that

for (i = 1; i <= 8; i++)

would be wrong anyway.

2)

that said, i would go for

for ( i = 0; addressLine[i]; i++ )

which is more efficient and general.

3)

I wouldn't use

for ( i = 0; i < 2; i++ )

because it's not "easily" reusable due to the
hard-coded number of iterations.

Sep 17 '07 #11
On Sep 17, 12:52 am, "runner" <emb@ddedwrote:
<jhagen...@fastermail.comwrote in message
news:11**********************@r29g2000hsg.googlegr oups.com...
On Sep 16, 5:38 pm, jhagen...@fastermail.com wrote:
On Sep 16, 5:27 pm, "Laurent D.A.M. MENTEN"
<laurent.men...@teledisnet.bewrote:
jhagen...@fastermail.com a écrit :
The following program compiles successfully on a
Microsoft Visual C++ 6.0 compiler but when I execute
it..it encounters an unknown error..may be a because of
a seg fault..
#include <string.h>
#include <iostream.h>
int main(int argc, char **argv){
int i;
int len;
char *addressLine[8];
addressLine[1] = "String1";
addressLine[2] = "String2";
for(i = 1; i <= 8; i++){
len = strlen(addressLine[i]);
if (len >= 1){
cout << "Address: " << addressLine[i];
cout << "\n";
}
}
return (0);
}
but when I just use the following without the loop..
cout << "Address: " << addressLine[1];
it just works fine...
You did not initialize entries 3..8 of the addressLine array... their
content is pure garbage...- Hide quoted text -
Thanks! for ur reply..I need to use only 2 entries in the array...How
do I iterate and print by handling the error at the same time..I tried
to use
if(!addressLine[i]){
//Print value..
}
but same problem.
I am sorry I meant..
if(addressLine[i]){
//Print value..
}
addressLine[0] = "String1";
addressLine[1] = "String2";
addressLine[2] = NULL;
First, of course, the obvious solution is to use std::vector<
std::string >; std::vector can be adjusted to the required size.
But given what he's got, he really should write:

char const* addressLine[ 8 ] = { "String1", "String2 } ;

Never, never leave a pointer uninitialized.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 17 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
0
by: Matt S | last post by:
Hello, I'm trying to build a C# client to consume an AXIS Web Service (running SOAP over HTTP). The Web Service encodes full server-side exception traces in the Soap Fault > Detail element...
0
by: Jaime Stuardo | last post by:
Hi all.... When I explicitly throw an exception from my Web Service, this XML is sent: <?xml version="1.0" encoding="utf-8"?><soap:Envelope...
3
by: Moshe Kravchik | last post by:
Hi! We have a Web Service written in ATL Server and a client written in Java using Axis. When something goes wrong on the server side, it returns an HRESULT of the error which is translated into...
0
by: relaxedrob | last post by:
Hi All, I have a portType such as this: <portType name="CMLeJobSoapGetEmpBrand"> <operation name="EJobGetEmpBrand"> <input message="tns:EJobEmpBrdReq" name="EJobEmpBrdReq"/> <output...
0
by: Nol | last post by:
Hi all, My webservice throws an exception, which is translated into a soap Fault in the soap message body. See below for the actual message format as it is send by the server. On the (dotNet)...
0
by: Aaron | last post by:
The following code snippet was posted in an MSDN blog, which seems to indicate that "wired in" deserialization of SoapFault elements to .NET exception types will work on Indigo/WCF. Is there any...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: =?Utf-8?B?TWFucHJlZXQgU3VzaGls?= | last post by:
I am having a Webservice within which i am throwing SOAP Exceptions and therefore whenever something wrong happens a SOAP fault comes up in the response - see below: <?xml version="1.0"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.