473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Dynamic mem allocation for pointers

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
4 1186
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
11
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I...
5
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
13
by: jimjim | last post by:
Hello, I am coming from a C background and the below dynamic allocation of an array of pointers makes sense to me: #define SIZE 2 int **p; p = malloc ( SIZE * sizeof ( int * )); for(int j=0;...
1
by: john townsley | last post by:
OK so with c++ when using pointers for dynamic allocation at runtime, like a database type program. I am talking about a user adding records of an unkown amount at runtime, so pointers would be the...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
4
by: Tomassus | last post by:
Hi there, I have a problem with dynamic memory allocation. I know that it would have been easier to use vectors methods, but i want to know what i do here wrong. This is one of my methods in...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
3
by: ranjeetasharma81 | last post by:
Hi all, I have a big C-cod, in which there are lots of dynamic memory allocation used. I want to replace dynamic memroy allocation by static arrays. The following are the problems that i am...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.