#### Archived

This topic is now archived and is closed to further replies.

# Simple char* problem

This topic is 5452 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok heres another dumb question. This function is meant to extract whatever is between the first and last " in the string. For example: If you passed the string: username="peter",password="twoe32" it would return: peter",password="twoe32 char * extractquotation(char * s) { char * FIRSTQUOTE, * LASTQUOTE; FIRSTQUOTE=strchr(s,''"''); LASTQUOTE=strrchr(s,''"''); *LASTQUOTE=NULL; return FIRSTQUOTE+1; } however its not doing that, instead its crashing. any ideas?

##### Share on other sites
What's crashing is the *LASTQUOTE = NULL. You can't really do that, nor should you really need to as most strings have an implicit null-termination that was added when they were created.

This works.

char * FIRSTQUOTE, * LASTQUOTE;
FIRSTQUOTE=strchr(s,'"');
LASTQUOTE=strrchr(s,'"');
int last = LASTQUOTE - s + 1;
int first = FIRSTQUOTE - s + 1;
char result[1] = "";
strncat(result, FIRSTQUOTE, last-first);
return result+1;

-fel

just noticed this is a double-post btw... I'll delete the other one since it didn't seem to get helped really.

[edited by - felisandria on November 12, 2003 11:06:24 AM]

##### Share on other sites
felisandria,
I''m sure your method works fine, but mine should work fine too. Because it DOES work! - in DJGPP. but under VC++ it crashes! Does anyone know why this is??

I probably should stop, My doctor says I have the wrists of an 80-year old

##### Share on other sites
Even if it does work, forcing a null termination where it''s not expected is going to do some weird stuff, especially if you plan to further manipulate the string elsewhere.

-fel

##### Share on other sites
it works if I compile as a Release. good enough for me!

##### Share on other sites
Nodger,
I tested your function using the Intel C++ compiler, and it runs fine. One thing I noticed, however, is that you are setting the value of *LASTCHAR to null, which means that you are changing the data passed into the function. Is this intended? Perhaps what''s actually crashing is somewhere else in your code you are referencing past that "new" null terminator in some way and it''s causing a problem? Also, keep in mind that when you crash in debug mode it''s a good thing. The compiler pads your memory with all sorts of flags so that it can detect if you write past the bounds of arrays and such. In release mode it doesn''t. So, for instance, you could have an array directly followed by a boolean on the heap, and they could both belong to your app. If you were to write past the end of that array, you would change the value of that boolean. Quite a hard logic error to find. I would figure out why your app is crashing in debug mode and not ignore it.

Hope that helps a bit

- Jason Citron
- ZeroInfinity Studios
- www.zeroinfinity.net

##### Share on other sites
One thing about VC is that there is an option to put immediate strings in read only memory, so if your calling it with something like extractquotation("some string") that would crash

and *shudder* at felisandria''s method, nice buffer overrun there... result is only 1 char long, so you have promptly overwritten whatsver is after result in memory

##### Share on other sites
Eep. Had meant to go back and allocate that properly once I had done the math. Sorry.

His exact code does crash in VC, btw.

-fel

[edited by - felisandria on November 12, 2003 1:10:44 PM]

##### Share on other sites
you should almost definately not be implementing the function the way you are ... because it is misleading as mentioned previously ...

FIRST - IF you are wanting to return a changed string, in other words an input AND output pareter, do it using that scheme, instead of a return value, because the return value makes users thing they are passing in an input only parameter and getting a different output returned ... when in fact they are passing in an input, which is promtly screwed up by your function, and getting a DIFFERENT output from the funtion ...

look at the values-
prior to function argument equals:
returned from the function is:
after the function argument equals:

see how confusing this would be ... where this makes more sense -

void extractquotation(char *&s)
{
char * FIRSTQUOTE, * LASTQUOTE;
FIRSTQUOTE=strchr(s,''"'');
LASTQUOTE=strrchr(s,''"'');
*LASTQUOTE=''\0'';
s = FIRSTQUOTE;
return;
}

but as mentioned, immediate C strings are supposed to be read only (const) ... so even that is not valid if the argument passed in is an immediate value and not a string you are allowed to be manipulating ...
;

##### Share on other sites
quote:
Original post by Nodger
I''m sure your method works fine, but mine should work fine too. Because it DOES work!

This is not how C++ programs should be written. The observable behaviour of a program under a simple test case does not suggest that the program is correct under all scenarios, neither does it guarantee that the program is correct C++.
quote:

in DJGPP. but under VC++ it crashes! Does anyone know why this is??

You''re probably invoking undefined behaviour. When you invoke UB, any outcome is valid. To introduce some sort of stability into your function, try to eliminate side-effects. Change the function signature to this:
extractquotation(char const * const s);

This means that s is a const pointer to a const char, and you cannot modify either the pointer nor the char array pointed to. This will force you to malloc storage within your function to hold the result of the function. You then need to make it clear that the caller of the function is responsible for free-ing this storage. The overall effect of doing all this will be to ensure you are not accidentally passing in const char arrays and writing to them, which will eliminate the most likely source of undefined behaviour in your function.

1. 1
2. 2
3. 3
Rutin
17
4. 4
5. 5

• 14
• 9
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
632912
• Total Posts
3009185
• ### Who's Online (See full list)

There are no registered users currently online

×