Advertisement

Dynamic allocated arrays....

Started by April 20, 2001 10:37 AM
1 comment, last by Spartacus 23 years, 9 months ago
Hi! Can someone tell me how I can get the size of a dynamic allocated array? I mean how can i find out how many elements have been allocated? For example: char* pnt = new char[10]; How can i find out how many char elememts have been allocated. I cant do this: int size = sizeof(pnt); because that will only give me the size of the pointer which is 4. And i cant do this either: int size = sizeof(*pnt); because that will give me the size of the type of element pnt points to...Does someone know how i can find out how many elements has been allocated??? Thanks...

Real programmers don't document, if it was hard to write it should be hard to understand

Your code has to remember what it allocated.

milo
Advertisement
You need to save the array size when allocating it.

eg :

typedef struct {
int size;
char* pChar;
} arrayOfchar;

or use the STL

This topic is closed to new replies.

Advertisement