LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 06-09-2005, 02:36 PM   #1
benobi
LQ Newbie
 
Registered: May 2004
Posts: 18

Rep: Reputation: 0
Need help: Seg fault, Memcpy, and dynamically allocated arrays


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?

Code:
#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.

Last edited by benobi; 06-09-2005 at 02:46 PM.
 
Old 06-09-2005, 03:50 PM   #2
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
Hello,

First buffer doesn't point to allocated memory. It points 'somewhere'. You must allocate space first, memcpy doesn't do that.

So:
buffer = new char(100); // c++ style
or
buffer = (char*)malloc(100*sizeof(char)); // c style

(edited this, I don't know why i was under impression it used printf )
Secondly, . By copying only the 5 charachters, you don't know what the 6th will be.
So you should print only what you copy.

Thrid: memcpy needs pointers to memory locations. Not pointers to pointers to memory locations. So you should do:
memcpy(buffer,s,5);

You should take it a little easier if you are new to c++. Maybe it would be a good idea to read one or two more tutorials.

;

Last edited by vladmihaisima; 06-09-2005 at 03:59 PM.
 
Old 06-09-2005, 03:55 PM   #3
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
Hi,

You are actually copying the address of s into the address of buffer, not the contents of s. The reason for the seg fault is that when the program hits the delete [] buffer I believe that it is trying to free up memory that does not belong to the heap. The reason for printing out everything is because after the memcpy the buffer pointer points to the original data text.

I think what you are trying to do is:

Code:
#include <iostream>

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

int main()
{
    char * s = "hello world";
    char * buffer = new char[16]; // Arbitrary length - big enough to hold all data
    int bufferSize = 15;

    memcpy(&buffer[0], &s[0], 5);

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

    delete buffer;

    return 0;
}
 
Old 06-09-2005, 10:58 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,093

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
Someone should have mentioned that you copy 5 chars to an arbitrary place in mem (ie into buffer var), but you are trying to print/cout 15(!) ie bufferSize chars....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
In C, using qsort for dynamically allocated array ntmsz Programming 7 08-23-2005 10:33 AM
how to create device node for dynamically allocated major number appas Programming 5 11-01-2004 09:37 AM
C seg fault drigz Programming 5 10-01-2004 03:35 PM
xawtv seg fault lackluster Linux - Hardware 8 08-18-2003 12:12 AM
gnome_app_create_menus seg fault in RH 7.3 Castro Programming 0 05-30-2003 08:46 PM


All times are GMT -5. The time now is 08:11 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration