Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

memcpy problem

Posted on 2006-11-10
4
Medium Priority
?
775 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 85

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 85

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

916 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