Solved

memcpy problem

Posted on 2006-11-10
4
766 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Building cUrl in Windows v7.43.0 4 28
Issue accessing member variable of atomic class 16 110
C language IDE – Compilers installation 14 66
max float value 3 20
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

947 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now