Dynamic mem allocation for pointers

S

srini

Hi

I need to create two-dimensional array; number of rows and columns
unknown at compile time. The idea is to read a file, print it if
requested by user and to delete when the program quits.

Here is what I came up with using C:

struct diskinfo {

char **part_name; /* name of each partition */
int **num_blocs;

}

I need only one instance of 'diskinfo' in the program. What is the best
way to create & free memory and what are the C libraries that I should
include for this ?

Thanks for your time and help
-Srini
 
V

vighnesh.nayak

Hi Srini,

By what i can understand thius is your requirement.

A program which opens file on demand, reads the content, prints it if
asked for and at exit cleans up the memory used.

I am slightly modifying the structure

u can use this

typedef struct _files
{
char * data;
int fd;
} *pfile;

then sonewhere in main u can say

int main(){
pfile pf;
// U can maintain a seperate variable for storing the number of files.

pf = malloc(some initial number of files);


while ( no more files to be procesed )
{
// get the file name;

// run "stat" on the file, and get the file size

// pf.fd = open(current file, O_RDONLY);
// pf.data = malloc(file size);

// processing...

}

then in a simple for loop before exit u can just do the closing /
deleting the files and freeing up the memory.

}

U need to use

stdio.h, stdlib.h, malloc.h, fcntl.h, sys/stat.h, sys/types.h


Hope u get the logic.

-Vighnesh
 
C

Chuck F.

.... snip ...

U need to use

stdio.h, stdlib.h, malloc.h, fcntl.h, sys/stat.h, sys/types.h

Hope u get the logic.

Please do not top-post, especially in technical newsgroups. And
especially do not use silly abbreviations, such as 'u'. They make
you appear ignorant, besides creating a problem for readers.

In addition, malloc.h, fcntl.h, sys/*.h are all non-standard
headers and are off-topic in c.l.c. Such misinformation is worse
than simply ignoring the OPs query.
 
S

srini

I am slightly modifying the structure

Vighnesh, thanks for the response; I'm specifically trying to implement
dynamic two dimensional arrays to undestand usage of malloc() and
free() for a structure that has 'pointers to pointers' along with other
data types.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top