help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: undefined reference link errors


From: Thomas Maeder
Subject: Re: undefined reference link errors
Date: Thu, 17 Mar 2005 19:18:50 +0100
User-agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux)

"Srini" <srinivasa.s@gmail.com> writes:

> -- buffer.h --
> #ifndef _BUFFER_H_
> #define _BUFFER_H_

This means that your program, should it compile, has undefined
behavior. Name starting with _[A-Z] are reserved to the C/C++
implementation. We mere users must not declare such names in our code.


> #include "typedefs.h"
>
> // Place holder ONLY!!
> #define BLOCK_SIZE 256

Even as a placeholder, I'd write

enum
{
  BLOCK_SIZE = 256
};

to make it behave.


> class Buffer
> {
>    public:
>      Buffer();
>      Buffer(ULong fillPattern);
>      ~Buffer();
>
>    private:
>      UInt length;
>      char buf[BLOCK_SIZE];  // Need to define BLOCK_SIZE somewhere
> appropriate
> };
>
> #endif
>
>
> -- buffer.cpp --
> #include "buffer.h"
> #include <cstring>
>
> using std::memcpy;
>
> #ifdef DEBUG
> #include <iostream>

#include <ostream>

> #endif
>
> /*************** Construction & Destruction *************/
>
> inline Buffer::Buffer()
>   : length(BLOCK_SIZE)
> { }
>
> Buffer::Buffer(ULong fillPattern /*, UInt len */)
>   : length(BLOCK_SIZE /*len */)
> {
>    for(int i = 0; i < length; i += sizeof(ULong))
>    {
>      memcpy( buf+i , &fillPattern , sizeof(ULong) );

In general, this will write past the end of buf.

>    }
>    #ifdef DEBUG
>    for(int i = 0 ; i < 2*sizeof(ULong) ; i += 4)
>    {
>      ULong *tmp = reinterpret_cast<ULong *>(buf+i);

In general, this will read past the end of buf. You may also run into
alignment problems.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]