LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 09-17-2006, 02:10 AM   #1
ratsietsi
LQ Newbie
 
Registered: Sep 2006
Posts: 2

Rep: Reputation: 0
Many dimension array


I am trying to declare a two dimensional array without giving it any size. The program compiles without any errors but i keep on getting a segmentation error and I can't see where it comes from. This is how the code looks like:

----from the class------------------
private:
int **LDM;
============================================================

----this part is from a different segment of the code-----

LDM = new **int;

for(i = 0; i <= 4; i++){
for( j = 0; j < k; j++)
{
temp = patern1[i] - patern2[j];
if(temp < 0)
temp = -1 * temp;
LDM[i][j] = temp;
cout << "Local Distance Matrix: " << LDM[i][j] << endl;
}
 
Old 09-17-2006, 05:06 AM   #2
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 30
Quote:
I can't see where it comes from.
Using a debugger (such as gdb) would help with this.

Quote:
I am trying to declare a two dimensional array without giving it any size.
Well, there's your problem. This is not the way things are done in C or C++.

C and C++ will not do anything that you don't explicitly tell them to do. If you don't say "allocate memory for my two dimensional array of size X by Y", they won't... and you haven't. Besides, even if C++ were designed to do this sort of allocation for you, the math necessary to translate a two-dimensional coordinate into a one-dimensional memory address requires the size of at least one of the dimensions to be known, and you aren't specifying that either.

Now, look at the line in your code which you intend to allocate an array with:
Code:
LDM = new **int;
You are not creating a new int array, you are creating a new **int: a pointer to a pointer to an int. On a typical 32-bit machine, you've just allocated only 4 bytes of memory.

This is the source of your segmentation fault, since you eventually try to access memory that hasn't been allocated (the usual meaning of a segmentation fault).

You could accomplish what you're trying to do with a vector of vectors, or by using the new operator properly (the method I'd recommend, since the dimensions of your array are apparently known before you enter the nested for-loops).

I suggest finding a good C++ tutorial through google, or check out cppreference.com if you need a quick reference to some of the STL classes like vector (I've got it bookmarked). There are also plenty of good C++ books out there that could teach you these things.
 
Old 09-17-2006, 05:38 AM   #3
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
Also:
Code:
int **LDM;
LDM = new **int;
Beside the fact, that this does not, what you want it to do (as 'zhangmaike' said), I can't believe, that you are able to compile this. Not only that it is a wrong syntax of new, semantically you try to assign a "int***" to a "int**"?? -I've no possibilietes to test this at the moment, so maybe I'm wrong and there exists some strange hacks, but I would code:
Code:
int **LDM;
LDM = new int*;
... But as mentioned, this isn't what you want at all.

Last edited by Flesym; 09-17-2006 at 05:42 AM.
 
Old 09-17-2006, 01:28 PM   #4
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 30
I've just tested the following code for whether or not it compiles:
Code:
int main() {
        int **LDM;
        LDM = new **int;

        delete LDM;
        return 0;
}
It does not. It complains of a syntax error before the * token on line 3. Moving th ** to the other side of int causes this error to become:
Code:
error: cannot convert `int***' to `int**' in assignment
Flesym is correct.

ratsietsi: I can only assume that you are using a non-standard C++ compiler, though it would surprise me greatly if there exists such a C++ compiler which would compile the above code (or yours) without errors.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dell Dimension power down solly Slackware 1 05-21-2006 06:24 PM
Multi-dimension array problem. ArthurHuang Programming 2 05-20-2006 09:36 AM
Reading from a txt file into a two dimension array in C kponenation Programming 3 11-26-2005 07:04 PM
Change Resolution (not dimension) LinxNew Mandriva 5 05-10-2005 04:20 AM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM


All times are GMT -5. The time now is 08:58 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration