473,657 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Allocating array of pointers to structs

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

ptrLogArray = new structDisplayLo gData *[iCount];

// iCount = 50 structDisplayLo gData is a strcuture of data

Thanks
Nov 15 '07 #1
5 2287
dev_15 <na************ @googlemail.com writes:
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

ptrLogArray = new structDisplayLo gData *[iCount];
It behaves as per the comments. What follows 'new' is, syntactically,
a declaration with the name missed out. If you declared this array
you'd write:

structDisplayLo gData *an_array_of_pt rs_to_structs[iCount];

now just remove the name and add 'new':

new structDisplayLo gData *[iCount];

If you thought

structDisplayLo gData *array[iCount];

should mean:

structDisplayLo gData (*array)[iCount];

(i.e. a pointer to an array of structs) rather than what it *does* mean:

structDisplayLo gData *(array[iCount]);

(an array of pointers) then you need to study the syntax a bit more.

--
Ben.
Nov 15 '07 #2
Thanks also in the code later on is

ptrLogArray[0] = new structDisplayLo gData;

-------------
more code

delete [] ptrLogArray;

Does this delete the structs that the pointers are pointing to?

Ben Bacarisse wrote:
dev_15 <na************ @googlemail.com writes:
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

ptrLogArray = new structDisplayLo gData *[iCount];

It behaves as per the comments. What follows 'new' is, syntactically,
a declaration with the name missed out. If you declared this array
you'd write:

structDisplayLo gData *an_array_of_pt rs_to_structs[iCount];

now just remove the name and add 'new':

new structDisplayLo gData *[iCount];

If you thought

structDisplayLo gData *array[iCount];

should mean:

structDisplayLo gData (*array)[iCount];

(i.e. a pointer to an array of structs) rather than what it *does* mean:

structDisplayLo gData *(array[iCount]);

(an array of pointers) then you need to study the syntax a bit more.

--
Ben.
Nov 15 '07 #3
On Thu, 15 Nov 2007 08:45:56 -0800 (PST) in comp.lang.c++, dev_15
<na************ @googlemail.com wrote,
>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

ptrLogArray = new structDisplayLo gData *[iCount];
Array of pointers; that's what the '*' is about.

However, that's probably undesirable code. What happens next is either
trying to write via an uninitialized pointer, or else lots of memory
management headaches. What you wanted instead is probably

std::vector<str uctDisplayLogDa taLogArray(iCou nt);

Nov 15 '07 #4
dev_15 <na************ @googlemail.com writes:
Thanks also in the code later on is

ptrLogArray[0] = new structDisplayLo gData;

-------------
more code

delete [] ptrLogArray;

Does this delete the structs that the pointers are pointing to?
No. As someone else has pointed out, if you are changing the code
consider switching to std::vector.

PS. Don't top post.

--
Ben.
Nov 15 '07 #5
dev_15 wrote:
Thanks also in the code later on is
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.c om/c++-faq-lite/how-to-post.html>
Nov 15 '07 #6

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

Similar topics

5
1340
by: Koster | last post by:
Just a quickie: I heard that arrays declared in the global scope are automatically initialised with a block of zeros. To help with making my C source compatible with multiple compilers, I'd like to know if this is a proper ANSI-C thing or just another C++ quirk that only confuses people, if it's true at all. Thanks for your time, Koster.
3
2169
by: Erik S. Bartul | last post by:
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
15
6717
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 function. Is there any general rules that tell me when to allocate memory? I thought I don't have to if it is a variable that's not a pointer, and I have to if it is. I am a bit confused about the arrays particularly. In normal situations,...
2
2124
by: Simon Morgan | last post by:
I hope this isn't OT, I looked for a newsgroup dealing purely with algorithms but none were to be found and seeing as I'm trying to implement this in C I thought this would be the best place. I have an array of structs containing data which I'd like to output ordered based on the value of a single member. I was wondering if there is a relatively simple way of doing this without actually modifying the structure of the array? I had a...
5
3123
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
15
3829
by: Paminu | last post by:
Still having a few problems with malloc and pointers. I have made a struct. Now I would like to make a pointer an array with 4 pointers to this struct. #include <stdlib.h> #include <stdio.h> typedef struct _tnode_t { void *content; struct _tnode_t *kids;
3
9315
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: This is what I'm toying with: #include <stdio.h>
10
1760
by: Chris Saunders | last post by:
Here is the declaration of a struct from WinIoCtl.h: // // Structures for FSCTL_TXFS_READ_BACKUP_INFORMATION // typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT { union { //
2
11939
by: hal | last post by:
Hi, I'm trying to make an array of pointers to 'TwoCounts' structs, where the size of the array is arraySize. Right now I'm just mallocing enough space for all the pointers to the structs, and mallocing space for the pointer 'countPtr' in each struct, but do I need to do anything else? Thanks. typedef struct TwoCounts { int *countPtr;
0
8742
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
8518
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
8621
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
7354
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...
1
6177
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5643
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
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
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
1734
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.