+ Reply to Thread
Results 1 to 3 of 3

Thread: C Dynamic String Allocation

  1. #1
    souradipm is offline x10Hosting Member souradipm is an unknown quantity at this point
    Join Date
    Jun 2008
    Posts
    45

    C Dynamic String Allocation

    Hi,
    I am using C to make something, and part of it requires dynamic string allocation. It works, but not perfectly. The demo below prints "Hell World" instead of "Hello World". Please help me to find anything wrong in it.
    Code:
    /*
    Dynamic String Allocation
    By Five Point Software 2008
    */
    
    #include <string.h>
    #include <stdio.h>
    
    char *ConcatStr(char *old_str, char *append_str)
    {
      int num_chars=strlen(old_str)+strlen(append_str);
      char *new_str=malloc(num_chars);
      int i,s;
      for(i=0;i<num_chars;i++){
          *(new_str+i)=0;
      }
      for(i=0;i<strlen(old_str)-1;i++){
          *(new_str+i)=*(old_str+i);
      }
      s=i;
      for(i=0;i<strlen(append_str);i++){
          *(new_str+i+s)=*(append_str+i);
      }
      free(old_str);
      return new_str;
    }
    
    char *NewStr(char *initial_str)
    {
      int num_chars=strlen(initial_str);
      char *new_str=malloc(num_chars+1);
      int i;
      for(i=0;i<num_chars;i++){
          *(new_str+i)=0;
      }
      strcpy(new_str,initial_str);
      return new_str;
    }
    
    void FreeStr(char *old_str)
    {
         free(old_str);
    }
    
    int main(){
        char *s1=NewStr("Hello");
        char *s2=ConcatStr(s1," world");
        printf(s2);
        FreeStr(s2);
        getch();
    }
    Btw, I found the skeleton of the code on the page of a C programming course, with just /* FILL IN THIS FUNCTION */ instead of any real code. I was bored so I filled it in. I don't see what is wrong with it, so if you could spot anything wrong, please tell me.

    ~souradipm

  2. #2
    quantum1's Avatar
    quantum1 is offline x10Hosting Member quantum1 is an unknown quantity at this point
    Join Date
    Sep 2008
    Location
    near Nashville, TN
    Posts
    68

    Re: C Dynamic String Allocation

    I believe the code
    for(i=0;i<strlen(old_str)-1;i++){
    should be
    for(i=0;i<strlen(old_str);i++){
    Two rules of development:
    1) Computers work for people; People do not work for computers
    2) Maintainability is all that matters.

  3. #3
    natsuki's Avatar
    natsuki is offline x10 Sophmore natsuki is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    112

    Re: C Dynamic String Allocation

    It's what quantum said. But if you simply remove the -1 in it you might end up crashing. It's like a bug. When you dynamically allocate strings ALWAYS make sure it's the length of the string you want + 1 or else your string will continue till it eats up all your memory or it crashes. It's because you need to null terminate (with null character '\0' or 0) so it knows the string has ended.
    Quote Originally Posted by souradipm View Post
    Code:
    char *ConcatStr(char *old_str, char *append_str)
    {
      int num_chars=strlen(old_str)+strlen(append_str);
      char *new_str=malloc(num_chars); // should be + 1
      int i,s;
      for(i=0;i<num_chars;i++){
          *(new_str+i)=0;
      }
      for(i=0;i<strlen(old_str)-1;i++){ // this one luckily saves you from your mistake though you didn't expect a mistake will save you from even a bigger mistake above
          *(new_str+i)=*(old_str+i);
      }
      s=i;
      for(i=0;i<strlen(append_str);i++){
          *(new_str+i+s)=*(append_str+i);
      }
      free(old_str);
      return new_str;
    }
    Code:
    char *NewStr(char *initial_str)
    {
      int num_chars=strlen(initial_str);
      char *new_str=malloc(num_chars+1);
    I wonder why in this you added + 1 but in the other one you didn't.. lol
    And you could just use [I]new_str instead, the dereferencing * thing is kinda bulky.

    In php it's ok to make a mistake but in c it's doom. lol

    Maybe you want to try out my programming language just for fun lol.
    Last edited by natsuki; 11-22-2008 at 02:39 PM.

+ Reply to Thread

Similar Threads

  1. Dynamic images with PHP
    By marshian in forum Tutorials
    Replies: 24
    Last Post: 10-30-2008, 11:14 AM
  2. Split string
    By radofeya in forum Programming Help
    Replies: 3
    Last Post: 10-02-2008, 02:35 AM
  3. Replies: 3
    Last Post: 03-10-2008, 12:22 PM
  4. Feedback, please
    By DefecTalisman in forum Scripts & 3rd Party Apps
    Replies: 7
    Last Post: 09-04-2007, 08:33 AM
  5. dynamic ip-sever prob
    By NewFuture in forum Scripts & 3rd Party Apps
    Replies: 1
    Last Post: 07-23-2005, 09:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers