Originally Posted by
tabstop
You're either lucky or unlucky -- I get "Segmentation fault", which is what I would expect given the complete unsuitability of
Code:
table[0] = new short[cols * rows];
in making a 2-D array. Why not do table = new short[rows][cols], other than that's a real array and not an short **? If you do want the short ** approach, you're going to have to use a for-loop. (First by setting table = new short*[rows], and then by setting each table[i] = new short[cols].)
I wanted to do the table = new short[rows][cols] thing first, but that was even less working. (I have to say I'm new to pointers and dynamical allocation of memory.) Here I try something else, based on a few examples form the book I'm learning from (there's nothing really specific in it about what bugs me here):
Code:
#include <iostream>
#include <fstream>
using namespace std;
// Prototypes
void loadImage(short *table, short *secTable, unsigned short &rows, unsigned short &cols);
int main() {
short* image; // tables
short* zones;
// short** image = new short*[1]; // tables
// short** zones = new short*[1];
unsigned short rows, cols; // dimensions
loadImage(image, zones, rows, cols);
cout << "de retour dans le programme\n";
cout << "rows: " << rows << endl << "cols: " << cols << endl;
for (int i = 0;i < rows;i++){
for (int j = 0;j < cols;j++) {
cout << image[i][j] << " ";
}
cout << endl;
}
/* delete []image[0];
delete []image;
delete []zones[0];
delete []zones;
*/ return 0;
}
// Reads the picture and stores information in variables
void loadImage(short *table, short *secTable, unsigned short &rows, unsigned short &cols) {
ifstream picture("lac.txt");
if (!picture) {
cout << "Erreur lors de l'ouverture du fichier lac.txt\n";
exit(1);
}
picture >> rows; // dimensions
picture >> cols;
table = new short[rows][cols];
secTable = new short[rows][cols];
/* table[0] = new short[cols * rows]; // allocate tables
for (int i = 1;i < rows;i++)
table[i] = table[i-1] + cols;
secTable[0] = new short[cols * rows];
for (int i = 1;i < rows;i++)
secTable[i] = secTable[i-1] + cols;
*/ for (int r = 0;r < rows;r++) { // read picture
for (int c = 0;c < cols;c++) {
picture >> table[r][c];
}
}
picture.close();
for (int i = 0;i < rows;i++){
for (int j = 0;j < cols;j++) {
cout << table[i][j] << " ";
}
cout << endl;
}
cout << "end of the function\n";
return;
cout << "this should NEVER run\n";
}
But then I get these errors preventing me from compiling:
Code:
1>cyanobac.cpp
1>c:\documents and settings\administrator\my documents\udes\s1\app 6\cyanobac\cyanobac\cyanobac.cpp(33) : error C2109: subscript requires array or pointer type
1>c:\documents and settings\administrator\my documents\udes\s1\app 6\cyanobac\cyanobac\cyanobac.cpp(54) : error C2540: non-constant expression as array bound
1>c:\documents and settings\administrator\my documents\udes\s1\app 6\cyanobac\cyanobac\cyanobac.cpp(54) : error C2440: '=' : cannot convert from 'short (*)[1]' to 'short *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\administrator\my documents\udes\s1\app 6\cyanobac\cyanobac\cyanobac.cpp(55) : error C2540: non-constant expression as array bound
1>c:\documents and settings\administrator\my documents\udes\s1\app 6\cyanobac\cyanobac\cyanobac.cpp(55) : error C2440: '=' : cannot convert from 'short (*)[1]' to 'short *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\administrator\my documents\udes\s1\app 6\cyanobac\cyanobac\cyanobac.cpp(64) : error C2109: subscript requires array or pointer type
1>c:\documents and settings\administrator\my documents\udes\s1\app 6\cyanobac\cyanobac\cyanobac.cpp(70) : error C2109: subscript requires array or pointer type
1>cyanobac - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
:S