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

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 structDisplayLogData *[iCount];

// iCount = 50 structDisplayLogData is a strcuture of data

Thanks
Nov 15 '07 #1
5 2269
dev_15 <na************@googlemail.comwrites:
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 structDisplayLogData *[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:

structDisplayLogData *an_array_of_ptrs_to_structs[iCount];

now just remove the name and add 'new':

new structDisplayLogData *[iCount];

If you thought

structDisplayLogData *array[iCount];

should mean:

structDisplayLogData (*array)[iCount];

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

structDisplayLogData *(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 structDisplayLogData;

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

delete [] ptrLogArray;

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

Ben Bacarisse wrote:
dev_15 <na************@googlemail.comwrites:
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 structDisplayLogData *[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:

structDisplayLogData *an_array_of_ptrs_to_structs[iCount];

now just remove the name and add 'new':

new structDisplayLogData *[iCount];

If you thought

structDisplayLogData *array[iCount];

should mean:

structDisplayLogData (*array)[iCount];

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

structDisplayLogData *(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.comwrote,
>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 structDisplayLogData *[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<structDisplayLogDataLogArray(iCount);

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

ptrLogArray[0] = new structDisplayLogData;

-------------
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.com/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
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...
3
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...
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...
2
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...
5
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
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>...
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: ...
10
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
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.