Solved

memcpy problem

Posted on 2006-11-10
4
764 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Great. Thanks.  Got the idea.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

762 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

11 Experts available now in Live!

Get 1:1 Help Now