Thread: Problem with new :/

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    27

    Problem with new :/

    Code:
    char* file_location = new char;
          strncat (file_location, filename, strlen(filename) - strlen(basename(filename)));
          strcat (file_location, "../conf/sync.conf");
          file = new char;
          strcpy (file, file_location);
    Simply enough, i takes file_location, copies filename into it minus the filename, and then adds "../conf/sync.conf"

    (filename is a string to the absolute path of the program being ran)

    But, when the "file = new char;" is called, it cuts down the string file_location a lot, making the file sync.conf unopenable o_0;

    Why is new on a variable killing a different one? o_0
    Can I only have one new at a time.. or.. ? x_X

    Thanks...

    Funny, doing this in the malloc way works fine =(
    Last edited by Zarkhalar; 08-03-2004 at 08:14 PM. Reason: update

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You are only allocating one char. You need to allocate room for the entire string. You do this by:

    Code:
    char* file_location = new char[100];
    Obviously 100 can be replaced by however many char's you need to allocate. Also, when you use new with [] you need to use delete with [].

    delete [] file_location.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    27
    Ahh, I thought that it didn't need that =)
    Thank yas

    One question.. why would all the other ones work ok until this point?

    Edit: Also, what if I want to change the size of it? Delete it first then do another new?
    Last edited by Zarkhalar; 08-03-2004 at 08:51 PM.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    What do you mean "all the other ones"? malloc? I'm not sure what you mean there..

    As for your edit, yes, you would have to delete it then new another.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    A better idea would be to use std::string.

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    27
    I ment all the strings, even though there wasn't the space for it, still printed out and all... with new, not talking about malloc

    I don't even know what std::string is :P I haven't played with iostream.h, if thats what it is :O (I rarely use anything that I can't find on my unix man pages :P)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM