![]() |
allocation of memory in 2d array
Ello all
I am currently attempting to write a program that dynamically allocates memeory according to what is in a text file. The text file contains rows of numbers with the first row describing the size of the matrix eg 2 3 1 2 3 2 1 3 4 5 5 after much hair pulling and fustration I am sure that it is in the memory allocation/deallocation that I am having difficulties with. I have attached my code for these two functions which are part of a larger program. The first function opens the file and calls the memory allocation function the next function then populates the 2d array the next function then calcculates the required calculations on the array the next function the displays the results the next functuion then frees the memory Code:
/*--------------------------------------------------------* |
Re: allocation of memory in 2d array
Here are two different methods of allocating and freeing a dynamically allocated 2D array.
Code:
#include <stdio.h>- it takes only 2 mallocs to allocate the whole thing. - the actual data is contiguous, as it would be for a true 2D array. - you don't need the number of rows in order to free it at the end. |
Re: allocation of memory in 2d array
Thanks heaps for that. Ill implement that and will get back if I have any furether questions
Thanks again |
Re: allocation of memory in 2d array
Can someone explain what size_t does in this case. I have looked it up on the net and it doesent make any sense, so can you make your explaination preeschool level. thanks
|
Re: allocation of memory in 2d array
perhaps i should just submit my entire program and see if somebody wants to try and debug it. I have the code compliing, but it dumps out at the memory allocation.
|
Re: allocation of memory in 2d array
size_t is the type which sizeof() returns, that's all. In simplistic terms it is not that much different from an int.
Did you try to write the whole program before trying to run it? |
Re: allocation of memory in 2d array
No I was doing it slowly and in increments ,and got stuck on the momery allocation
I can read in the file, and the processing part is doable, but with out the memory allocation, and the transfer of data into the 2d array its nothing |
Re: allocation of memory in 2d array
Sorry, I've still no idea what it is you've tried and how you could possibly be going wrong.
Post some code. |
Re: allocation of memory in 2d array
Quote:
Ill PM you... |
Re: allocation of memory in 2d array
Quote:
|
| All times are GMT +5.5. The time now is 10:15. |