Solved

memcpy problem

Posted on 2006-11-10
4
767 Views
Last Modified: 2008-02-01
Hello,

Below is a simple program. I have a function to which I am passing a char*.
Then within the function I do a memcpy(). Then I print the copied memmory
element by element.

I see teh first 4 locations are comming out all right. The next 4 elments are junk.

The
     cout << chr << endl;
statement immediately inside the mem_copy function shows the input char* as ABDYF6MX&#9492; &#8597;
(with some garbage at the end). But it still has the first 8 characters I am passing in. Why is
that the memcpy() does not  work properly?

#include <iostream>

using namespace std;

void mem_copy(char *chr)
{
    cout << chr << endl;                //This prints  ABDYF6MX&#9492; &#8597;
    char *cpyDat =  new char[8];
   
    memcpy(cpyDat, chr, sizeof(chr));
   
    cout << "size : " << sizeof(chr) << endl;

    cout << cpyDat[0] << endl;          //this prints 'A'   (Good Data OK)
    cout << cpyDat[1] << endl;          //this prints 'B'   (Good Data OK)
    cout << cpyDat[2] << endl;          //this prints 'D'   (Good Data OK)
    cout << cpyDat[3] << endl;          //this prints 'Y'   (Good Data OK)
    cout << cpyDat[4] << endl;          //this prints '='   (garbage) why?
    cout << cpyDat[5] << endl;          //this prints '='   (garbage) why?
    cout << cpyDat[6] << endl;          //this prints '='   (garbage) why?
    cout << cpyDat[7] << endl;          //this prints '='   (garbage) why?
}

int main()
{
  char chr[] = {'A','B','D', 'Y', 'F' , '6', 'M', 'X'};

  cout << "Size Before : " << sizeof (chr) << endl;

  mem_copy(chr);

  return 0;
}
 
0
Comment
Question by:prain
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 17919036
chr in mem_copy is a pointer, so sizeof(chr) is the size of a pointer
chr in main is an array of 8 characters, so sizeof(chr) is the size of  array of 8 characters
0
 
LVL 84

Accepted Solution

by:
ozo earned 75 total points
ID: 17919187
void mem_copy(char *chr,unsigned int size)
{
    cout << chr << endl;                //This prints  ABDYF6MX&#9492; &#8597;
    char *cpyDat =  new char[size];

    memcpy(cpyDat, chr, size);

    cout << "size : " << sizeof(chr) << endl;

    cout << cpyDat[0] << endl;          //this prints 'A'   (Good Data OK)
    cout << cpyDat[1] << endl;          //this prints 'B'   (Good Data OK)
    cout << cpyDat[2] << endl;          //this prints 'D'   (Good Data OK)
    cout << cpyDat[3] << endl;          //this prints 'Y'   (Good Data OK)
    cout << cpyDat[4] << endl;          //this prints '='   (garbage) why?
    cout << cpyDat[5] << endl;          //this prints '='   (garbage) why?
    cout << cpyDat[6] << endl;          //this prints '='   (garbage) why?
    cout << cpyDat[7] << endl;          //this prints '='   (garbage) why?
}

int main()
{
  char chr[] = {'A','B','D', 'Y', 'F' , '6', 'M', 'X'};

  cout << "Size Before : " << sizeof (chr) << endl;

  mem_copy(chr,sizeof(chr));

  return 0;
}
0
 
LVL 22

Expert Comment

by:grg99
ID: 17921096
You can't find out the size of a char * in the called function.  Pass the size as a parameter.

0
 

Author Comment

by:prain
ID: 17921497
Great. Thanks.  Got the idea.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question