By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 981 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

Dynamic mem allocation for pointers

P: n/a
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

Dec 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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[i].fd = open(current file, O_RDONLY);
// pf[i].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
srini wrote:
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


Dec 14 '05 #2

P: n/a
vi************@gmail.com wrote:
.... 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.

--
Read about the Sony stealthware that is a security leak, phones
home, and is generally illegal in most parts of the world. Also
the apparent connivance of the various security software firms.
http://www.schneier.com/blog/archive...drm_rootk.html
Dec 14 '05 #3

P: n/a
vi************@gmail.com wrote:
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.

Dec 14 '05 #4

P: n/a
On 13 Dec 2005 21:17:11 -0800, "srini" <th*******@yahoo.com> wrote:
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.


See question 6.16 in the FAQ at
http://www.eskimo.com/~scs/C-faq/top.html
<<Remove the del for email>>
Dec 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.