Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Dynamic mem allocation for pointers

Reply
Thread Tools

Dynamic mem allocation for pointers

 
 
srini
Guest
Posts: n/a
 
      12-14-2005
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

 
Reply With Quote
 
 
 
 
vighnesh.nayak@gmail.com
Guest
Posts: n/a
 
      12-14-2005
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


 
Reply With Quote
 
 
 
 
Chuck F.
Guest
Posts: n/a
 
      12-14-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
srini
Guest
Posts: n/a
 
      12-14-2005
(E-Mail Removed) 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.

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      12-18-2005
On 13 Dec 2005 21:17:11 -0800, "srini" <(E-Mail Removed)> 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>>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
init of class members : mem(0) vs. mem() vs. not-init at all news.aon.at C++ 11 01-29-2011 07:30 PM
allocating mem in a function and assigning a ptr to the first byte of that mem array... mast2as@yahoo.com C Programming 8 03-01-2007 08:18 AM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
Problem with structs and mem. allocation Nafai C++ 7 09-29-2004 09:33 PM



Advertisments