473,803 Members | 4,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"expandable " arrays - using pointers ... How to ?

Hi,

I want to create a container structure (array/list etc), that is expandable.

Currently, I have static arrays like this

SomeStruct *ptrArray[MAX_NUM_STRUCT] ;

What I want to do is to be able to use ptrs to ptrs like so:

SomeStruct **ptrArray ;

However, I am unsure as to how to do the ff after the declaration above:

1). Allocate memory (both initially and for subsequent data structure
increases)
2). Navigate the array - i.e. index to specific element in the array

mtia

Nov 15 '05 #1
2 3227

Alfonso Morra wrote:
Hi,

I want to create a container structure (array/list etc), that is expandable.

Currently, I have static arrays like this

SomeStruct *ptrArray[MAX_NUM_STRUCT] ;

What I want to do is to be able to use ptrs to ptrs like so:

SomeStruct **ptrArray ;

However, I am unsure as to how to do the ff after the declaration above:

1). Allocate memory (both initially and for subsequent data structure
increases)
2). Navigate the array - i.e. index to specific element in the array

mtia


Use realloc() to create and extend a dynamic buffer. You can access
individual elements as you would with a normal array. Here's an
untested example:

#include <stdlib.h>

typedef struct {...} SomeStruct;

SomeStruct **extendArray(S omeStruct **arr, size_t *cursize, size_t
extent)
{
SomeStruct **tmp;

/**
* realloc() will return NULL on failure, address of the
* first element in the buffer on success. In order to
* extend the buffer, realloc() may move it to a
* different location, so the base address may change.
*/
tmp = realloc(arr, sizeof arr[0] * (*cursize + extent));
if (tmp)
{
arr = tmp;
*cursize += extent;
}
return arr;
}

int main(void)
{
/**
* Make sure array pointer is initially NULL
*/
SomeStruct **myArr = NULL;
size_ t arrSize = 0;
size_t i;

/**
* Initially allocate 20 elements to myArr
*/
myArr = extendArray(myA rr, &arrSize, 20);
if (arrSize != 20)
{
/**
* Allocation failed; fatal error
*/
return EXIT_FAILURE;
}

for (i = 0; i < arrSize; i++)
{
/**
* do something with myArr[i]
*/
}

/**
* Extend array by another 5 elements
*/
myArr = extendArray(myA rr, &arrSize, 5);
if (arrSize != 25)
{
/**
* Attempt to extend existing array failed.
*/
return EXIT_FAILURE;
}

for (i = 20; i < arrSize; i++)
{
/**
* do something with myArr[i]
*/
}

return EXIT_SUCCESS;
}

There are probably a hundred better ways to indicate success or
failure, and you'll want to add some intelligent error handling, but
that should give you the basic idea.

Nov 15 '05 #2


John Bode wrote:
Alfonso Morra wrote:
Hi,

I want to create a container structure (array/list etc), that is expandable.

Currently, I have static arrays like this

SomeStruct *ptrArray[MAX_NUM_STRUCT] ;

What I want to do is to be able to use ptrs to ptrs like so:

SomeStruct **ptrArray ;

However, I am unsure as to how to do the ff after the declaration above:

1). Allocate memory (both initially and for subsequent data structure
increases)
2). Navigate the array - i.e. index to specific element in the array

mtia

Use realloc() to create and extend a dynamic buffer. You can access
individual elements as you would with a normal array. Here's an
untested example:

#include <stdlib.h>

typedef struct {...} SomeStruct;

SomeStruct **extendArray(S omeStruct **arr, size_t *cursize, size_t
extent)
{
SomeStruct **tmp;

/**
* realloc() will return NULL on failure, address of the
* first element in the buffer on success. In order to
* extend the buffer, realloc() may move it to a
* different location, so the base address may change.
*/
tmp = realloc(arr, sizeof arr[0] * (*cursize + extent));
if (tmp)
{
arr = tmp;
*cursize += extent;
}
return arr;
}

int main(void)
{
/**
* Make sure array pointer is initially NULL
*/
SomeStruct **myArr = NULL;
size_ t arrSize = 0;
size_t i;

/**
* Initially allocate 20 elements to myArr
*/
myArr = extendArray(myA rr, &arrSize, 20);
if (arrSize != 20)
{
/**
* Allocation failed; fatal error
*/
return EXIT_FAILURE;
}

for (i = 0; i < arrSize; i++)
{
/**
* do something with myArr[i]
*/
}

/**
* Extend array by another 5 elements
*/
myArr = extendArray(myA rr, &arrSize, 5);
if (arrSize != 25)
{
/**
* Attempt to extend existing array failed.
*/
return EXIT_FAILURE;
}

for (i = 20; i < arrSize; i++)
{
/**
* do something with myArr[i]
*/
}

return EXIT_SUCCESS;
}

There are probably a hundred better ways to indicate success or
failure, and you'll want to add some intelligent error handling, but
that should give you the basic idea.


many thanks John

Nov 15 '05 #3

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

Similar topics

14
2251
by: Robert S | last post by:
I am trying to use POST to transfer data to another page. When I do this, '.' characters get converted to"_". For example: #index.html: <form action="test.php" method="post"> <input type="submit" name="filename.txt"> </form> #test.php <html>
2
1700
by: Katit | last post by:
Hello, I searched archives and all pointing to use of VB6 objects from C# I want something different. 1. How can I build expandable platform? Let's say DLL's will have common set of methods (interface) and I will have main program using those DLL's. I want to achieve when I won't have to recompile main program and be able to add modules by dropping them into folder. Then
7
4826
by: bookon | last post by:
I was running into the System.Drawing.Image.FromStream "parameter is not valid" on some of the images I was retrieving from a blob column in Sql Server. I thought there were corrupt images as almost all worked (all are gifs), and only a few broke when this line ran: Image img = Image.FromStream(ms); here is the original code: b = (byte)dt.Rows.ItemArray; //b.ToString() ms = new MemoryStream(); ms.Write(b, 0, b.Length);
11
2241
by: gg9h0st | last post by:
i saw a code refactorying onload event listener window.onloadListeners=new Array(); window.addOnLoadListener=function(listener) { window.onloadListeners=listener; } why declare the onloadListeners, addOnLoadListener below window?
18
3651
by: desktop | last post by:
I have 3 types of objects: bob1, bob2 and bob3. Each object is identified by a unique ID which gets returned by the function getId(). All bobs are descendants from class BaseBob which is an abstract class that has the virtual function getId(). I then have a function that prints a special string when a bob meets another bob (all combinations of bobs has to meet and since the are schizophrenic they can also meet themselves):
0
1439
by: No.23 | last post by:
#!/usr/bin/env python import libgmail from time import gmtime, strftime fp = open('/tmp/python.list', 'w') ga = libgmail.GmailAccount("No.0023@gmail.com", "mypass") ga.login() result = self.ga.getMessagesByLabel('python.list', True)
3
3336
by: cketcham | last post by:
All, I have the following code: use 5.010; use LWP::UserAgent; use URI::URL; use LWP::Debug qw(+); my $browser = LWP::UserAgent->new();
11
2260
by: 200dogz | last post by:
Hi guys, I'm making an expandable table which currently layers of <table>s are used to contain the table rows in order to show/hide them. This works, but having to format columns of the child table to be like its parent table can be quite annoying. Before I use tables to do this I use <tbody> to store the <tr>s. Doing so made the format looked perfect unfortunately it could only be used for at most two layers, since having a <tbody>...
0
9699
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
10542
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...
1
10289
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
10068
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9119
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
6840
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5496
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...
0
5625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2968
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.