Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

memcpy problem

Posted on 2006-11-10
4
Medium Priority
?
774 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 300 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.
Suggested Courses

688 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