473,396 Members | 1,995 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,396 software developers and data experts.

strange problem

The following code can be compiled. But When I run it, it causes
"Segmentation fault".

#include <iostream>

int main(){
char **c1;

*c1 = "HOW"; // LINE1

std::cout <<"1"<<std::endl;

++(*c1);
*c1 = "ARE";

std::cout <<"2"<<std::endl;
++(*c1);
*c1 = "YOU";
std::cout <<"3"<<std::endl;
std::cout <<*c1<<std::endl;
(*c1)++;
std::cout <<*c1<<std::endl;
(*c1)++;
std::cout <<*c1<<std::endl;

}

Why LINE1 causes "Segmentation fault"?

What is the correct way to write the above code?

Thanks a lot.

May 28 '06 #1
11 1936
* ju******@gmail.com:
The following code can be compiled. But When I run it, it causes
"Segmentation fault".

#include <iostream>

int main(){
char **c1;

*c1 = "HOW"; // LINE1

std::cout <<"1"<<std::endl;

++(*c1);
*c1 = "ARE";

std::cout <<"2"<<std::endl;
++(*c1);
*c1 = "YOU";
std::cout <<"3"<<std::endl;
std::cout <<*c1<<std::endl;
(*c1)++;
std::cout <<*c1<<std::endl;
(*c1)++;
std::cout <<*c1<<std::endl;

}

Why LINE1 causes "Segmentation fault"?
Dereferencing an uninitialized pointer - Undefined Behavior.

What is the correct way to write the above code?


Rather, what's a better way to use your compiler? Up the warning
levels. Ensure that you get a clean compile (no warnings), always.

To output the text "HOW ARE YOU", do

std::cout << "HOW ARE YOU" << std::endl;

Just avoid pointers, they're dangerous.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 28 '06 #2
junw2...@gmail.com wrote:
The following code can be compiled. But When I run it, it causes
"Segmentation fault".
Yes. The english name for that is "invalid memory access."
#include <iostream>

int main(){
char **c1;
This is a variable which holds a memory address. If it held a valid
address (it doesn't, unless by chance), the contents of the memory at
said address would be another memory address. If *that* address were
valid, it would be the location of a variable of type char.

A little confusing? Yes. Easy to mix up and make a mistake, resulting
in a seg fault? Yup.
*c1 = "HOW"; // LINE1
Right, so here you're dereferencing a pointer which does not hold a
valid memory address. Hence, seg fault. If *c1 held a valid pointer
(to a variable of type char*), the result of this operation would be to
make *c1 point to the area in static memory containing the string
literal "HOW."
What is the correct way to write the above code?


Use std::string. It exists to prevent you from feeling the pain you
now feel.

Luke

May 28 '06 #3
Thanks.
Certainly, std::string is safer. I am trying to learn
pointer-to-pointer to char.
If using C language, how to initialize c1 to three strings---"HOW",
"ARE", "YOU"?

Thanks again.

May 28 '06 #4
junw2000 wrote:
Certainly, std::string is safer.
This newsgroup always instructs students to start with the Standard library
stuff.
I am trying to learn
pointer-to-pointer to char.
If using C language, how to initialize c1 to three strings---"HOW",
"ARE", "YOU"?


Start with three pointers to characters:

char const * strings[] = { "HOW", "ARE", "YOU" };

And point to the first one:

char const * * p = strings;

Now p[1] points to "ARE", and p[1][1] points to 'R'.

You might be able to Google for "aggregate initializers" from here.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
May 28 '06 #5
ju******@gmail.com wrote:
Certainly, std::string is safer. I am trying to learn
pointer-to-pointer to char.
Sure. The best way, in my opinion, to learn to use char** correctly is
to first learn about using, for example, int**. Integers are easier to
deal with because you don't have the added complications of string
literals, string lengths, null termination, etc. It may sound like I'm
being perversely intransigent, but the fact is that the fundamental
things you're failing to understand, in the code you posted, are issues
which pertain to all pointer-to-pointer types. The reason you're
failing to understand them has a lot to do with the fact that you're
trying to jump straight into the complexities of char** without
understanding those fundamentals.
If using C language, how to initialize c1 to three strings---"HOW",
"ARE", "YOU"?


This isn't a C newsgroup. If you want help with C, try comp.lang.c
(actually, I see that you already did so).

Luke

May 28 '06 #6
Luke Meyers wrote:
If using C language, how to initialize c1 to three strings---"HOW",
"ARE", "YOU"?


This isn't a C newsgroup. If you want help with C, try comp.lang.c
(actually, I see that you already did so).


Mr. Manners reminds the Gentle Poster that some neophytes have not yet
learned to write, "if using C-style C++..."

Note the transition was from std::string; the poster was trying to
distinguish from the C++ Standard Library.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
May 28 '06 #7
LuB

ju******@gmail.com wrote:
The following code can be compiled. But When I run it, it causes
"Segmentation fault".

#include <iostream>

int main(){
char* d1 = "";
char** c1 = &d1;

*c1 = "HOW"; // LINE1

std::cout <<"1"<<std::endl;

++(*c1);
*c1 = "ARE";

std::cout <<"2"<<std::endl;
++(*c1);
*c1 = "YOU";
std::cout <<"3"<<std::endl;
std::cout <<*c1<<std::endl;
(*c1)++;
std::cout <<*c1<<std::endl;
(*c1)++;
std::cout <<*c1<<std::endl;

}

As the previous posters implied, this approach is not recommended.

-LuB

May 28 '06 #8
LuB schrieb:
#include <iostream>

int main(){

char* d1 = "";
char** c1 = &d1;


Are you sure you want to do this?
*c1 = "HOW"; // LINE1

std::cout <<"1"<<std::endl;

++(*c1);
Advances the pointer after d1.
*c1 = "ARE";
Will do bad things.
std::cout <<"2"<<std::endl;
++(*c1);
*c1 = "YOU";
Even worser.
std::cout <<"3"<<std::endl;
std::cout <<*c1<<std::endl;
(*c1)++;
std::cout <<*c1<<std::endl;
(*c1)++;
std::cout <<*c1<<std::endl;

}


As the previous posters implied, this approach is not recommended.


And does not work. You should better use std::string and std::vector.

Thomas
May 28 '06 #9

<ju******@gmail.com> wrote in message
news:11**********************@j73g2000cwa.googlegr oups.com...
The following code can be compiled. But When I run it, it causes
"Segmentation fault".

#include <iostream>

int main(){
char **c1;
Okay, c1 is a pointer to a pointer of char.
*c1 = "HOW"; // LINE1


Here you are saying to change the contents of where c1 points to point to
the string literal "HOW". But, c1 is not yet pointing to valid memory.
It's pointing anywhere since it was never initialized. So you are
attempting to change memory that your program doesn't "own", hence the
segmentation fault.

What it looks like you are trying here is to have c1 to be an array of
pointers to char. So you need to set aside the memory for the pointers.

This would work:

char* c1[3];
c1[0] = "HOW";

because now c1 is an array of 3 pointers to char, and the memory has been
allocated for 3 pointers. Also, though, you could now reference these by
your char** such as;

char* c0[3];
char** c1 = c0;
*c0 = "HOW";

because c1 now points to memory that has been allocated (by the declaration
of c0 which allocated the space for 3 pointers).
May 28 '06 #10

ju******@gmail.com wrote:
The following code can be compiled. But When I run it, it causes
"Segmentation fault".

#include <iostream>

int main(){
char **c1;
const char * c1;
*c1 = "HOW"; // LINE1


c1 = "HOW";

[snip]

the rest you can figure out ;-)

May 29 '06 #11
LuB

Thomas J. Gritzan wrote:
LuB schrieb:
#include <iostream>

int main(){

char* d1 = "";
char** c1 = &d1;


Are you sure you want to do this?


No I'm not sure. What do you think?
*c1 = "HOW"; // LINE1

std::cout <<"1"<<std::endl;

++(*c1);
Advances the pointer after d1
I'm sure you are correct - but I can't tell .. but I've no idea what
your point is. *c1 is now "OW"
*c1 = "ARE";
Will do bad things.
Again - I'm sure you are correct ... but what are you talking about?

g++ isn't complaining.
std::cout <<"2"<<std::endl;
++(*c1);
*c1 = "YOU";
Even worser.
Again - I can't disagree ... cause I've no idea what you are talking
about.

Someone else posted this ... I added two lines at the top ... the
program compiles and appears to run in g++. I'm having trouble parsing
your post. Again, I'm sure your are correct - but I've no idea what you
are saying?
std::cout <<"3"<<std::endl;
std::cout <<*c1<<std::endl;
(*c1)++;
std::cout <<*c1<<std::endl;
(*c1)++;
std::cout <<*c1<<std::endl;

}


As the previous posters implied, this approach is not recommended.


And does not work. You should better use std::string and std::vector.


Ok ... I'm not saying your wrong.

But I beg to take issue with your verbage.

It does indeed work.

$ g++ -v
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--enable-checking=release --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-libgcj-multifile
--enable-languages=c,c++,objc,java,f95,ada --enable-java-awt=gtk
--with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre
--host=i386-redhat-linux
Thread model: posix
gcc version 4.0.2 20051125 (Red Hat 4.0.2-8)

$ g++ -o test.exe test3.cpp
$ ./test.exe
1
2
3
YOU
OU
U
[zentdd@s5 cpp]$
Maybe you have some mystical notion about _your_ understanding or
definition of the word WORK.

If so - maybe thats all I'd need to clarify what you're talking about.

I don't know if its portable. I don't know if it works all the time
everywhwere. I don't know if its to spec. I don't know if its
dangerous. I don't claim any of those things. And I think it would
likely be impossible to prove many such things via formal proof.

So again - I'm sure that given your definition of the word "Work" - you
are correct. But given the context of your statement - I'm at a loss
for what you are talking about.

Imagine a student compiling this program, running it - turning it in --
and the teacher claiming "it doesn't work - use .... instead." and
givng the student an F.

And how about ... "are you sure you want to do this?" "advances the
pointer" "will do bad things" "even worser".

Does that teach the student anything?

Your post was just noise. The only people that know what you're talking
about ... already knew what you were talking about -- if indeed, you
said anything worth talking about.

Jun 10 '06 #12

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

Similar topics

5
by: Rob Ristroph | last post by:
Hi, It's pretty unhelpful to post "I have a huge piece of code that crashes in strange places, what's the problem?" but that's basically my problem and I really am at my wit's end. The piece...
2
by: Paul Drummond | last post by:
Hi all, I am developing software for Linux Redhat9 and I have noticed some very strange behaviour when throwing exceptions within a shared library. All our exceptions are derived from...
25
by: Neil Ginsberg | last post by:
I have a strange situation with my Access 2000 database. I have code in the database which has worked fine for years, and now all of a sudden doesn't work fine on one or two of my client's...
2
by: TB | last post by:
I am seeing a very strange problem as follows... I have a loop where a fair amount of processing is going on and near the top of the loop I access a class that has only static helper functions...
1
by: Sam Kong | last post by:
Hello! Recently I had a strange problem with Visual C# 2005 beta 1. When I ran(F5 key) a program, <#if DEBUG> statement was not working. It ran as RELEASE mode. So I had to manually define...
8
by: Spam Trap | last post by:
I am getting strange resizing problems when using an inherited form. Controls are moving themselves seemingly randomly, but reproducibly. "frmBase" is my base class (a windows form), and...
11
by: Martin Joergensen | last post by:
Hi, I've encountered a really, *really*, REALLY strange error :-) I have a for-loop and after 8 runs I get strange results...... I mean: A really strange result.... I'm calculating...
12
by: StephQ | last post by:
I have a class Bounds with two constructors: class Bounds { private: list<SegmentupperLinearSpline; // Upper bound. list<SegmentlowerLinearSpline; // Lower bound. ....
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
5
by: ioni | last post by:
Good day, fellows! I have a strange problem – at my site there is a flash strip, that loads data dynamically. It works fine (grabs data from the remote server and presents it), however in IE7...
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: 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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.