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
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
Try putting print statements and find which line it is segmenting.
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
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?
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
do you find this online tester accurate?
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
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
Quote:
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.
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.
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
Quote:
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.
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.
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
Quote:
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.
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..?
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.
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
Quote:
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.
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?
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
char *body[line][charLength];
Guys, correct me if I'm wrong but I do believe this is declaring a 3D array, not a 2D like he wants.
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
True, a (well-known) typo. However, if you mix up [] with () when DECLARING a vector, any average compiler will catch that up.
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
Quote:
Originally Posted by ariell
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.
It's just related to post count. I didn't notice it had changed.
Quote:
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.
All true (although I would describe it in terms of class semantics). I was simply mentioning that the "copying" statement didn't seem relevant to any of that. Nothing you said was wrong; but your phrasing implied a causal relationship which I don't think exists. But that's neither here nor there.
Quote:
P.S. is it always that dangerous to post here?
You think that's "dangerous", you obviously didn't see any of the Star Trek forums the day Enterprise was canceled.
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
Use string rather than char array.
If you want multidimensional char array is this syntax.
char *aStr[10] -- Need allocate memory
char aStr[10][200] Not require allocate memory on runtime.
Both syntax is same.
I hope this help.
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
Quote:
Originally Posted by jmass16
char *body[line][charLength];
Guys, correct me if I'm wrong but I do believe this is declaring a 3D array, not a 2D like he wants.
Technically, yes. The final dimension stores a pointer to a null-terminated array of char's, aka a c-style string. In that context most people will talk about a 2D array of strings rather than a 3D array of char.
Re: I'm SICK of THIS! WHY THIS CODE DOES NOT WORK!
First of all: Thanks for that StarTrek hint - I didn't laugh like that in a long time.
Just dropped by cos I started a big compile/build, which is a perfect chance to have a break.
I agree with putting it in the context of class semantics, it is definitely more precise. Be that as it may, when you're new and feel to be under attack you'll try to make a post as short as possible.
However, not, that things are "calmed", I feel much better, and I learned, that boards/forums is quite obviously not what I am good for.
A great time to everybody, and best from the south.