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

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

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
Asked:
mcravenufo
  • 9
  • 4
1 Solution
 
AxterCommented:
Hi mcravenufo,
because one is a size of a pointer and the other is the size of an array.

David Maisonave :-)
Cheers!
0
 
AxterCommented:
> >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
 
AxterCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
mcravenufoAuthor 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
 
AxterCommented:
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
 
AxterCommented:
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
 
AxterCommented:
mcravenufo,
Why do you need to know the size?

David Maisonave :-}
0
 
mcravenufoAuthor 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
 
AxterCommented:
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
 
mcravenufoAuthor 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
 
AxterCommented:
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
 
AxterCommented:
Actually, in above example I forgot to actually assign value to the data, so it's not a complete working example.
0
 
mcravenufoAuthor Commented:
That's ok. I appreciate the help.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

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