String assignment segmentation fault (core dump)

This is a discussion on String assignment segmentation fault (core dump) within the C++ Programming forums, part of the General Programming Boards category; kapil1089thekin, since you are experienced in C, why do you think new will be any different from malloc? What you ...

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    kapil1089thekin, since you are experienced in C, why do you think new will be any different from malloc? What you new you must delete.
    However, to help you out on the way, we have smart pointers in C++. Look up std::shared_ptr and std::weak_ptr. std::shared_ptr is the default one you should use. In (doubly) linked lists, you must watch out, because if the prev pointer is a shared_ptr and the next is a shared_ptr, you will get circular references and neither object will be destroyed (they will keep each other alive). For this situation, use weak_ptr on the prev and shared_ptr on the next.
    Experiment a little. Get a feel for smart pointers. They are powerful tools and will relieve you of the freeing duty.
    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.

  2. #17
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107

    Ah yes the memory leak....

    Absolutely, delete should have been applied. So rather than:

    Code:
    int main(int argc, char *argv[])
    {
            struct node *first;
            first = new struct node;
            first->name ="Hi there";
            cout << first->i;
    
    }
    Which neglects the proper book keeping, one should release the memory from the heap prior to to leaving the main function's scope, and it is usually prudent to test that the allocation was successful. Thus a proper implementation would read:

    Code:
    int main(int argc, char *argv[])
    {
        struct node *first;
        first = new struct node;
    
        if(NULL == first)
        {
            // error handling for failed memory allocation....
            cout << "Memory was not allocated." << endl;
    
            return 1;
        }
        first->name ="Hi there";
        cout << first->i;
        delete first;
    
        return 0;
    }
    I apologize about missing that in the previous post.

    Best Regards,

    New Ink

  3. #18
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    Kapil,

    Quote Originally Posted by kapil1089thekin View Post
    1.Do I need to delete each node in a linked list if I get each node'e memory allocation by calling new?
    Absolutely yes, doing so will prevent memory leaks, and bad references from making your application unstable.

    Quote Originally Posted by kapil1089thekin View Post
    2. In that case it will add O(n) time complexity more? right?
    I don't believe that we included the memory allocation behaviors when we calculated complexity while I was in algorithm analysis. An algorithm's complexity was associated with the number/frequency of arithmetic and comparison operations which pertained to the task being completed. As far as space efficiency is concerned the answer is a half-hearted yes. The number of operations pertaining to managing the space consumed while executing an algorithm will increase by (at most) a scalar constant of the input size.

    This was generally neglected during my course work, and as Levitin, Anany stated in Introduction to The Design & Analysis of Algorithms "...we are after a measure of an algorithm's efficiency, we would like to have a metric that does not depend on [speed of a given computer, quality of the implementation nor the compiler generating the machine code]...." (p44) In fact, he suggests that "[the] thing to do is to identify the most important operation of the algorithm, called the basic operation, the operation contributing the most to the total running time, and compute the number of times the basic operation is executed." (p44) He arrived at that suggestion after examining the possibility of counting the number times each of the algorithm's operations are executing using counters and a such things which is often unnecessary.

    There is not much algorithm associated with your example, so I suppose that the additional comparison, to test that allocation is successful, does add a notable amount of time complexity (i.e. 1*the number of nodes) .

    Quote Originally Posted by kapil1089thekin View Post
    3.Is there any better solution for freeing the memory?
    The new & delete operators are C++'s interface for the memory heap. I'm not sure how you mean "better," but they are the primary tools for manipulating memory consumed/release during to course of a program's execution.

    Best Regards,

    New Ink -- Henry

  4. #19
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    @new_ink2001:
    The standard new (that is, none overloaded and the usual not-nothrow version of new) doesn't return NULL but rather throws an exception (std::bad_alloc) when the allocation failed. So in your case, the check for NULL is useless. However, you might want to wrap the code in a try catch block unless you don't care about the program crashing on a failed allocation (in this case you shouldn't bother).

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Is there a better solution for freeing memory? Yes.
    Code:
    int main(int argc, char *argv[])
    {
        std::shared_ptr<node> first(new node);
    
        first->name ="Hi there";
        cout << first->name;
    
        return 0;
    }
    Again, remember that an exception will be thrown if memory could not be allocated.
    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.

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

Similar Threads

  1. Polymorphism and generic lists
    By Shibby3 in forum C# Programming
    Replies: 9
    Last Post: 07-26-2010, 06:27 AM
  2. Segmentation fault when changing a string
    By lilydjwg in forum C Programming
    Replies: 6
    Last Post: 12-02-2009, 07:43 AM
  3. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 03:01 AM
  4. segmentation core dump - need help
    By knight101 in forum C++ Programming
    Replies: 1
    Last Post: 11-26-2001, 04:43 PM

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