sizeof

S

Slain

I am using sizeof to doa memcpy but run into probs because of the
following problem.

static char arr1[] = {0x01,0x00,0x03,0x04};
static char arr2[] = {0x00,0x03,0x04,0x06,0x07};

const char* string_CDR[8]= {arr1,arr2};


cout<<"The size of is " <<sizeof(arr1)<<" and "<<sizeof(arr2) <<endl;
cout<<"The size of is " <<sizeof(string_CDR[0])<<" and
"<<sizeof(string_CDR[1]) <<endl;

I expect the sizeof(string_CDR[0]) to be 4 and of
sizeof(string_CDR[1]) to be 5. But the output is as below.
The size of is 4 and 5
The size of is 4 and 4


This is messing up my memcpy in the main function as the sizeof is not
what I am expecting. Can anyone point out the problem.

Thanks
Manny
 
I

Ian Collins

Slain said:
I am using sizeof to doa memcpy but run into probs because of the
following problem.

static char arr1[] = {0x01,0x00,0x03,0x04};
static char arr2[] = {0x00,0x03,0x04,0x06,0x07};

const char* string_CDR[8]= {arr1,arr2};


cout<<"The size of is " <<sizeof(arr1)<<" and "<<sizeof(arr2) <<endl;
cout<<"The size of is " <<sizeof(string_CDR[0])<<" and
"<<sizeof(string_CDR[1]) <<endl;
This line outputs sizeof(char*) twice.
This is messing up my memcpy in the main function as the sizeof is not
what I am expecting. Can anyone point out the problem.
There is no way to calculate the size of the arrays pointed to in
string_CDR.

Use std::vector.
 
S

Slain

Slain said:
I am using sizeof to doa memcpy but run into probs because of the
following problem.
static char arr1[] = {0x01,0x00,0x03,0x04};
static char arr2[] = {0x00,0x03,0x04,0x06,0x07};
const char* string_CDR[8]= {arr1,arr2};
cout<<"The size of is " <<sizeof(arr1)<<" and "<<sizeof(arr2)  <<endl;
cout<<"The size of is " <<sizeof(string_CDR[0])<<" and
"<<sizeof(string_CDR[1])  <<endl;

This line outputs sizeof(char*) twice.


This is messing up my memcpy in the main function as the sizeof is not
what I am expecting. Can anyone point out the problem.

There is no way to calculate the size of the arrays pointed to in
string_CDR.

Use std::vector.

DOn't have STL's. Is there any other way to define either string_CDR
or some kind of casting?
 
I

Ian Collins

Slain said:
Slain said:
I am using sizeof to doa memcpy but run into probs because of the
following problem.
static char arr1[] = {0x01,0x00,0x03,0x04};
static char arr2[] = {0x00,0x03,0x04,0x06,0x07};
const char* string_CDR[8]= {arr1,arr2};
cout<<"The size of is " <<sizeof(arr1)<<" and "<<sizeof(arr2) <<endl;
cout<<"The size of is " <<sizeof(string_CDR[0])<<" and
"<<sizeof(string_CDR[1]) <<endl;
This line outputs sizeof(char*) twice.
This is messing up my memcpy in the main function as the sizeof is not
what I am expecting. Can anyone point out the problem.
There is no way to calculate the size of the arrays pointed to in
string_CDR.

Use std::vector.
*PLease* don't quote signatures.
DOn't have STL's. Is there any other way to define either string_CDR
or some kind of casting?

Then you have to save the length of the arrays somewhere. Why not use a
small struct (with the pointer to the array and its size as members) as
the type for string_CDR?
 
S

Shobhit Gupta

You could do something like this: (I chose to stick closely with your
problem, otherwise you would be better off with dynamically allocated
arrays)



struct MyArray{
public:
char *arr;
unsigned int size;
MyArray() {}
void initialize(char tempArr[], int tempSize){
arr = tempArr;
size = tempSize;
}


};

#define initialize(arg) initialize(arg, sizeof(arg))

int main()
{
static char arr1[] = {0x01,0x00,0x03,0x04};
static char arr2[] = {0x00,0x03,0x04,0x06,0x07};

MyArray m1, m2;
m1.initialize(arr1);
m2.initialize(arr2);
const MyArray string_CDR[8]= {m1,m2};

cout<<"The size of is " << string_CDR[0].size<<" and "<<
string_CDR[1].size <<endl;
return 0;
}

-Shobhit
http://shobhitgupta12.blogspot.com
 
I

Ian Collins

[please retain context and attributions]

Shobhit said:
You could do something like this: (I chose to stick closely with your
problem, otherwise you would be better off with dynamically allocated
arrays)

struct MyArray{
public:
char *arr;
unsigned int size;
MyArray() {}
void initialize(char tempArr[], int tempSize){
arr = tempArr;
size = tempSize;
}
Why not do this in the constructor?
};

#define initialize(arg) initialize(arg, sizeof(arg))
You never use this.
 
V

Victor Bazarov

Ian said:
[please retain context and attributions]

Shobhit said:
You could do something like this: (I chose to stick closely with
your problem, otherwise you would be better off with dynamically
allocated arrays)

struct MyArray{
public:
char *arr;
unsigned int size;
MyArray() {}
void initialize(char tempArr[], int tempSize){
arr = tempArr;
size = tempSize;
}
Why not do this in the constructor?

Would probably work like this:


V
 
S

Shobhit Gupta

Why not do this in the constructor?
You never use this.

I thought of making code inside main() more readable for Slain guy
( Manny )

So I thought of having only 1 argument, by which both array and its
size could be set.
Thats why I used a #define.
And #defining a constructor didn't seem like a good idea, so I created
an initialize().

I just wanted to float the idea of grouping "The array" & "its size"
in a struct; and as I said earlier there could be definitely be much
more better way of implementing it.
 
I

Ian Collins

[*please* don't snip attributions!]

Shobhit said:
Ian Collins wrote:

I thought of making code inside main() more readable for Slain guy
( Manny )

So I thought of having only 1 argument, by which both array and its
size could be set.
Thats why I used a #define.
And #defining a constructor didn't seem like a good idea, so I created
an initialize().

I just wanted to float the idea of grouping "The array" & "its size"
in a struct; and as I said earlier there could be definitely be much
more better way of implementing it.
OK. The simple template approach hinted at by Victor has the advantage
of not adding a size member to the struct.
 
J

James Kanze

Slain said:
I am using sizeof to doa memcpy but run into probs because of the
following problem.
static char arr1[] = {0x01,0x00,0x03,0x04};
static char arr2[] = {0x00,0x03,0x04,0x06,0x07};
const char* string_CDR[8]= {arr1,arr2};
cout<<"The size of is " <<sizeof(arr1)<<" and "<<sizeof(arr2) <<endl;
cout<<"The size of is " <<sizeof(string_CDR[0])<<" and
"<<sizeof(string_CDR[1]) <<endl;
This line outputs sizeof(char*) twice.
This is messing up my memcpy in the main function as the
sizeof is not what I am expecting. Can anyone point out
the problem.
There is no way to calculate the size of the arrays pointed to in
string_CDR.
Use std::vector.
DOn't have STL's.

Why not? The alternative is reimplementing all of the logic it
contains manually. You seem to want the behavoir of a
container. So you really only have two choices: use the
standard containers, or implement one yourself.
 
M

Micah Cowan

James Kanze said:
Slain wrote:
I am using sizeof to doa memcpy but run into probs because of the
following problem.
static char arr1[] = {0x01,0x00,0x03,0x04};
static char arr2[] = {0x00,0x03,0x04,0x06,0x07};
const char* string_CDR[8]= {arr1,arr2};
cout<<"The size of is " <<sizeof(arr1)<<" and "<<sizeof(arr2) <<endl;
cout<<"The size of is " <<sizeof(string_CDR[0])<<" and
"<<sizeof(string_CDR[1]) <<endl;
This line outputs sizeof(char*) twice.
This is messing up my memcpy in the main function as the
sizeof is not what I am expecting. Can anyone point out
the problem.
There is no way to calculate the size of the arrays pointed to in
string_CDR.
Use std::vector.
DOn't have STL's.

Why not? The alternative is reimplementing all of the logic it
contains manually.

Might be using a freestanding implementation.
You seem to want the behavoir of a
container. So you really only have two choices: use the
standard containers, or implement one yourself.

Or learn to use arrays properly.

I suppose one could take an approach similar to:

struct Sized_array {
const char *ary;
size_t size;
};

Sized_aray string_CDR[] = {
{arr1, sizeof arr1}, {arr2, sizeof arr2}
};
 
J

James Kanze

Might be using a freestanding implementation.

In which case, he might have to do some additional work (which
probably requires a higher level of experience as well).
Or learn to use arrays properly.

You mean, learn enough to be able to implement std::vector
oneself.

Sooner or later, I think that most C++ programmers probably
should get to that point. (It's not as difficult as it seems.)
But it's certainly not where one should start.
 

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

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top