Thread: _strdup on an atoi necessary?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    55

    _strdup on an atoi necessary?

    Here is my struct
    Code:
    struct pdat {
    	struct playerQuestInfo questData;
    };
    
    struct playerQuestInfo {
    	int questID[MAX_QUESTS_PER];
    	char *questObjectiveType[MAX_QUESTS_PER];
    };
    
    #define PLAYER_QUEST(ch)	(ch)->questData
    Here is the program setting the data in the above struct
    Code:
    const char *num = "1"; // This is only here to give an idea of where num is coming from.
    const char *txt = "T";
    PLAYER_QUEST(ch).questID[qd] = atoi(_strdup(num));
    PLAYER_QUEST(ch).questObjectiveType[qd] = _strdup(txt);
    My question is this: Do I need "_strdup" on an atoi?

    Is it safe to do the following?:
    Code:
    PLAYER_QUEST(ch).questID[qd] = atoi(num);
    Instead of:
    Code:
    PLAYER_QUEST(ch).questID[qd] = atoi(_strdup(num));
    ???

    Thanks.

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    atoi(num) is perfectly safe in the scenario you've described.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    55
    Quote Originally Posted by PING View Post
    atoi(num) is perfectly safe in the scenario you've described.
    Excellent. Thank you. I'm trying to eliminate as many _strdup's as possible so I don't have to "free" them later.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. _strdup and free();
    By xwielder in forum C Programming
    Replies: 2
    Last Post: 04-29-2011, 10:10 PM
  2. atoi?
    By mayfda in forum C++ Programming
    Replies: 6
    Last Post: 11-19-2002, 09:45 AM
  3. atoi
    By Max in forum C Programming
    Replies: 1
    Last Post: 11-05-2002, 10:28 AM
  4. atoi
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 02:39 AM