Re: problem with function

From: rossum (rossum48_at_coldmail.com)
Date: 10/24/04


Date: Sun, 24 Oct 2004 22:03:25 +0100

On Sat, 23 Oct 2004 12:34:05 -0500, "mobile Cephus" <beau@highway.net>
wrote:

[snip]

>that is interesting about naming conventions...I guess it is the same as
>people do with VB variables?
As Jack said, it is best to avoid the full horror of Hungarian
notation. To me it is one of those basically good, simple ideas that
got taken to extremes and complicated to the point where it ceased to
be useful. By all means put a 'p' on the start of pointer variables
and an 'i' on the start of integers if you want to, but please don't
do much more than that.

>
>Can you tell me why this work and my first example doesn't?
You are getting one of the most dangerous forms of "undefined
behaviour", that is you program appears to work despite the error.
That means that the error could sit in your code until it suddenly
jumps out to bite you when you least expect it.

I suspect that your original version had the two file names declared
one after the other:
>//oldName = "data";
>//newName = '\0';
Hence any attempt to write into memory past the end of oldName would
hit memory reserved for newName and cause the crash. In your second
version of the code, you are declaring newName inside the
changeFileName() function, so it is being stored in a different place
by the compiler. So by chance there is nothing sitting in the memory
after oldName so the compiler is not warning you when you overwrite
it. You still have the same error, just a different symptom.

If you declare a dummy string just after oldName in your second
version then the problem will probably reappear:
    const char* oldName = "data";
    const char* dummy = "dummy";

>
>void main(int argc, char **argv){
*Always* int main(), never void main().

> changeFileName(argv[1]);
Compile error here, you need a forward declaration, or just move
main() to the end.

>}
>
>void changeFileName(char *oldName){
> char *newName;
> newName = oldName;
> cout << newName << "\n\n";
Looks like you are using C++. That gives you a far better option.
See below.
> //now let's add the .sorted to it
> int x = 0;
> while (newName[x] != '\0')
> {
> x++;
> }
> //add the .sorted
> newName[x] = '.';
> newName[x+1] = 's';
> newName[x+2] = 'o';
> newName[x+3] = 'r';
> newName[x+4] = 't';
> newName[x+5] = 'e';
> newName[x+6] = 'd';
> newName[x+7] = '\0';
> cout << newName;
>}
>
>/////////
>Beau
>

C++ has the std::string class which is much better than the C-style
strings you have been using. Specifically the addition operator works
as you would expect it to and concatenates strings. Use the member
function c_str() to get a C-style string if you need one.

#include <iostream>
#include <string>
using std::string;

string changeFileName(const string& oldName, const string& extra) {
    return (oldName + extra);
}

int main() {
    const char* oldName = "data";

    string newName = changeFileName(oldName, ".sorted");
    const char* newName_C_string =
                changeFileName(oldName, ".sort").c_str();

    std::cout << "Old name: " << oldName << '\n'
              << "New name: " << newName << '\n'
              << "C-string: " << newName_C_string << '\n';

    return 0;
}

rossum

--
The ultimate truth is that there is no Ultimate Truth


Relevant Pages

  • Re: VB 2008: Option Strict On + Infer On at class level
    ... What does the compiler prevent from declaring x As String? ... Public Sub New ...
    (microsoft.public.dotnet.languages.vb)
  • Re: wchar_t as a string parameter
    ... The bottom line is that there is no setting for this, and there is no known compiler bug that causes this to happen. ... I agree totally on my bad form of declaring my string variable wchar_t ... MyFunction(szMyString); ...
    (microsoft.public.vc.mfc)
  • Re: Question about pointers
    ... compiler you are declaring ... Compiler already knows its a pointer to a char and you are ... asterisk. ...
    (comp.lang.c)
  • Re: Macro for Changing Files Names
    ... Dim MyPath As String, MyFilename As String ... Dim OldName() As String, NewName As String ... Dim Prefix As String ...
    (microsoft.public.word.vba.general)
  • Re: Macro for Changing Files Names
    ... Dim MyPath As String, MyFilename As String ... Dim OldName() As String, NewName As String ... Dim Prefix As String ...
    (microsoft.public.word.vba.general)

Loading