27

I'm trying to build an open source c++ library with gcc and eclipse. But I get this error ‘memcpy’ was not declared in this scope

I've try to include memory.h (and string.h) and eclipse find the function if I click "open declaration" but gcc give me the error.

How can I do?

#include <algorithm>
#include <memory.h>

namespace rosic
{
   //etc etc
template <class T>
  void circularShift(T *buffer, int length, int numPositions)
  {
    int na = abs(numPositions);
    while( na > length )
      na -=length;
    T *tmp = new T[na];
    if( numPositions < 0 )
    {

      memcpy(  tmp,                buffer,              na*sizeof(T));
      memmove( buffer,            &buffer[na], (length-na)*sizeof(T));
      memcpy( &buffer[length-na],  tmp,                 na*sizeof(T));
    }
    else if( numPositions > 0 )
    {
      memcpy(  tmp,        &buffer[length-na],          na*sizeof(T));
      memmove(&buffer[na],  buffer,            (length-na)*sizeof(T));
      memcpy(  buffer,      tmp,                        na*sizeof(T));
    }
    delete[] tmp;
  }

//etc etc
}

I get error on each memcpy and memmove function.

Andrea993
  • 663
  • 1
  • 10
  • 23
  • 1
    Better to add your code to the post - it will help us help you – NirMH Jul 20 '14 at 12:27
  • 2
    Well, to start with you will need to provide more context on the options with which you invoke gcc and what the exact error is (filename, extract of the code not compiling, copy/pasting the exact error). Normally `string.h` is bundled with gcc and does not require any additional option, so there is something fishy here. – Matthieu M. Jul 20 '14 at 12:28
  • 1
    My guess is you mistyped `#include ` or put it too low in your file. Show us some [minimal example code that demonstrates the problem](http://stackoverflow.com/help/mcve). – user2357112 Jul 20 '14 at 12:30
  • Generally, if you're compiling C++ code, you should be using g++ and not gcc. There are a few subtle differences. [This answer](http://stackoverflow.com/a/173007/2548721) shows a small comparison between the two. Of note is the third point. Give it a try and let us know. Otherwise, post your code. – WGS Jul 20 '14 at 12:32
  • 1
    Have you tried including `cstring` and adding `using namespace std;` (if this is a cpp file) (http://en.cppreference.com/w/cpp/string/byte/memcpy)? – BartoszKP Jul 20 '14 at 12:53
  • Yes I've already try and this is a .h file – Andrea993 Jul 20 '14 at 12:54
  • The standard library contains this algorithm; it's called [`rotate`](http://en.cppreference.com/w/cpp/algorithm/rotate). – NicholasM Jul 20 '14 at 14:40
  • OMG, why are you moving data? You should be changing indices or pointers instead. Try declaring an array and using the formula `next_index = (previous_index + 1) % ARRAY_CAPACITY;`. Much faster, less code than copying each element. – Thomas Matthews Jul 20 '14 at 19:06
  • 1
    Possible duplicate of [What header should I include for memcpy and realloc?](https://stackoverflow.com/questions/2283712/what-header-should-i-include-for-memcpy-and-realloc) – jww Nov 30 '17 at 06:10

2 Answers2

34

You have to either put

using namespace std;

to the other namespace or you do this at every memcpy or memmove:

[...]

std::memcpy(  tmp,                buffer,              na*sizeof(T));

[...]

in your code the compiler doesnt know where to look for the definition of that function. If you use the namespace it knows where to find the function.

Furthermore dont forget to include the header for the memcpy function:

#include <cstring>
Flocke
  • 1,004
  • 17
  • 23
  • 3
    Don't forget to mention the correct `#include` statement. – πάντα ῥεῖ Jul 20 '14 at 13:05
  • 4
    Should be [`#include `](http://en.cppreference.com/w/cpp/string/byte/memcpy) – πάντα ῥεῖ Jul 20 '14 at 13:12
  • 2
    OP mentions that they've tried it. Also, you should not put `using namespace` statements in headers. – BartoszKP Jul 20 '14 at 13:44
  • @BartoszKP: i think you should never have namespaces in headers... if you are using/want to use the header in some other file you will have the namespace, which you probably wont reconize (e.g. deeply nested header inclusion) – Flocke Jun 08 '15 at 08:20
  • What if I use `gcc`? How can I access `memcpy`? – Royi Jul 18 '17 at 16:40
  • @Royi: `#include void *memcpy(void *dest, const void *src, size_t n);` – Flocke Jul 22 '17 at 10:46
  • @Flocke, Thank You. It's funny it is needed in GCC yet not in VS. – Royi Jul 22 '17 at 10:51
  • I tried using std::, but I got response that memcpy_s is not a member of std (I'm using memcpy with _s though (so if someone can help me with memcpy_s that would be nice).) – skittlebiz Nov 04 '21 at 15:46
  • skittlebiz memcpy_s is added since C++11 dont know if you are using something older maybe? Are you compiling in gcc oder visual studio compiler or what? – Flocke Nov 05 '21 at 08:47
0

There's also one possibility, when you do CP and in some platforms, such as USACO, it doesn't allow you to use memcpy because it's an unchecked operation in C++, which could produce serious memory errors and even potential attacks.

Dwa
  • 593
  • 4
  • 13