Can this code work

Can this code work:
[code]
#include

char* abc()
{

//Here goes code.....
return(ValueToReturn);
}

#define F (abc())

void main()
{
char * s = F;
}

[/code]

?

Comments

  • : Can this code work:
    : [code]
    : #include
    :
    : char* abc()
    : {
    :
    : //Here goes code.....
    : return(ValueToReturn);
    : }
    :
    : #define F (abc())
    :
    : void main()
    : {
    : char * s = F;
    : }
    :
    : [/code]
    :
    : ?
    :
    [blue]It will work, if 'ValueToReturn' is a storage which will not be destroyed when exiting from function abc(). Example:
    [code]
    char* abc ()
    {
    char* p = "Hello";
    char array [] = "Hello";

    return p; //OK
    return array; // Absolutely not OK!
    }
    [/code]
    [/blue]
  • The full code is:

    [code]

    #include
    #include

    char* GetStringBscFunc()
    {


    char * StringToReturn;

    cscanf( "%s", s );


    return( s );

    }


    #define GET_STRING ( GetStringBscFunc() )


    void main()
    {

    char* s;

    gotoxy(34,12);
    s = GET_STRING;

    cout << s << endl;

    }
    [/code]

    It seem to have an error that the compiler will not detect.
    Can you help me?
  • [b][red]This message was edited by AsmGuru62 at 2004-4-1 8:45:30[/red][/b][hr]
    : The full code is:
    :
    : [code]
    :
    : #include
    : #include
    :
    : char* GetStringBscFunc()
    : {
    :
    :
    : char * StringToReturn;
    :
    : [red][b]StringToReturn = (char*) malloc (128);[/b][/red]
    : cscanf( "%s", [red][b]StringToReturn[/b][/red] );
    :
    :
    : return( [red][b]StringToReturn[/b][/red] );
    :
    : }
    :
    :
    : #define GET_STRING ( GetStringBscFunc() )
    :
    :
    : void main()
    : {
    :
    : char* s;
    :
    : gotoxy(34,12);
    : s = GET_STRING;
    :
    : cout << s << endl;
    : [red][b]free (s);[/b][/red]
    :
    : }
    : [/code]
    :
    : It seem to have an error that the compiler will not detect.
    : Can you help me?
    :
    [blue]Since you have a C++ (I see [b]cout[/b] there...) - you should use it better. For example, make an object to hold that string, so the allocation/deallocation of memory will be automated by C++.[/blue]


  • [b][red]This message was edited by WerewolfWare at 2004-4-1 9:31:21[/red][/b][hr]
    [b][red]This message was edited by WerewolfWare at 2004-4-1 9:30:56[/red][/b][hr]
    [b][red]This message was edited by WerewolfWare at 2004-4-1 9:30:35[/red][/b][hr]
    And another question

    [code]
    #include
    #include

    char* GetStringBscFunc(int sta)
    {


    char * StringToReturn = new char[sta];

    cscanf( "%s", s );


    return( s );

    }


    #define GET_STRING(SIZE) ( GetStringBscFunc(SIZE) )
    #define GET_CHAR ( GET_STRING(1)[0] ) [red][b]//->is this code ok?[/b][/red]
    [/code]






  • [b][red]This message was edited by AsmGuru62 at 2004-4-1 10:59:47[/red][/b][hr]
    [b][red]This message was edited by AsmGuru62 at 2004-4-1 10:57:12[/red][/b][hr]
    : [b][red]This message was edited by WerewolfWare at 2004-4-1 9:31:21[/red][/b][hr]
    : [b][red]This message was edited by WerewolfWare at 2004-4-1 9:30:56[/red][/b][hr]
    : [b][red]This message was edited by WerewolfWare at 2004-4-1 9:30:35[/red][/b][hr]
    : And another question
    :
    : [code]
    : #include
    : #include
    :
    : char* GetStringBscFunc(int sta)
    : {
    :
    :
    : char * StringToReturn = new char[sta];
    :
    : cscanf( "%s", s );
    :
    :
    : return( s );
    :
    : }
    :
    :
    : #define GET_STRING(SIZE) ( GetStringBscFunc(SIZE) )
    : #define GET_CHAR ( GET_STRING(1 [b]/* you need 2 here */[/b] )[0] ) [red][b]//->is this code ok?[/b][/red]
    : [/code]
    :
    [blue]Also, who is gonna delete the allocated room?[/blue][code]
    char ch = GET_CHAR; // MEMORY LEAK!
    [/code]
    [blue]If you want to do it without object-oriented programming at least make a global buffer (instead of allocated one) to keep your inputted string. But, then you will have troubles to work with more then one inputted string at a time, because every new string from console will erase the old string, because the variable is global.

    Again, the best way to do it - use objects.[/blue]

  • I'm only tring things here - I will use the exit() func to clear all alocated momrey.
  • : I'm only tring things here - I will use the exit() func to clear all alocated momrey.
    :
    [blue]That will not help if you use that function in a loop - every loop turn you will lose more and more memory and finally it will flop.[/blue]
  • Can I use this macro on a program at the end of every loop?
    [code]
    char* ts;

    char* GetString(int sz)
    {
    ts = new char[sz];
    cscanf("%s",ts);
    return(ts);
    }

    #define DELETE_MEM (delete ts)

    [/code]
  • [b][red]This message was edited by AsmGuru62 at 2004-4-2 8:22:50[/red][/b][hr]
    [b][red]This message was edited by AsmGuru62 at 2004-4-2 8:20:19[/red][/b][hr]
    : Can I use this macro on a program at the end of every loop?
    : [code]
    : char* ts[red][b]=NULL[/b][/red];
    :
    : char* GetString(int sz)
    : {
    : [red][b]delete [] ts;[/b][/red]
    : ts = new char[sz];
    : cscanf("%s",ts);
    : return(ts);
    : }
    :
    : #define DELETE_MEM (delete ts)
    :
    [red][b]int main ()
    {
    // your ALL code here...

    delete [] ts;
    return 0;
    }[/b][/red]
    : [/code]
    :
    [blue]You got it!
    But, look at the RED... it is better solution. Since 'GetString()' is the only function to deal with that global 'ts' you simply delete it BEFORE reusing it in function - this way you need to delete only last one before returning to DOS.

    You can improve on it even more!
    If you keep the room allocated for 'ts' somewhere, then you can see if the room requested by GetString() already there and simply return same pointer. Only re-allocate it if you need larger room than saved from previous calls. That will save you from defragmenting the heap and DOS is very sensitive to this problem.[/blue]





  • Thank you - you have been very helpful.
    Here is the file I've been working on - tell me if you find something wrong:
    [code]
    //Header - INPUTSDB.H
    #ifndef ___INPUTSDB_H
    #define ___INPUTSDB_H

    #include
    #include

    char *TemporaryStringForInputData = NULL; [red]//Long for a simple reason - I will not remember the entier name so I wouldn't[/red]
    [red]//be able to address it in my programs[/red]

    #define FREE_INPUT_STRING ( delete TemporaryStringForInputData )

    char* GetStringEx(int alloc_size)
    {

    FREE_INPUT_STRING;
    TemporaryStringForInputData = new char[ alloc_size ];

    cscanf( "%s", TemporaryStringForInputData );

    return( TemporaryStringForInputData );

    }

    #define GET_STRING(SIZE) ( GetStringEx( SIZE ) )
    #define GET_FLOAT ( atof( GET_STRING( 100 ) ) )
    #define GET_LONG ( atol( GET_STRING( 100 ) ) )
    #define GET_CHAR ( GET_STRING( 1 )[ 0 ] )
    #define GET_DOUBLE ( GET_FLOAT )
    #define GET_INT ( atoi( GET_STRING( 100 ) ) )

    #define FREE_INPUT_STRING ( delete TemporaryStringForInputData )

    #endif

    [/code]
  • [b][red]This message was edited by AsmGuru62 at 2004-4-2 12:16:50[/red][/b][hr]
    [b][red]This message was edited by AsmGuru62 at 2004-4-2 12:16:16[/red][/b][hr]
    [b][red]This message was edited by AsmGuru62 at 2004-4-2 12:10:15[/red][/b][hr]
    [code]
    #define FREE_INPUT_STRING ( delete TemporaryStringForInputData )

    ...

    #define GET_STRING(SIZE) ( GetStringEx( SIZE ) )
    [red]#define GET_DOUBLE ( atof( GET_STRING( 100 ) ) )[/red]
    #define GET_LONG ( atol( GET_STRING( 100 ) ) )
    #define GET_CHAR ( GET_STRING( [red][b]2[/b][/red] )[ 0 ] )
    [red]#define GET_FLOAT ( (float) GET_DOUBLE )[/red]
    #define GET_INT ( atoi( GET_STRING( 100 ) ) )

    #define FREE_INPUT_STRING ( delete TemporaryStringForInputData )
    [red][b]^^^ Why defined twice?[/b][/red]

    #endif
    [/code]






  • Thank's - you've helped me a lot.
    And - FREE_INPUT_STRING is defined twice because I forgot to delete the second line when I copyed it to the head of the header.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories