| | |
Dynamic Memory Allocation - offset pointers
![]() |
•
•
Join Date: Feb 2005
Posts: 2
Reputation:
Solved Threads: 0
Hi all,
I am just learning DMA, and am supposed to write 2 functions utilizing the offset technique, such that a 2-D array starts and ends the index of each dimension as the user specifies.
matrix(): creates a 2D array of float type numbers, indexing from -n to n for each dimension. A proper pointer is to be returned so that after invocation of this function, a 2D array of float type numbers and the specified starting and ending indices can be used.
free_matrix(): releases the memory allocated for the 2D array created by matrix().
I am using C, and use a UNIX-based compiler to run my program. Below is what I have:
I keep getting the error "Segmentation fault". What is wrong with my code?
All those in blue were given in the question, so I can't change any of that. I followed the code to create the array closely with that given in my notes, which showed how to create a 2D array.
On a similar matter, I was able to create a 1D array (with starting and ending indices as -n and n respectively), but had problems freeing the memory. My function to free the memory was
void free_vector(float *x, int n)
{
free(x + n);
}
but I got the same "Segmentation Fault" error. What is wrong?
Thank you.
Regards,
Rayne
I am just learning DMA, and am supposed to write 2 functions utilizing the offset technique, such that a 2-D array starts and ends the index of each dimension as the user specifies.
matrix(): creates a 2D array of float type numbers, indexing from -n to n for each dimension. A proper pointer is to be returned so that after invocation of this function, a 2D array of float type numbers and the specified starting and ending indices can be used.
free_matrix(): releases the memory allocated for the 2D array created by matrix().
I am using C, and use a UNIX-based compiler to run my program. Below is what I have:
#include <stdio.h> #include <stdlib.h> float** matrix(float **x, int n); void free_matrix(float **x, int n); int main(void) { float **x; int i, j, n; scanf("%d", &n); x = matrix(x, n); for (i = -n ; i <= n ; i++) { for (j = -n ; j <= n ; j++) x[i][j] = i + j; } for (i = -n ; i <= n ; i++) { for (j = -n ; j <= n ; j++) printf("%f ", x[i][j]); printf("\n"); } free_matrix(x, n); return 0; } float** matrix(float **x, int n) { int i; x = (float**) malloc(2 * n * sizeof(float*)); for (i = 0 ; i < n ; i++) { x[i] = (float*) malloc(2 * n * sizeof(float)); x[i] -= n; } x -= n; return x; } void free_matrix(float **x, int n) { int i; x += n; for (i = 0 ; i < n ; i++) free(x[i] + n); free(x); }
I keep getting the error "Segmentation fault". What is wrong with my code?
All those in blue were given in the question, so I can't change any of that. I followed the code to create the array closely with that given in my notes, which showed how to create a 2D array.
On a similar matter, I was able to create a 1D array (with starting and ending indices as -n and n respectively), but had problems freeing the memory. My function to free the memory was
void free_vector(float *x, int n)
{
free(x + n);
}
but I got the same "Segmentation Fault" error. What is wrong?
Thank you.
Regards,
Rayne
Last edited by alc6379; Feb 28th, 2005 at 5:35 pm.
x = (float**) malloc(2 * n * sizeof(float*));
That's the problem I belive, why the 2? ,just put n like
x = (float**) malloc(n * sizeof(float*));
Then try.......
Also put in debug lines following each step,that way you can quickly find out where the problems are....
And this is really weird for (i = -n ; i <= n ; i++), I belive it's a mistake, it should be for (i = 0 ; i < n ; i++)
That's the problem I belive, why the 2? ,just put n like
x = (float**) malloc(n * sizeof(float*));
Then try.......
Also put in debug lines following each step,that way you can quickly find out where the problems are....
And this is really weird for (i = -n ; i <= n ; i++), I belive it's a mistake, it should be for (i = 0 ; i < n ; i++)
![]() |
Similar Threads
- Error with Dynamic Memory Allocation (C++)
- Malloc/Calloc Dynamic Memory Allocation. (C++)
- Please Help Dynamic Memory Allocation. (C++)
- regarding dynamic memory allocation (C)
- memory allocation ptr to array? how? (C)
- Dynamic memory allocation homework (C++)
Other Threads in the C Forum
- Previous Thread: Linked Lists
- Next Thread: GetKeyState syntax
| Thread Tools | Search this Thread |
* adobe ansi api array binarysearch centimeter changingto char character cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax database directory feet fflush fgets file floatingpointvalidation fork frequency function givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux highest histogram homework i/o inches infiniteloop input interest intmain() iso keyboard kilometer km linked linkedlist linux linuxsegmentationfault list locate looping lowest match meter microsoft mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc program programming pyramidusingturboccodes read recv recvblocked repetition reversing scanf scheduling segmentationfault send single socketprograming socketprogramming stack standard string suggestions systemcall unix urboc user voidmain() wab whythiscodecausesegmentationfault win32api windows.h windowsapi





