Thanks for your replies. By the way, i not really understand why u say you are not free all the memory. You did it very well. My problem is the allcoation successfull at allocate function but when come back to main the rowptr and rowptr_1 become NULL. I don't know why.
The swap done it well but i don't.
Thanks for your replies.
|
Contributor
|
|
| 24Apr2007,14:14 | #21 |
|
Team Leader
|
![]() |
| 24Apr2007,17:39 | #22 |
|
I don't understand what you mean by
Quote:
IF YOU ARE USING SOME CODE WHICH YOU ARE NOT SHOWING, THEN DON'T SHOW THE CODE YOU ARE NOT USING, BUT THE CODE THAT IS FAILING!!!! I AM NOT going to play guessing games with you. Present your problem in a sensible way. IF you are trying to use your Allocate function, INSTEAD of the code in main, then SHOW HOW YOU ARE CALLING IT. The function that you show makes NO arrangements for passing the rowpointers back to main and main shows NO arrangements to pass pointers to rowpointers which can be set by the function. Everything in the function concerning the rowpointers is a local variable and will disappear (quite normally, I assure you) when the function returns. Last edited by DaWei; 24Apr2007 at 17:50.. |
|
Contributor
|
|
| 25Apr2007,07:15 | #23 |
|
Is it ok i post the entire code ? Then, you will understand what i mean.
I bag your pardon. |
|
Contributor
|
|
| 25Apr2007,07:18 | #24 |
|
I have a function called allocation uses to allocated memory for rowptr and rowptr_1. The operation of allocation memory for rowptr and rowptr_1 is successful but after the allocation function finished its job, return back to main. The rowptr and rowptr_1 become NULL.
I have review it from the debugging process, the error messa ge is cannot evaluate the expression. |
|
Team Leader
|
![]() |
| 25Apr2007,08:03 | #25 |
|
Zip up your CURRENT code, as in the code that fails, and attach it to your post. Don't include any huge blocks that are commented out, and don't include any functions that aren't actually called. The stuff is hard enough to read. One hopes, coming out the other side of this, that you will have learned how to rationally whittle down your code to the parts that actually have an effect on the operation that is failing. I supect that you are having scope resolution issues, but we shall see.
|
|
Go4Expert Founder
|
![]() |
| 25Apr2007,08:47 | #26 |
|
Thats one way but then there is trade off that you are asking people to download and see and I would add that you apply your judgement.
I would add the files and not file as attachment and have the file content in the post as a reference something like I am having trouble in some function Code:
SomeFunction Code:
SomeOtherFunctionsLikeThis Code:
main() |
|
Contributor
|
|
| 25Apr2007,13:25 | #27 |
|
I bag your pardon shabbir. I will remember forever. I will do it two method as mentioned by dawei and shabbir.
Thanks for your taught. I will remember forever. |
|
Contributor
|
|
| 26Apr2007,08:12 | #28 |
|
That's true. I think i might having scope problem.
This problem occurs here. Code:
Allocate(nMatrix, nrowptr, ncolptr, nrow_1ptr, ncol_1ptr); Add(rowptr, rowptr_1, nrowptr, ncolptr); Code:
void Allocate(int nMatrix, int *nrowptr, int *ncolptr,
int *nrow_1ptr, int *ncol_1ptr)
{
int symmetric;
int row_loop, col_loop;
static int **rowptr;
static int **rowptr_1;
switch(nMatrix)
{
case 1:
{
rowptr = malloc(sizeof(int **) * (*nrowptr));
if (rowptr == NULL)
{
perror("Dynamic Memory Allocation for row Fails");
}
for (row_loop = 0;row_loop < (*nrowptr); row_loop++)
{
rowptr[row_loop] = malloc(sizeof(int *) * (*ncolptr));
if (rowptr[row_loop] == NULL)
{
perror("Dynamic Memory Allocation for column Fails");
}
}
for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
{
for (col_loop = 0; col_loop < (*ncolptr); col_loop++)
{
printf("Enter the [%d][%d] number : ", row_loop, col_loop);
scanf("%d", &rowptr[row_loop][col_loop]);
}
}
break;
}
// ------------------------------------------------------------
case 2:
{ // Enter the first matrix
do
{
printf("\nEnter the First Matrix : \n");
// Allocate memory for rowptr **
// if nrow == 3, then same as rowptr *[3]
rowptr = malloc(sizeof(int **) * (*nrowptr));
if (rowptr == NULL)
{
perror("Dynamic Memory Allocation for row Fails");
}
// For each rowptr * , allocate ncolumn
for (row_loop = 0;row_loop < (*nrowptr); row_loop++)
{
rowptr[row_loop] = malloc(sizeof(int *) * (*ncolptr));
if (rowptr[row_loop] == NULL)
{
perror("Dynamic Memory Allocation for column Fails");
}
}
for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
{
for (col_loop = 0; col_loop < (*ncolptr); col_loop++)
{
printf("Enter the [%d][%d] number : ", row_loop, col_loop);
scanf("%d", &rowptr[row_loop][col_loop]);
}
}
// -----------------------------------------------------------
// Enter the second Matrix
printf("Enter the Second Matrix : \n");
// Allocate memory for rowptr_1 **
// if nrow == 3, then same as rowptr_1 *[3]
rowptr_1 = malloc(sizeof(int **) * (*nrow_1ptr));
if (rowptr_1 == NULL)
{
perror("Dynamic Memory Allocation Matrix 2 for row Fails");
}
// For each rowptr_1 * , allocate ncolumn
for (row_loop = 0; row_loop < (*nrow_1ptr); row_loop++)
{
rowptr_1[row_loop] = malloc(sizeof(int *) * (*ncol_1ptr));
if(rowptr_1[row_loop] == NULL)
{
perror("Dynamic Memory Allocation Matrix 2 for column Fails");
}
}
for (row_loop = 0; row_loop < (*nrow_1ptr); row_loop++)
{
for (col_loop = 0; col_loop < (*ncol_1ptr); col_loop++)
{
printf("Enter the [%d][%d] number : ", row_loop, col_loop);
scanf("%d", &rowptr_1[row_loop][col_loop]);
}
}
// Check the Dimension of Two Matrix
symmetric = Symmetric(nrowptr, ncolptr, nrow_1ptr, ncol_1ptr);
if (symmetric == 1)
{
printf("The two Matrix is same dimension");
}
else
{
printf("The two Matrix is different dimension");
}
}while(symmetric == 0);
break;
}
}
}
// ------------------------------------------------------------
int Symmetric(int *nrowptr, int *ncolptr,
int *nrow_1ptr, int *ncol_1ptr)
{
int symmetric;
if ((*nrowptr) == (*nrow_1ptr) && (*ncolptr) == (*ncol_1ptr))
{
symmetric = 1;
return symmetric;
}
else
{
symmetric = 0;
return symmetric;
}
}
I think this might clarify what i want more clearly. Thanks for your help. Your help is greatly appreciated by me and others. |
|
Team Leader
|
![]() |
| 26Apr2007,22:26 | #29 |
|
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int** matBegin;
typedef struct sMatrix *pMatrix;
typedef struct sMatrix
{
unsigned rows;
unsigned cols;
matBegin begin;
} matrix;
void demoFill (pMatrix matrix)
{
unsigned i, j;
for (i = 0; i < matrix->rows; ++i)
{
for (j = 0; j < matrix->cols; ++j)
{
matrix->begin[i][j] = i*10 + j;
}
}
}
void displayMatrix (pMatrix matrix)
{
unsigned i, j;
printf ("\n");
for (i = 0; i < matrix->rows; ++i)
{
for (j = 0; j < matrix->cols; ++j)
{
printf ("%02d ", matrix->begin[i][j]);
}
printf ("\n");
}
printf ("\n");
}
matBegin allocateMatrix (pMatrix matrix, unsigned rows, unsigned cols)
{
unsigned i;
int mallocFail = 0;
matrix->begin = malloc (sizeof (*matrix->begin) * rows);
if (matrix->begin == NULL) return NULL;
memset (matrix->begin, 0, sizeof (*matrix->begin) * rows);
matrix->cols = cols;
matrix->rows = rows;
for (i = 0; (i < rows) && (!mallocFail); ++i)
{
matrix->begin [i] = malloc (cols * sizeof (int));
if (!matrix->begin [i]) mallocFail = 1;
}
if (mallocFail)
{
for (i = 0; i < rows; ++i) free (matrix->begin [i]);
matrix->cols = 0;
matrix->rows = 0;
free (matrix->begin);
matrix->begin = NULL;
}
return matrix->begin;
}
int badNews (char *trouble)
{
fprintf (stderr, "%s\n", trouble);
return -1;
}
int main (int argc, char *argv[])
{
matrix firstMatrix;
matrix secondMatrix;
matBegin pFirst;
matBegin pSecond;
pFirst = allocateMatrix (&firstMatrix, 3, 4);
if (pFirst == NULL) return badNews ("Malloc failed");
pSecond = allocateMatrix (&secondMatrix, 4, 5);
if (pSecond == NULL) return badNews ("Malloc failed");
demoFill (&firstMatrix);
demoFill (&secondMatrix);
displayMatrix (&firstMatrix);
displayMatrix (&secondMatrix);
return 0;
}
Quote:
Originally Posted by Output |
|
Contributor
|
|
| 27Apr2007,10:38 | #30 |
|
I not really understand the program. What the difference your program and my program ?
Thanks for your help. |


