possible seg fault

J

jhagens14

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);
if (len >= 1){
cout << "Address: " << addressLine;
cout << "\n";
}
}
return (0);
}

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

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

it just works fine...

Please help!...
 
L

Laurent D.A.M. MENTEN

(e-mail address removed) 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);
if (len >= 1){
cout << "Address: " << addressLine;
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...
 
J

jhagens14

(e-mail address removed) 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);
if (len >= 1){
cout << "Address: " << addressLine;
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){
//Print value..
}

but same problem..
 
J

jhagens14

(e-mail address removed) 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);
if (len >= 1){
cout << "Address: " << addressLine;
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){
//Print value..
}

but same problem..- Hide quoted text -

- Show quoted text -


I am sorry I meant..

if(addressLine){
//Print value..
}
 
L

LR

Laurent said:
(e-mail address removed) 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);
if (len >= 1){
cout << "Address: " << addressLine;
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
 
R

runner

(e-mail address removed) 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);
if (len >= 1){
cout << "Address: " << addressLine;
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){
//Print value..
}

but same problem..- Hide quoted text -

- Show quoted text -

I am sorry I meant..
if(addressLine){
//Print value..
}





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

....

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

Laurent D.A.M. MENTEN

LR a écrit :
Laurent said:
(e-mail address removed) 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);
if (len >= 1){
cout << "Address: " << addressLine;
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...
 
J

jhagens14

LR a écrit :




Laurent said:
(e-mail address removed) 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);
if (len >= 1){
cout << "Address: " << addressLine;
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.

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

- Show quoted text -


That worked by setting rest to NULL...Thanks!!
 
K

Kai-Uwe Bux

runner said:
(e-mail address removed) 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);
if (len >= 1){
cout << "Address: " << addressLine;
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){
//Print value..
}

but same problem..- Hide quoted text -

- Show quoted text -

I am sorry I meant..
if(addressLine){
//Print value..
}





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

...

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


Did you mean:

for ( i = 0; addressLine != 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
 
R

red floyd

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);
if (len >= 1){
cout << "Address: " << addressLine;
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 <iostream> and std::cout
4. You never use string.h. Don't include it. Also, use <cstring>
instead of string.h.
 
R

runner

Kai-Uwe Bux said:
runner said:
On Sep 16, 5:27 pm, "Laurent D.A.M. MENTEN"





(e-mail address removed) 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);
if (len >= 1){
cout << "Address: " << addressLine;
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){
//Print value..
}

but same problem..- Hide quoted text -

- Show quoted text -

I am sorry I meant..
if(addressLine){
//Print value..
}





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

...

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


Did you mean:

for ( i = 0; addressLine != 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)
{
//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++ )

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.
 
J

James Kanze

On Sep 16, 5:38 pm, (e-mail address removed) wrote:
(e-mail address removed) 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);
if (len >= 1){
cout << "Address: " << addressLine;
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){
//Print value..
}
but same problem.
I am sorry I meant..
if(addressLine){
//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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,871
Messages
2,569,919
Members
46,171
Latest member
A.N.Omalum

Latest Threads

Top