Is this right

This is a discussion on Is this right within the C++ Programming forums, part of the General Programming Boards category; Ive been trying to do this for a while and still cant figure it out, im probably going about it ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    42

    Question Is this right

    Ive been trying to do this for a while
    and still cant figure it out, im probably
    going about it the wrong way


    Code:
    
    int size;
    char test[] = "hello world this is a great day";
    
    
    size = strlen(test);
    
    
    my_function(test,size);
    
    
    void my_function(char *string, int sizer)
    {
    
    
      char new_string[sizer];
    
    
    return(0);
    
    
    }

    I want to allocate the size of new_string dynamically
    can this be done?


    Marky_Mark

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You'd have to do -

    char* new_string = new char[sizer+1];
    strcpy(new_string,string);

    and after you've finished with new_string(don't let it go out of scope unless you're copying/returning it) you need to free the memory -

    delete[] new_string;
    new_string=NULL;
    zen

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    42
    Thanks zen

    I am nearly pulling my hair out, i need to pass
    a filename to a function, but am strugling


    How do i get the whole filename and use it in my function


    It's accessing words as a whole that im struggling with
    surely i don't have to loop through an array to
    get my filename


    Marky_Mark
    Last edited by Marky_Mark; 10-20-2001 at 01:32 PM.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Do a search through the boards... I remember exactly that topic being discussed at least once recently.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    42
    Ive been searching all day, and all i can find
    is how to access strings through looping


    Id like to point straight to my string and use
    this as a whole variable, is this possible without
    looping


    Marky_Mark

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    Prototype
      void open_thisfile(char *filename);

    Function
      void open_thisfile( char *filename ) {
      // blah
      fp = fopen(filename,"r");
      }

    main
      char file[] = "test.txt";
      open_thisfile(file);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    95
    I was raised to think that you had to declare the function before the function call...Is that not true?!?!
    Think out of the box! Open Source rules!

    -Breach23

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    42
    Thanks Salem



    Marky_Mark

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    > I was raised to think that you had to declare the function before the function call...Is that not true?
    Either the function declaration (or it's prototype) should be seen before you try and use a function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User bljonk's Avatar
    Join Date
    Oct 2001
    Posts
    70

    Question return VALUE



    void my_function(char *string, int sizer) {
    ....
    return (0);
    ....
    }

    what does this mean anyway?
    Does it create a new space within the RAm ur are just playing with it.
    Ünicode¬>world = 10.0£

  11. #11
    Intranasal Heroin User Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,035
    hey Salem, quick question, how do you print the contents of a file on your program with your technique? im just curious....thanks

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    42
    zen you talked about freeing up the space held
    by my objects, how do i free these memory resources


    Code:
    
            int a;
    	int *ptr;
    
    
    	Test* X = NULL;
    
    
    	X = new Test;
    
    
            X->set_id(10);
    	a = X->get_id();
    
    
    	ptr = &a;

    I need to free *ptr and which object do i need to free X or Test i don't know


    Some correct syntax would be nice for standard pointer and object


    Marky_Mark
    Last edited by Marky_Mark; 10-20-2001 at 04:31 PM.

  13. #13
    Intranasal Heroin User Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,035
    Code:
    delete *ptr

  14. #14
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    NO NO NO

    delete ptr;
    delete X;
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    42
    Can anyone shed some light on what's happening


    Ive added


    Code:
    
    delete ptr; 
    delete X;

    To my program and it crashes, do i need to
    add a memory header to use delete


    Im using Visual C++ 6.0


    Marky_Mark

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21