Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

memcpy & dynamically allocated array


  • Please log in to reply

#1
benobi

benobi

    New Member

  • Member
  • Pip
  • 1 posts
I have the following and it seg faults at the line: delete [] buffer; i believe. can anyone tell me why? Also the output is: "hello world" followed by a space and some rectangle which i'm guessing is some non-standard ascii symbol. I thought memcpy's only supposed to copy over 5 bytes from string s?
#include <iostream>

using std::cout;
using std::endl;

int main()
{
   char * s = "hello world";
   char * buffer;
   int bufferSize = 15;

   memcpy(&buffer, &s, 5);

   for (int i = 0; i < bufferSize; i++)
   {
      cout << buffer[i];
   }
   cout << endl;

   delete [] buffer;

   return 0;
}

If I comment out the memcpy line, it doesn't seg fault.

Edited by benobi, 09 June 2005 - 01:51 PM.

  • 0

Advertisements


#2
amieln

amieln

    New Member

  • Member
  • Pip
  • 4 posts
first allocate memmory to the buffer

like this way:

buffer=new int[15];

you can't delete memmory that you didn't

allocated!!!!!
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP