d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Some C Help Actually Should Be Very Easy > New To Pointers And Mem Management
123Next
Add Reply New Topic New Poll
Member
Posts: 7,319
Joined: Apr 20 2007
Gold: 2,383.33
Jul 5 2016 04:16pm
Let me know if you have an email address so I can reach you pretty quickly.


Looking for ASAP clarification.

Can pay FG if you feel its required.


Thanks!
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jul 5 2016 05:29pm
Post your questions, problems, or error messages with relevant code snippets.
Member
Posts: 7,319
Joined: Apr 20 2007
Gold: 2,383.33
Jul 5 2016 05:50pm
Pming! dont want to violate academic integrity posting here!

Thanks sir!
Member
Posts: 7,319
Joined: Apr 20 2007
Gold: 2,383.33
Jul 5 2016 06:43pm
Nevermind running out of time and cant find it for the life of me, I'll just have mods clean this up when I am done:


We are just working with a 2D matrix loaded into a 1D array, between the pointers and structures I am lost.

Program compiles but regardless of how many changes I make, I faced with: segmentation fault (core dumped).



Code
#include<stdio.h>
#include<stdlib.h>

int i,j;
/* create type called matrix_t that holds dimensions and pointer*/
typedef struct matrix{
int rows; /* number of rows*/
int cols; /* number of columns*/
int *datalocation; /* pointer to where data is stored*/
} matrix_t;

/*create a new matrix and allocate memory */
matrix_t * new_matrix(int rows, int cols) {

matrix_t * onedimensionalmatrix = (matrix_t *) malloc(rows*cols);

/*set row and column size for manipulation*/
onedimensionalmatrix->rows = rows;
onedimensionalmatrix->cols = cols;

/* set all values of matrix to zero by default */
for(i = 0; i < (rows*cols); i++){
onedimensionalmatrix->datalocation[i] = 0;
}

return onedimensionalmatrix;
}
/* neatly print the matrix */
void print_matrix(const matrix_t * matrix){

for (i = 0; i < (matrix->rows); i++)
{
for (j = 0; j < (matrix->cols); j++)
{
printf("%d \t",matrix->datalocation[i*j]);
}
printf("\n");/* skip line */
}
}

int main(){

matrix_t * tester;
tester = new_matrix(10,10);
print_matrix(tester);
return 0;
}



My instructors advice:
Quote
I didn't run your code, but it looks reasonable. The reason you are getting the seg fault (or at least part of it) is this line:
matrix->datalocation[i*j]
in print matrix. You need to convert the row/col coordinates (in this case, i & j) to the index in datalocation. There is a post on the discussion board about how to do that. Other than that it looks good. just make sure you have this much working before you move on - with a good base the rest of it is much easier.


I couldnt find a related discussion post and I have made multiple manipulations. ALSO the method values and parameters are set in stone. Is there an issue with me using typedef instead of constant when defining matrix_t?

as it stands, it compiles with no errors, and I cannot continue my project until this portion is complete.

Feel free to rip me apart here.

Thanks!

Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jul 5 2016 10:00pm
Your instructor is partially handicapped if he thinks that is the only issue with this code.

I would of imagined

Code
onedimensionalmatrix->datalocation[i] = 0;


would be a sure sign that something was amist as explained in my pm to you.

Let's just write 0 to all the memory addresses without allocating anything.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 6 2016 04:44pm
Quote (AbDuCt @ Jul 5 2016 11:00pm)
Your instructor is partially handicapped if he thinks that is the only issue with this code.

I would of imagined

Code
onedimensionalmatrix->datalocation[i] = 0;


would be a sure sign that something was amist as explained in my pm to you.

Let's just write 0 to all the memory addresses without allocating anything.


this.

You are blindly writing a value to some memory address. I would seg fault your ass too and I am not even an operating system.

This post was edited by Eep on Jul 6 2016 04:44pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jul 6 2016 05:49pm
Quote (Eep @ Jul 6 2016 06:44pm)
this.

You are blindly writing a value to some memory address. I would seg fault your ass too and I am not even an operating system.


I ended up rewriting and explaining his project 2 different ways for him to learn from with lots of comments. Seems to be eager to learn so its cool.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 7 2016 06:17pm
Quote (AbDuCt @ Jul 6 2016 06:49pm)
I ended up rewriting and explaining his project 2 different ways for him to learn from with lots of comments. Seems to be eager to learn so its cool.


fair enough. Should definitely learn to be wary of memory references early on, though. As long as you understand that the opportunity to shoot yourself in the foot exists, you might do a few more code reviews before smashing that compile button.
Banned
Posts: 4,407
Joined: Apr 28 2016
Gold: Locked
Trader: Scammer
Warn: 10%
Jul 10 2016 05:39pm
I think you need to debug line by line when you get a segmentation error...been a while since I used C iunno

This post was edited by eLeMeNt477 on Jul 10 2016 05:40pm
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Jul 12 2016 01:58am
Code
matrix->datalocation[i*j]


Depending on whether you keep your data by column or by row it should be
Code
matrix->datalocation[i + j * matrix->rows]
or
Code
matrix->datalocation[i*matrix->cols + j]
Go Back To Programming & Development Topic List
123Next
Add Reply New Topic New Poll