1

I have the following code, which takes an unsorted list of songs and artists and sorts and displays them.

int main()
{
   SongList totalList; // has a public 2d array 'unsortedSongs' variable
   char songs[100][80] =
   {
      {"David Bowie 'Ziggy Stardust'",},
      {"Smokey Robinson 'You've Really Got A Hold On Me'",},
      {"Carole King 'You've Got A Friend'",},
      // many more songs here totaling to 100
      {"Joni Mitchel 'A Case Of You'",},
      {"Prince 'Kiss'"}

   };
   memcpy(&totalList.unsortedSongs, &songs, sizeof(songs)); // this causes a segmentation fault
   totalList.displaySortedList();
   return 0;
}

I took the code for memcpy almost directly off of the example here, so I am confused as to why this doesn't work. Can someone help me fix this?

edit:

this is the initialization of SongList

class SongList
{
public:
   char unsortedSongs[100][80];
public:
   void displaySortedList();
   void sortList();
   string rearrange(char[]);
   string getSongsForArtist(int*);
};
i .
  • 475
  • 2
  • 10
  • 22
  • Can you show us how `SongList` is declared and implemented? I am guessing `unsortedSongs` is not properly initialized. – Shafik Yaghmour Jun 18 '13 at 00:06
  • Are you sure you need `&songs` and not just `songs`? (perhaps the same for `unsortedSongs`, but we don't know that... – John3136 Jun 18 '13 at 00:08
  • I don't really think there is a segmentation fault in the line with `memcpy`: [http://ideone.com/lJKboC](http://ideone.com/lJKboC). – newbie Jun 18 '13 at 00:16
  • Although nothing I said was technically incorrect after coming back to this I don't see how it answers the underlying problem. Either you had a size mismatch that was corrected when you copied and pasted or the issue was really in `displaySortedList`. It seems like I can not delete my answer since it is accepted so can you add the code for `displaySortedList`. – Shafik Yaghmour Jun 18 '13 at 02:25

3 Answers3

4

This line:

memcpy(&totalList.unsortedSongs, &songs, sizeof(songs));

should be:

memcpy(totalList.unsortedSongs, songs, sizeof(songs));

since both songs and totalList.unsortedSongs will decay to pointers which is analogus to the first example in the reference you cited:

memcpy ( person.name, myname, strlen(myname)+1 );
Community
  • 1
  • 1
Shafik Yaghmour
  • 143,425
  • 33
  • 399
  • 682
  • @0x499602D2 Aplogies, I was distracted but I updated the answer. – Shafik Yaghmour Jun 18 '13 at 00:18
  • I don't understand why passing an array to `memcpy` via the unary `&` operator causes a segmentation fault. [Here is](http://stackoverflow.com/questions/2893911/address-of-an-array-address-of-being-ignored-be-gcc) a complete explanation why this should work. – newbie Jun 18 '13 at 00:59
1

http://www.cplusplus.com/reference/cstring/memcpy/

Memcpy expects the source and destination variables to be pointers ( void * )

totalList.unsortedSongs is a pointer.

When you write &totalList.unsortedSongs you are asking for the address of the pointer. A bit like "the pointer to the pointer"... See here: http://www.cplusplus.com/doc/tutorial/pointers/

tim k
  • 152
  • 1
  • 9
0

I just compiled your code and it works fine.

However, I find your initializer list rather curious. While it works, it makes me think you actually want to define an array of an array of char[80], not just an array of char[80].

So I think your display routine might be wrong and your debugger just not showing you the real line where things go wrong, because of optimization or whatnot.

FRob
  • 3,393
  • 1
  • 25
  • 36