C string array

This is a discussion on C string array within the C Programming forums, part of the General Programming Boards category; Hello, I am newbie in C. I stored strings in array. (I know that string itself is an array. ) ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    9

    C string array

    Hello,

    I am newbie in C. I stored strings in array. (I know that string itself is an array. ) There is error when I compile and run the program. But I need to be sure that the following idea is correct or not since I dont want to meet unnecessary problems later.

    I'd like push strings onto the stack_array using a loop or function call.

    For example, it will look like:

    Code:
    stack[0]  =  " This is first line";
    stack[1]  =  " This is second line";
    .....
    .....
    .....
    stack[i]  = " This is last line";
    Code:
    // global variable declaration 
    
    const int  depth = 30;
    char*  Stack[depth ];
    int index= 0                    
    
    
    function Add_data_to_stack (char *data, int data_length)
    {
    
              // memory allocation for stack[index] happened here
    
        	 strncpy(Stack[index],data,data_length);   // copy data to stack[index]
        	 Stack[counter][len] = '\0';                        //  adding zero terminated character
          
             index++;
            
         // release memory allocation here  ( free ()  )
    }
    Last edited by galaxy_virus; 10-03-2008 at 12:44 AM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    Hmmm.. I can't edit my post. There are some typos. I am sorry.

    I wanted to mean that there is *no* error when I compile and run the program.
    Last edited by galaxy_virus; 10-02-2008 at 07:03 AM. Reason: to fix the typo

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Why would you release memory allocation inside the Add_data_to_stack fucntion? You want to release memory allocated (with free() ) when you don't want it anymore. Make another function Destory_stack() to do this job.

    Aslo, since you have the depth of the stack use it in order to avoid overflows. You can do something this at the very start of the Add_data_to_Stack.
    Code:
    if (index > depth-1) {
       printf("Stack full!\n");
       return;
    }
    Last edited by C_ntua; 10-02-2008 at 07:15 AM.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    Quote Originally Posted by C_ntua View Post
    Why would you release memory allocation inside the Add_data_to_stack fucntion? You want to release memory allocated (with free() ) when you don't want it anymore. Make another function Destory_stack() to do this job.

    Aslo, since you have the depth of the stack use it in order to avoid overflows. You can do something this at the very start of the Add_data_to_Stack.
    Code:
    if (index > depth-1) {
       printf("Stack full!\n");
       return;
    }

    Hello C-ntua,

    Thanks for your very prompt reply. I will check depth of the stack as per your suggestion.

    For memory allocation, I have one question. Whenever function is called,
    I need to allocate the memory for stack[index] ( index is somehow incremented).
    So if I free memory in different function, how can I reallocate the memory here?
    'though I know there is realloc(), I dont know how to control it.
    Here is my naive idea.

    such as.. If ( index = 1 )
    malloc();

    else
    realloc();

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    The idea is:
    Push: allocate / copy
    Pop: free

    You don't need more than that.
    And push is add data to the stack and pop is remove data from the stack.
    And kindly put your reply text outside the code tags.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    Quote Originally Posted by Elysia View Post
    The idea is:
    Push: allocate / copy
    Pop: free

    You don't need more than that.
    And push is add data to the stack and pop is remove data from the stack.
    And kindly put your reply text outside the code tags.

    Thanks for your reply. And I placed my post ouside code.

    push : allocate

    so there is no problem if we do malloc() for many times in push ( or add_data_to_stack) function () ??


    My pop function looks like -

    1) printing out all items
    2) filling up NULL .. ( I am using this function as init() before using stack )
    3) free memory

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Quote Originally Posted by galaxy_virus View Post
    Thanks for your reply. And I placed my post ouside code.
    Yeah, that's good, but code should stay inside code tags. It was invented for the use of putting code inside.

    push : allocate
    so there is no problem if we do malloc() for many times in push ( or add_data_to_stack) function () ??

    My pop function looks like -

    1) printing out all items
    2) filling up NULL .. ( I am using this function as init() before using stack )
    3) free memory
    Example:
    Code:
    void push(char* data)
    {
        stack[++sp] = malloc(strlen(data) + 1); // Allocate
        strcpy(&stack[sp], data);
    }
    
    char* pop()
    {
        char* temp = malloc( strlen(stack[sp]) );
        strcpy(temp, stack[sp]);
        free(&stack[sp--]);
        return temp;
    }
    Now we can talk about efficiency, but... let's leave that aside for now. This is just a basic example of how you might do.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  8. #8
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You might want to add a check for if the stack is empty, since then pop() will cause a segmantation fault.

    The performance issue is just about allocating and freeing memory all the time. Just keep that in mind for the feature. The alternative is to allocate once memory, lets say in an init() fucntion and free it with e delete() function. Another way is to just do char Stack[depth][stringDepth] and statically allocate memory.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Quote Originally Posted by C_ntua View Post
    You might want to add a check for if the stack is empty, since then pop() will cause a segmantation fault.
    Yes, I left that out on purpose.
    It's only a basic example, not meant to be complete.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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