Allocating array of pointers to structs

D

dev_15

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
 
B

Ben Bacarisse

dev_15 said:
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.
 
D

dev_15

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 said:
dev_15 said:
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.
 
D

David Harmon

On Thu, 15 Nov 2007 08:45:56 -0800 (PST) in comp.lang.c++, dev_15
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<structDisplayLogData> LogArray(iCount);
 
B

Ben Bacarisse

dev_15 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top