It's UWAweek 48

help2002

This forum is provided to promote discussion amongst students enrolled in CITS2002 Systems Programming.
Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!"  followup message.

Displaying the 5 articles in this topic
Showing 5 of 919 articles.
Currently 4 other people reading this forum.


 UWA week 39 (2nd semester, week 9) ↓
SVG not supported

Login to reply

👍?
helpful
9:19am Thu 29th Sep, Monti C.

Hi, I'm having an issue with my strcat_p function where I'm getting an EXC_BAD_ACCESS code=2 error which occurs with the sample solution also. I've attempted to debug it and found that the addresses of dest and src are next to each other, so I'm assuming its something to do with the dest pointer overstepping its bounds into the src pointer but I don't understand how I could fix this. Thank you.


SVG not supported

Login to reply

👍?
helpful
2:56pm Thu 29th Sep, James A.

It's hard to tell without looking at your system, but it sounds like there isn't enough space allocated in the destination array to hold the result. Remember, strcat doesn't contain any mechanism for allocating more space, so the destination has to have enough space to contain both the source and destination strings once combined (plus null terminator). Otherwise, dest will go past the end of valid memory, which would be a bug in the caller, not strcat itself.


SVG not supported

Login to reply

👍?
helpful
4:21pm Fri 30th Sep, Gihad C.

Piggybacking on this thread as I had a related question...

From what I understand, strcat_p only works as intended when there just happens to be enough memory adjacent to the dest string where we can copy the entire src (plus '\0'). If there happened to be other (meaningful but currently irrelevant) data in the adjacent memory, wouldn't we be rewriting this memory by doing "*dest = *src" (where dest is a pointer to the last character in destination string)?

How do you increase the amount of space available for the destination string to be lengthened by? Are you not passing a pointer to a string that already has a defined size at runtime(?)


 UWA week 40 (2nd semester, week 10) ↓
SVG not supported

Login to reply

👍x1
helpful
3:02am Mon 3rd Oct, Christopher M.

"Gihad Coorey" [email protected] wrote:

From what I understand, strcat_p only works as intended when there just happens to be enough memory adjacent to the dest string where we can copy the entire src (plus '\0'). If there happened to be other (meaningful but currently irrelevant) data in the adjacent memory, wouldn't we be rewriting this memory by doing "*dest = *src" (where dest is a pointer to the last character in destination string)?

Yes. You need to provide sufficient space for dest before calling strcat_p()

How do you increase the amount of space available for the destination string to be lengthened by? Are you not passing a pointer to a string that already has a defined size at runtime(?)

You can increase the space associated with a pointer, and copy its original data too, with realloc() but, of course, that requires the pointer to have first been obtained through an earlier call to malloc() or realloc().

Otherwise, you need to first allocate new memory long enough to hold the original and the new data.


SVG not supported

Login to reply

👍x1
helpful
11:48am Thu 6th Oct, James A.

From what I understand, strcat_p only works as intended when there just happens to be enough memory adjacent to the dest string

This is correct, other than saying "just happens to be". As you are the one who determines what memory (and thus, the size of the memory) that dest points to, ensuring there is enough remaining space for the src string is your responsibility.

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Written by [email protected]
Powered by history
Feedback always welcome - it makes our software better!
Last modified  1:17AM Sep 14 2022
Privacy policy