[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 336

# Why are these sizeof values different?

Why are these sizeof values different?

char* text = new char[10];
cout << sizeof text << endl; // produces size 4

char text2[10];
cout << sizeof text2 << endl; // produces size 10
0
mcravenufo
• 9
• 4
1 Solution

Commented:
Hi mcravenufo,
because one is a size of a pointer and the other is the size of an array.

David Maisonave :-)
Cheers!
0

Commented:
> >char* text = new char[10];
> >cout << sizeof text <

The above will just give you the size of the pointer.  It's not going to give you the size of what it's pointing to.
0

Commented:
For example:

int *i = NULL;
char *c = NULL;
long *L = NULL;
int *i2 = new int[99];
char *c2 = new char[123];
long *L2 = new long[33];

cout << sizeof(i) << endl;
cout << sizeof(c) << endl;
cout << sizeof(L) << endl;
cout << sizeof(i2) << endl;
cout << sizeof(c2) << endl;
cout << sizeof(L2) << endl;

All of the above variables will give you the same value for sizeof, because they're all pointers.
0

Author Commented:
Is there a way to find out the size of what its pointing to?

I increased the points since I am asking another question.
0

Commented:
mcravenufo,
> >Is there a way to find out the size of what its pointing to?

You can not determined the allocated size, but if you're storing NULL terminated data, you can determined the size of the string via strlen().

David Maisonave :-}
0

Commented:
If you need dynamic data, I recommend that you use a vector instead or std::string
#include <vector>
using namespace std;

vector<char> text;
0

Commented:
mcravenufo,
Why do you need to know the size?

David Maisonave :-}
0

Author Commented:
>>Why do you need to know the size?

When I use strncpy I would like to know the max value that is allowed to copy.

If I just use strcpy, any amount can be copied over.
0

Commented:
mcravenufo,
> >When I use strncpy I would like to know the max value that is allowed to copy.
If you need this information, you need to store it with the associated pointer.
If you don't save it, there's no way to find out.

I recommend you use std::string instead.  With std::string you can determined it's current size and it's total size, including unused buffer.

David Maisonave :-}
0

Author Commented:
I currently use string. I am looking at some older C applications and needed to make sure I was doing things right.

Thanks!
0

Commented:
mcravenufo,
> >I currently use string. I am looking at some older C applications and
> >needed to make sure I was doing things right.
The best you can do with C, is use a struct that contains the pointer and size allocated;

struct Data
{
char *ptr;
int SizeOf;
};

Data data = {new char[123], 123};

printf("data = %s\n", data.ptr);

Of course for pure C, you would replace new with malloc, and do a typedef for the struct name.
0

Commented:
Actually, in above example I forgot to actually assign value to the data, so it's not a complete working example.
0

Author Commented:
That's ok. I appreciate the help.
0

## Featured Post

• 9
• 4
Tackle projects and never again get stuck behind a technical roadblock.