|
-
October 1st, 2008, 06:34 PM
#1
I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
Code:
.
.
.
.
int line = 0;
int charLength =0;
char *body[line][charLength];
cout<<"Please enter line of texts. Press Ctrl+D to quit:"<<endl;
cin>>body[line][charLength];
while(*body[line][charLength] != 4)
{
if (*body[line][charLength] == '\n')
{
line++;
charLength++;
cin>>body[line][charLength];
}
else
{
charLength++;
cin>>body[line][charLength];
}
}
This code suppose to read input from keyboard. Reads lines of texts unitl user press CTRL+D
It compiles, i enter characters, press enter ..... I get message says segmentation fault
I tried to fix it, i still get the error message .... Can you help please
Input will look like:
***********************************
hello my name is none ...........
this is a test
GOOD JOB!
***********************************
Can you help plz
Last edited by f.ben.isaac; October 1st, 2008 at 06:39 PM.
-
October 1st, 2008, 06:47 PM
#2
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
Try putting print statements and find which line it is segmenting.
-
October 1st, 2008, 07:08 PM
#3
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
Code:
int line = 0;
int charLength =0;
char *body[line][charLength];
This isn't legal C++.
Code:
int main()
{
int line = 0;
int charLength =0;
char *body[line][charLength];
}
Code:
Thank you for testing your code with Comeau C/C++!
Tell others about http://www.comeaucomputing.com/tryitout !
Your Comeau C/C++ test results are as follows:
Comeau C/C++ 4.3.10.1 (May 29 2008 09:37:15) for ONLINE_EVALUATION_BETA1
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions
"ComeauTest.c", line 5: error: expression must have a constant value
char *body[line][charLength];
^
"ComeauTest.c", line 5: error: expression must have a constant value
char *body[line][charLength];
^
2 errors detected in the compilation of "ComeauTest.c".
In strict mode, with -tused, Compile failed
Arrays must be initialized with constant boundaries, not variables.
Turn on your ANSI switch on your compiler, since your compiler is set to compile something other than ANSI C++.
Regards,
Paul McKenzie
-
October 1st, 2008, 07:16 PM
#4
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
Even if that were legal syntax
int line = 0;
int charLength =0;
char *body[line][charLength];
with line and charLength being 0, you're not allocating anything so anything you do will be off the bounds of your array.
Are you sure you want a char* there and not just a char?
-
October 1st, 2008, 07:16 PM
#5
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
do you find this online tester accurate?
-
October 1st, 2008, 07:18 PM
#6
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
i see, but i want to make my 2d array dynamic. I do not want it to be fixed
-
October 1st, 2008, 07:19 PM
#7
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
 Originally Posted by AuraofMana
Try putting print statements and find which line it is segmenting.
Typically one would run the app in the debugger to find the faulty line. There's no reason to debug using print statements.
-
October 1st, 2008, 07:20 PM
#8
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
Variable-sized arrays are from C99, not C++. But that's not the root of your problem, it's just incidental.
You have a 2D array of char pointers, but they're not pointing at anything in particular. They're just garbage values. You have to assign them some memory to point at if you want them to behave as you're expecting. This is typically done using the "new" operator (paired with delete[] when you're done with the memory).
Or, an easier solution is:
Code:
vector< vector<string> > body;
with the <vector> and <string> includes.
-
October 1st, 2008, 07:21 PM
#9
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
 Originally Posted by f.ben.isaac
i see, but i want to make my 2d array dynamic. I do not want it to be fixed
You could use the dynamic array class vector. I doubt you really want to get into writing a dynamic array class yourself. You should just declare the array with reasonable limits.
-
October 1st, 2008, 07:23 PM
#10
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
Paul is perfectly right, array bounds must be a constant expression. A good work-around (in cases where you NEED to make the bound(s) variable) is simply to using a vector:
Example:
void someMethod(int n) {
int ar1[n]; // will invoke an error (run-time in worst case), cos n is not a constant expression, but
vector<int> ar1[n]; // works perfectly
}
This is simply because assigning a vector involves COPYING its elements.
Best from the south.
ariell
programming is understanding
-
October 1st, 2008, 07:58 PM
#11
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
 Originally Posted by ariell
This is simply because assigning a vector involves COPYING its elements.
True, but I'm not sure what that has to do with anything.
-
October 1st, 2008, 08:04 PM
#12
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
Since you are an elite member, I am NOT quite sure whether u want to "tease" me or this comment is serious..?
ariell
programming is understanding
-
October 1st, 2008, 08:08 PM
#13
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
Oh, hey, I am elite now. Cool.
Things get copied into a vector. Assigning from one vector into another involves copying. I agree with all that. But that's unrelated to why vectors can be variable-sized. That's just the way they're designed, enabled by using heap memory internally.
-
October 1st, 2008, 08:12 PM
#14
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
 Originally Posted by ariell
Since you are an elite member, I am NOT quite sure whether u want to "tease" me or this comment is serious..?
"vector<int> ar1[n]" works no better than trying to declare a dynamic array of POD types in the same manner.... because you're still trying to create a dynamic array (of vectors). Unless you meant to type "vector<int> ar1(n)", which uses the constructor to initialize the vector with 10 elements.
-
October 1st, 2008, 08:23 PM
#15
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
First of all, this is what is printed right under your name. It must not catch you by surprise, that a newbee like me recognizes such details.
Everything you say is right, and I am definitely not supposed to warm up stack/heap discussions. However, in the given context, your words are "incomplete". I tried to explain that bounds (defined as having to be constant) are to "work-around" with vectors to show the decisive difference: Constant expressions are SIMPLE expressions, evaluated by a compiler BEFORE an app is LINKED. A vector (object) assigned with a var (which "n" is in my example), will be determined at RUNTIME.
Maybe I took a strange approach, sorry in advance.
Best from the south.
P.S. is it always that dangerous to post here?
ariell
programming is understanding
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|