473,385 Members | 1,720 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,385 software developers and data experts.

allocating for an array of pointers

lets say i want to fill up a multidimentional array, but i wish to allocate
memory for it on the fly.

i assume i declare,
char **a;

but how do i allocate memory for the pointers, so i can then allocate to the
pointers to which those pointers point?

essentially lets say i during runtime deturmine i must allocate space for 60
pointers,

a = malloc(size of(char *) * 60);

what is this in effect allocating for?

and subsequently when i wish to allocate memory for a pointer to which one
of the pointers point,

a[0] = malloc(128); /* to allocate to the first pointer in the array of
pointers, 128 bytes */

im probably overlooking something very simple, and it comes to mind that ive
been in this pickle awhile back... i just cant seem to remember what that
solution was. and i have read over the C faq.
Nov 14 '05 #1
3 2151
Erik S. Bartul wrote:
lets say i want to fill up a multidimentional array, but i wish to allocate
memory for it on the fly.

i assume i declare,
char **a;

but how do i allocate memory for the pointers, so i can then allocate to the
pointers to which those pointers point?


int n, m, i;

/* read n and m */
a = malloc(n * sizeof *a); /* allocates n char* (type of *a) */
for(i=0; i < n; i++){
/* allocates m char (type of *a[i]) */
a[i] = malloc(m * sizeof *a[i]);
}

This allocates n x m chars.

HTH,

/david

--
"As a scientist, Throckmorton knew that if he were ever to break wind in
the echo chamber, he would never hear the end of it."

Nov 14 '05 #2
i appologize for the hasty post, the allocation method i was using worked
fine... it was an error elsewhere in the code. i should have debugged for
awhile before assuming i messed up on the allocation :P
Nov 14 '05 #3


"Erik S. Bartul" wrote:
lets say i want to fill up a multidimentional array, but i wish to allocate
memory for it on the fly.

i assume i declare,
char **a;

but how do i allocate memory for the pointers, so i can then allocate to the
pointers to which those pointers point?

essentially lets say i during runtime deturmine i must allocate space for 60
pointers,

a = malloc(size of(char *) * 60);

what is this in effect allocating for?

and subsequently when i wish to allocate memory for a pointer to which one
of the pointers point,

a[0] = malloc(128); /* to allocate to the first pointer in the array of
pointers, 128 bytes */

im probably overlooking something very simple, and it comes to mind that ive
been in this pickle awhile back... i just cant seem to remember what that
solution was. and i have read over the C faq.


Have you read and comprehended faq question 6.16?
It is located at: http://www.eskimo.com/~scs/C-faq/q6.16.html

This question does a good job on explaining various ways of allocating a
two-dimensional array.

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: soni29 | last post by:
hi, i'm reading a c++ book and noticed that the author seems to allocate memory differently when using classes, he writes: (assuming a class called CBox exists, with member function size()): //...
21
by: Philipp | last post by:
Hello, a very simple question: Ok I have a class MyClass with a constructor MyClass(int) (no constructor without argument defined) how can I make an array of pointers to objects of that class,...
4
by: kak3012 | last post by:
Hi, After my question today, I am not sure if this is the right group to ask but I have a simple question I guess. I have multi dimensional array called cover what I do is this iT GIVES AN...
15
by: fix | last post by:
Hi all, I am writing a program using some structs, it is not running and I believe it is because there's some memory leak - the debugger tells me that the code causes the problem is in the malloc...
14
by: Gattaca | last post by:
I would like to create a matrix of integers by allocating memory dynamically (malloc or calloc) because i and j are defined during execution of the program. I have got not problem to do this in...
3
by: Paminu | last post by:
If I have this struct: typedef struct test{ int x; int y; }container; Now I would like to make an array of 5 pointers to this struct: int main(void){
6
by: toktam | last post by:
Hi guys! I defined a type like this: typedef struct { char *key; int freq; float pr; } TableEntry; and then I have an array of pointers to this struct:
3
by: UncleRic | last post by:
Greetings: I'm trying to get the ASCI C syntax correct here. I want to create memory space for a variable-size array of pointers to structs. This is what I found in theScripts' archive: ...
5
by: dev_15 | last post by:
Hi, I'm going through some code and thought that this allocates an array of structs but its supposed according to comments to allocate an array of pointer to structs. What does it actually do ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.