1

I am having some issues with a memmove in C. As far as I can tell, both locations are valid memory addresses, I am able to print the contents of both memory locations before I perform the memmove, however as soon as I attempt to memmove, the function crashes with a SIGSEGV.

Here is the code in question:

std::cout << page[0] << "\n";
std::cout <<page[1] << "\n" ;
std::cout << cache[index][cache_itr] << "\n";

memmove(cache[index][cache_itr],page,sizeof(float));

//These lines never print.
std::cout << cache[index][cache_itr] << "\n";
std::cout << cache[index][cache_itr] << "\n";

The cache is declared as a float** , and page is a float* which contains a variable size array of floats. The size of the array being an issue is unlikely however, as I cannot even get it to copy over a single float, despite the fact that page undoubtedly contains a float, as evidenced by a valid print out of both page[0] and page[1].

Cache[0] and Cache[1] (index will only ever be 0 or 1, this is also confirmed) are both pointers to a previously calloc-ed cache of float** within a struct object. This can be seen here:

float** cache[2];
cache[0] = queryOb->cache;
cache[1] = cache[0] + sizeOfCache/2;

Where sizeOfCache is initially used to declare how many blocks of size float* we want to calloc for queryOb->cache.

I can't figure out for the life of me why this memmove is producing a sigsegv, I believe it might be to do with the memmove from a float* to a float** however I thought using the two indices on the memmove would cause it to start copying to the correct location.

Any help would be much appreciated!

paged90
  • 21
  • 3

1 Answers1

2

The definition of Memmove is

void* memmove( void* destination, const void* source, size_t num );

and you are calling function as

memmove(cache[index][cache_itr], page, sizeof(float);

Just noticed that cache[index][cache_itr] is the value of the array element present at lets say (for index =2,cache_itr =3) location cache[2][3]

You need to provide a pointer not value. Changing it to

 memmove(cache[index], page, sizeof(float));

will make sense.

Also, compiler should throw warning while compiling this code for "makes pointer from integer without a cast".

David G
  • 94,763
  • 41
  • 167
  • 253
uni
  • 117
  • 4