473,887 Members | 2,280 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically Allocated Multi-Dimensional Arrays

Is my understanding of the allocation of these correct?

I used fixed sized allocations for the example below, so I realize
there is some optimization that can be done to those.

I would like to use these in a linked list for something else I am
working on, but I want to make sure my understanding of the concept is
correct.

For example, from the code below string[0][0] would equal 'h' after
the copy, right?

The one area where I am not quite certain about is freeing these.

Another area where I am uncertain is how to keep track of the sizes
of all the allocations.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>

int
main(void) {
char **string;

string = malloc(20 * sizeof(char *));
if (string == NULL)
errx(1, "malloc failed");
string[0] = malloc(80 * sizeof(char));

if (string[0] == NULL)
errx(1, "malloc failed");
string[1] = malloc(80 * sizeof(char));

if (string[1] == NULL)
errx(1, "malloc failed");

strlcpy(string[0], "hello", 80);
strlcpy(string[1], "world", 80);

printf("%s %s\n", string[0], string[1]);

free(string[0]);
free(string[1]);
free(string);

exit(EXIT_SUCCE SS);
}

Sep 1 '06 #1
6 2635
bw*****@yahoo.c om wrote:
Is my understanding of the allocation of these correct?
Upon first examination, this all looks ok. I don't know what errx()
is, but I assume its defined in err.h in your environment.
I used fixed sized allocations for the example below, so I realize
there is some optimization that can be done to those.

I would like to use these in a linked list for something else I am
working on, but I want to make sure my understanding of the concept is
correct.
A linked list uses individual allocations that are joined up in
sequence by link pointers. What you are doing is collecting them into
an array of strings. That's a different thing (when handled correctly,
the array strategy usually works out better, BTW).
For example, from the code below string[0][0] would equal 'h' after
the copy, right?
Yes.
The one area where I am not quite certain about is freeing these.
You did it correctly. You freed the inner stuff then the outer stuff.
In absence of other copies of the pointers, you have to do it that way
(just because the outer stuff is the only thing with references to the
inner stuff, and that data becomes invalidated as soon as you free it).
Another area where I am uncertain is how to keep track of the sizes
of all the allocations.
Aha! And there's the rub! C gives you *NO* direct mechanisms for
dealing with this. You have to track them yourself, typically through
implicit or specific runtime tracking support.

The strategies and implementations for doing this are varied and can be
convoluted; its hard to recommend one over the other without more
information about your scenario. If you are just interested in a
simple strategy, I will again refer you to my string library (
http://bstring.sf.net/ ) as it contains an array of strings as one of
the types it supports.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>

int
main(void) {
char **string;

string = malloc(20 * sizeof(char *));
if (string == NULL)
errx(1, "malloc failed");
string[0] = malloc(80 * sizeof(char));

if (string[0] == NULL)
errx(1, "malloc failed");
string[1] = malloc(80 * sizeof(char));

if (string[1] == NULL)
errx(1, "malloc failed");

strlcpy(string[0], "hello", 80);
strlcpy(string[1], "world", 80);

printf("%s %s\n", string[0], string[1]);

free(string[0]);
free(string[1]);
free(string);

exit(EXIT_SUCCE SS);
}
--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Sep 2 '06 #2
we******@gmail. com writes:
bw*****@yahoo.c om wrote:
>Is my understanding of the allocation of these correct?

Upon first examination, this all looks ok. I don't know what errx()
is, but I assume its defined in err.h in your environment.
>I used fixed sized allocations for the example below, so I realize
there is some optimization that can be done to those.

I would like to use these in a linked list for something else I am
working on, but I want to make sure my understanding of the concept is
correct.

A linked list uses individual allocations that are joined up in
sequence by link pointers. What you are doing is collecting them into
an array of strings. That's a different thing (when handled correctly,
the array strategy usually works out better, BTW).
[...]

One should be careful about the phrase "array of strings".

In C, a string is a data format, not a data type. Specifically, a
"string" is "a contiguous sequence of characters terminated by and
including the first null character" (C99 7.1.1p1).

What you have in your program is an array of pointers to strings. (A
"pointer to string" is actually a pointer to its first character, but
the standard specifically defines a "pointer to a string" as "a
pointer to its initial (lowest addressed) character".)

You could have had an array of arrays of char (also known as a
two-dimensional array) where each row happens to contain a string, and
called that an "array of strings".

Arrays and pointers in C can be confusing (which is why there's an
entire section of the FAQ devoted to them). Keeping terminology
strictly correct isn't just pedantry; it can be vital to a proper
understanding of how things actually work.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 2 '06 #3

we******@gmail. com wrote:
A linked list uses individual allocations that are joined up in
sequence by link pointers. What you are doing is collecting them into
an array of strings. That's a different thing (when handled correctly,
the array strategy usually works out better, BTW).
But I can have a structure that contains an element that is allocated.

struct node {

char **string;
struct node *next;
};

The string would be allocated for each node and populated. The
weakness
is that each node point need not be contiguous as an array would be.

The weakness still remains in having to do overhead to keep a count of
all
the allocation sizes. The only advantage of allocating them is that I
allocate
memory as I need it, rather than allocating it all at once in the
beginning.

At this point I am exploring different ways to store and manipulate
data.

Sep 2 '06 #4
bw*****@yahoo.c om wrote:
we******@gmail. com wrote:
A linked list uses individual allocations that are joined up in
sequence by link pointers. What you are doing is collecting them into
an array of strings. That's a different thing (when handled correctly,
the array strategy usually works out better, BTW).

But I can have a structure that contains an element that is allocated.

struct node {
char **string;
struct node *next;
};
You probably mean:

struct node {
char * string;
struct node * next;
}

But another possibility is

struct node {
struct node * next;
char string[1]; /* struct hack */
};

Where you use malloc (offsetof (struct node, string) + BUFF_LENGTH) to
allocate each node. In this way you amortize the overhead of the node
allocation with that of the string allocation.
The string would be allocated for each node and populated. The
weakness is that each node point need not be contiguous as an
array would be.
Correct.
The weakness still remains in having to do overhead to keep a count of
all the allocation sizes.
Well, if you don't care about performance, you can always make minimum
assumptions and constantly perform reallocs all the time. I.e., always
realloc the array to have exactly the length of the string plus one
characters in it. In this way, you can "know" the size since it will
always be one more than the length of the string.
[...] The only advantage of allocating them is that I
allocate memory as I need it, rather than allocating it all at once in the
beginning.

At this point I am exploring different ways to store and manipulate
data.
As well you should. If you do enough research into it you're going end
up with the only strategy that makes sense: http://bstring.sf.net/

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Sep 2 '06 #5
On 1 Sep 2006 20:18:30 -0700, "bw*****@yahoo. com" <bw*****@yahoo. com>
wrote:
>
But I can have a structure that contains an element that is allocated.

struct node {

char **string;
char *string;
struct node *next;
};

The string would be allocated for each node and populated. The
weakness
is that each node point need not be contiguous as an array would be.
It's not much of a weakness and my actually be a benefit.

With an array, accessing the n-th entry is a simple
calculation. With a linked list, you must walk the list which is not
difficult but usually more time consuming.

As you note below, memory allocation is different between the
array approach and a linked list one. On a system with fragmented
memory, the allocation for the latter may be succeed (as many times as
you need) while allocation for the former fails on the first try.

Also note that with an array, all the strings would be the
same size. If your strings have widely varying lengths, this might
consume too much memory.
>
The weakness still remains in having to do overhead to keep a count of
all
the allocation sizes. The only advantage of allocating them is that I
Add another member to your structure:
size_t size;
>allocate
memory as I need it, rather than allocating it all at once in the
beginning.

At this point I am exploring different ways to store and manipulate
data.
There are other options. Your original code had an allocated array of
pointers, each pointing to an allocated string. Since pointers don't
consume much memory, you may choose to define the array of pointers
and allocate space only for the strings themselves (assuming you know
the maximum number of strings you will ever need).
Remove del for email
Sep 2 '06 #6

Barry Schwarz wrote:
There are other options. Your original code had an allocated array of
pointers, each pointing to an allocated string. Since pointers don't
consume much memory, you may choose to define the array of pointers
and allocate space only for the strings themselves (assuming you know
the maximum number of strings you will ever need).
So instead of allocating the "array" with malloc or calloc. I would
do something like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>

int
main(void) {

char *string[20] ={0};

string[0] = calloc(80, sizeof(char));
if (string[0] == NULL)
errx(1, "calloc failed");
strlcpy(string[0], "hello", 80);
printf("%s\n", string[0]);
free(string[0]);
exit(0);
}

So what other options do I have?

Sep 3 '06 #7

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

Similar topics

7
2447
by: masood.iqbal | last post by:
I am having lots of trouble getting a simple program that initializs a dynamically allocated 2D array to work. My 2D array is not getting initialized properly, and additionally I am getting a "Null pointer assignment" error. Kindly help. Also, eventually I intend to move this logic to a separate function. For that, I believe, that I will need to pass a pointer-to-pointer-to-pointer type as an arguent. Please confirm.
3
3616
by: Tony Johansson | last post by:
Hello Experts!! When you instansiate varaibles(object) you can do so in four different scops which are. Within a block which is called Local or block scope . Within a function which is called function scope. Whithin a class which is called class scope. Outside a function which is called global or file scope. Now to my question when you allocate memory dynamically you can't say
2
1381
by: songfire | last post by:
Hi everybody! Just wondering if it is possible to point to variables in the heap. For example, is this okay? int * ptr_to_DAIA; // pointer to dynamically allocated integer array ptr_to_DAIA = new int ; for(i=0; i<SIZE; i++) ptr_to_DAIA=i; // now say I want a pointer that points to the element that contains value TARGET
5
5602
by: nmtoan | last post by:
Hi, I could not find any answer to this simple question of mine. Suppose I have to write a program, the main parts of it are as follows: #include <blahblah.h> struct {
37
2805
by: yogpjosh | last post by:
Hello All, I was asked a question in an interview.. Its related to dynamically allocated and deallocated memory. eg. //start char * p = new char; ...
7
2811
by: Michael | last post by:
Hi, What's the benefit to dynamically allocate memory? using namespace std; int main() { char* ptr; ptr="abc";
4
6352
by: wyrmmage | last post by:
hello :) I need to make a dynamically allocated array of pointers, using a .hpp and .cpp file; how do I accomplish this? I think I know how to make an array of pointers, and I know how to make a dynamically allocated array, but I am lost as to how to put the two together... Any help would be appreciated :) -wyrmmage
2
3676
by: BillE | last post by:
I need to create a multi-line label dynamically. Since labels aren't multi-line, I'm creating a textbox and making it look like a label with the multiline property set to true. I want to increase the height of the textbox only if there are multiple lines, and leave it at the default height if the text does not wrap after setting the Text property. I thought I could use the Lines property after setting the Text property,
15
1802
by: tom | last post by:
why delete the dynamically allocated memory twice causes an error, see the code below: int _tmain(int argc, _TCHAR* argv) { int *pi = new int(12); cout<<*pi; delete pi; delete pi; }
7
2168
by: Rob | last post by:
This actually compiles and works but it doesn't seem like the best code, so I was wondering is there another way to do this? template <typename Tvector<T>* addDepth(T) { return new vector<T>; } ....a templated meth...
0
9957
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11173
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10771
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10877
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9593
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5810
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4633
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4239
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.