473,387 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

problem with pointer arithmetic

Hello,
I don't know why I've to increment the cahr pointer by 12 instead of 9 in
the code below.
regards, Oliver

class message
{
private:
char buffer_[100];

struct header
{
char name[3]; // should be 3 bytes
unsigned short version; // should be 2 bytes
unsigned long data_size; // should by 4 bytes
};

public:
message()
: buffer_()
{ ::bzero( buffer_, sizeof buffer_); }

message( std::string const& data)
: buffer_()
{
::bzero( buffer_, sizeof buffer_);

void * p = buffer_;
header * h = reinterpret_cast< header * >( p);
::strncpy( h->name, "tcp", 3);
h->version = 1;
h->data_size = data.size();
char * p_ = buffer_;
p_ += 12; // <<== why 12 and not 9
::strncpy( p_, data.c_str(), h->data_size);
}

const std::string name() const
{
void const* p = buffer_;
header const* h = reinterpret_cast< header const* >( p);
return std::string( h->name, 3);
}

unsigned short version() const
{
void const* p = buffer_;
header const* h = reinterpret_cast< header const* >( p);
return h->version;
}

unsigned long data_size() const
{
void const* p = buffer_;
header const* h = reinterpret_cast< header const* >( p);
return h->data_size;
}

const std::string data() const
{
char const* p = buffer_;
p += 12; // <<== why 12 and not 9
return std::string( p, data_size() );
}

net::mutable_buffer as_mutable_buffer()
{ return net::make_mutable_buffer( buffer_); }

net::const_buffer as_const_buffer()
{ return net::make_const_buffer( buffer_); }
};

Feb 23 '07 #1
3 1570
Oliver Kowalke wrote:
Hello,
I don't know why I've to increment the cahr pointer by 12 instead of 9 in
the code below.
Search the archive for "padding bytes"

--
Ian Collins.
Feb 23 '07 #2
On Feb 23, 1:20 pm, Oliver Kowalke <oliver.kowa...@gmx.dewrote:
Hello,
I don't know why I've to increment the cahr pointer by 12 instead of 9 in
the code below.
regards, Oliver
Why are you manually counting up the data size? Why not change by
sizeof(header)?

Feb 23 '07 #3

Oliver Kowalke wrote:
>
message()
: buffer_()
You can do not call default constructor "buffer_()" explicitly.
{ ::bzero( buffer_, sizeof buffer_); }
If you just want to terminate asciiz string, then instead of non-standard
"bzero" you can write "buffer_[0]=0;"

--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Feb 24 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: ceo | last post by:
Hi, Following is a program that doesn't give the expected output, not sure what's wrong here. I'm adding the size of derived class to the base class pointer to access the next element in the...
28
by: Davy | last post by:
Hi all, I found char x={"my"}; can be compiled. But char x; x={"my"}; can not be compiled.
22
by: Alex Fraser | last post by:
From searching Google Groups, I understand that void pointer arithmetic is a constraint violation, which is understandable. However, generic functions like qsort() and bsearch() must in essence do...
18
by: Manohar S | last post by:
It is a problem related to printf, and buffering to the printf statements Look through this program. main() { int pid; int loop,max=3; for(loop = 0; loop <max;loop++) { pid = fork();
37
by: Patrik Huber | last post by:
Hello! I got the following Code in Assembler (NASM), which prints out "5" in realmode: mov ax, 0xB800 mov es, ax mov byte , '5' I want to do the same in gcc now, but I'm stuck. GCC...
6
by: Francois Grieu | last post by:
Are these programs correct ? #include <stdio.h> unsigned char a = {1,2}; int main(void) { unsigned char j; for(j=1; j<=2; ++j) printf("%u\n", *( a+j-1 )); return 0; }
46
by: Albert | last post by:
Why doesn't: #include <stdio.h> void reverse(char, int); main() { char s;
7
by: barikat | last post by:
int a; int *Ptr1, *Ptr2; Ptr1 = a; Ptr1++; Ptr2 = a; printf("Ptr1 : %p\n", Ptr1); printf("Ptr2 : %p\n\n", Ptr2);
19
by: =?iso-8859-1?b?VG9t4XMg0yBoyWlsaWRoZQ==?= | last post by:
Coming originally from C++, I used to do the likes of the following, using a pointer in a conditional: void Func(int *p) { if (p) { *p++ = 7; *p++ = 8;
25
by: Ioannis Vranos | last post by:
Are the following codes guaranteed to work always? 1. #include <iostream> inline void some_func(int *p, const std::size_t SIZE) {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.