13 Replies - 1331 Views - Last Post: 02 October 2012 - 04:47 PM Rate Topic: -----

#1 DevonZ  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 73
  • Joined: 04-August 12

strcpy and struct, runtime error

Posted 01 October 2012 - 07:12 PM

I'm trying to copy a char array from a struct to a temporary variable. However, while my code is compiling, I'm receiving runtime errors.

Struct:
struct Record
{
	char name[50];
	int iq;
	float salary;
};


Code Causing the Error:
	char *pivotFName;
	Record pivot = db.getRecord((left + right) / 2);

	strcpy(pivotFName, pivot.name);
	pivotFName = strtok(pivotFName, " ");


Edit: More specifically, strcpy() is causing the error.

Quote

An unhandled exception of type 'System.AccessViolationException' occurred in Project1.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

This post has been edited by DevonZ: 01 October 2012 - 07:15 PM


Is This A Good Question/Topic? 0
  • +

Replies To: strcpy and struct, runtime error

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3461
  • View blog
  • Posts: 10,669
  • Joined: 05-May 12

Re: strcpy and struct, runtime error

Posted 01 October 2012 - 07:37 PM

Wow! Your code must really be broken to make the compiler run into a runtime error while you are just compiling, and you haven't even run it yet.

Kidding aside, though. Is there any particular reason why you are compiling with the /CLR flag turned on? Are you writing a managed application?

The exception is due to you trying to write into the memory pointed at by pivotFName, but you didn't allocate any memory for it.

Perhaps declaring pivotFName as a std::string would be better than using a C-string.
Was This Post Helpful? 0
  • +
  • -

#3 DevonZ  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 73
  • Joined: 04-August 12

Re: strcpy and struct, runtime error

Posted 01 October 2012 - 07:38 PM

Haha, I didn't mean it gave there error "while" it is compiling. I meant, it compiles successfully, and then gives the error! Stop pickin' on meh! QQ lol

Erm, because I don't know what /CLR even is at this point and it is apparently on by default.
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: strcpy and struct, runtime error

Posted 01 October 2012 - 07:56 PM

pivotFName is a pointer. A pointer's "purpose" is to point somewhere in memory that has been allocated. pivotFName doesn't point to anything!, Well actually it points to some garbage memory location that your program probably doesn't own, therefore when you try writing to that location you get a seg fault.

With all of that said, you don't really need a pointer, or any other memory to accomplish this.

example
//char *pivotFName; Kinda unnecessary
Record pivot = db.getRecord((left + right) / 2);

//strcpy(pivotFName, pivot.name); Dont need this since we ditched the pointer earlier
char *temp = strtok(pivot.name, " "); //temp points to somewhere in your pivot.name memory
strcpy(pivot.name, temp); //Now copy the temp string back into the array over writing the original data


This post has been edited by jjl: 01 October 2012 - 07:56 PM

Was This Post Helpful? 0
  • +
  • -

#5 DevonZ  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 73
  • Joined: 04-August 12

Re: strcpy and struct, runtime error

Posted 01 October 2012 - 09:20 PM

jjl, thanks for the explanation. However, you're code does exactly what I don't want to do. I don't want to alter pivot.name (it should keep both first and last names), I'm trying to isolate the first name into a temp var (pivotFName) to compare with another name which will be used in a quicksort algorithm to sort the names alphabetically. :)

I ended up solving the problem with pivotFName = (char*) malloc(50);

This post has been edited by DevonZ: 01 October 2012 - 09:23 PM

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3461
  • View blog
  • Posts: 10,669
  • Joined: 05-May 12

Re: strcpy and struct, runtime error

Posted 01 October 2012 - 10:32 PM

You are writing in C++. You should have used either:
std::string pivotFName;
pivotFName = pivot.name;


or
unique_ptr<char> pivotFName = new char[pivot.name.size() + 1];
strcpy(pivotFName, pivot.name.c_str());


or if really know what what you are doing:
char pivotFName[FIRST_NAME_MAX_LEN];
assert(pivot.name.size() + 1 < sizeof(pivotFName));
strcpy(pivotFName, pivot.name.c_str());


Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: strcpy and struct, runtime error

Posted 01 October 2012 - 10:38 PM

Quote

You are writing in C++. You should have used either:

What makes you think this? just out of curiosity that I missed something.

Quote

I ended up solving the problem with pivotFName = (char*) malloc(50);

Eh, not really the best idea. You should only malloc space on the run time heap when the number of bytes that you need to allocate is unknown. However, you know the number of bytes you need ... it's 50!. Just declare pivotFName as a char array with 50 elements.
Was This Post Helpful? 1
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3461
  • View blog
  • Posts: 10,669
  • Joined: 05-May 12

Re: strcpy and struct, runtime error

Posted 01 October 2012 - 10:47 PM

View Postjjl, on 01 October 2012 - 10:38 PM, said:

Quote

You are writing in C++. You should have used either:

What makes you think this? just out of curiosity that I missed something.


The exception he reported in post #1 was:

Quote

An unhandled exception of type 'System.AccessViolationException' occurred in Project1.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.


This error happens only if you build a C++ project with the /CLR option. I don't think you can combine the /CLR option with /TC anymore.

If he wasn't building with the /CLR, it should have been something like runtime exception 0xc0000005 or some such.

Additionally this hints strongly at C++:
Record pivot = db.getRecord((left + right) / 2);


Since the C that I grew up with didn't support methods on structures, assuming that getRecord() is a method, and db is a struct.

This post has been edited by Skydiver: 01 October 2012 - 10:47 PM

Was This Post Helpful? 0
  • +
  • -

#9 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1622
  • View blog
  • Posts: 3,080
  • Joined: 30-May 10

Re: strcpy and struct, runtime error

Posted 01 October 2012 - 10:52 PM

> However, you're code does exactly what I don't want to do.
> I don't want to alter pivot.name
It's a bit late for that, strtok() will have already trashed it.

Perhaps this, which will extract the first name, up to the first space.
char fname[50]; // hint, make 50 a const somewhere
sscanf(fname,"%s",pivot.name);


Was This Post Helpful? 0
  • +
  • -

#10 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: strcpy and struct, runtime error

Posted 01 October 2012 - 10:53 PM

Quote

Since the C that I grew up with didn't support methods on structures, assuming that getRecord() is a method, and db is a struct.


Well Duh, I completely missed that for some reason :sleep1:
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3461
  • View blog
  • Posts: 10,669
  • Joined: 05-May 12

Re: strcpy and struct, runtime error

Posted 01 October 2012 - 10:57 PM

View Postjjl, on 01 October 2012 - 10:38 PM, said:

Quote

I ended up solving the problem with pivotFName = (char*) malloc(50);

Eh, not really the best idea. You should only malloc space on the run time heap when the number of bytes that you need to allocate is unknown. However, you know the number of bytes you need ... it's 50!. Just declare pivotFName as a char array with 50 elements.


+1.

Or let the compiler do all the hardwork of keeping track of the size needed:
char pivotFName[sizeof(((Record *)(0))->name)];


Was This Post Helpful? 0
  • +
  • -

#12 DevonZ  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 73
  • Joined: 04-August 12

Re: strcpy and struct, runtime error

Posted 01 October 2012 - 10:57 PM

View PostSalem_c, on 02 October 2012 - 01:52 AM, said:

It's a bit late for that, strtok() will have already trashed it.


That's why I copied pivot.name to pivotFName first, then used strtok on pivotFName instead. :)
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3461
  • View blog
  • Posts: 10,669
  • Joined: 05-May 12

Re: strcpy and struct, runtime error

Posted 01 October 2012 - 11:52 PM

I hope that realize that not all cultures write their given name before their family name.
Was This Post Helpful? 0
  • +
  • -

#14 DevonZ  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 73
  • Joined: 04-August 12

Re: strcpy and struct, runtime error

Posted 02 October 2012 - 04:47 PM

And I would hope that my professor doesn't expect this application to be used in any practical setting... :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1