Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Linked Lists

Linked Lists

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structuresperformance
10 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sroberts82
    wrote on last edited by
    #1

    Hi all, I have a linked list of typedef struct someStruct{ DatasourasRex someData; someStruct *nextElement; }someStruct; so I have a list where 1 of these points to the second and a second points to the 3rd. I wish to remove the second so I have just set the pointer to point to the 3rd. But Id imagine this would give me problems with memory leaks maybe? I have used malloc each time to give me the memory, so how do I deallocate? Thanks in advance

    J C 2 Replies Last reply
    0
    • S sroberts82

      Hi all, I have a linked list of typedef struct someStruct{ DatasourasRex someData; someStruct *nextElement; }someStruct; so I have a list where 1 of these points to the second and a second points to the 3rd. I wish to remove the second so I have just set the pointer to point to the 3rd. But Id imagine this would give me problems with memory leaks maybe? I have used malloc each time to give me the memory, so how do I deallocate? Thanks in advance

      J Offline
      J Offline
      Jack Puppy
      wrote on last edited by
      #2

      Use free There's a link to an example at the bottom of the page. In Italy for thirty years under the Borgias they had warfare, terror, murder, bloodshed - but they produced Michelangelo, Leonardo da Vinci and the Renaissance. In Switzerland they had brotherly love, five hundred years of democracy and what did that produce - the cuckoo clock! -- Harry Lime

      C 1 Reply Last reply
      0
      • J Jack Puppy

        Use free There's a link to an example at the bottom of the page. In Italy for thirty years under the Borgias they had warfare, terror, murder, bloodshed - but they produced Michelangelo, Leonardo da Vinci and the Renaissance. In Switzerland they had brotherly love, five hundred years of democracy and what did that produce - the cuckoo clock! -- Harry Lime

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        NEVER use free unless you use malloc and NEVER use malloc if you're using C++. Christian Graus - Microsoft MVP - C++

        A 1 Reply Last reply
        0
        • S sroberts82

          Hi all, I have a linked list of typedef struct someStruct{ DatasourasRex someData; someStruct *nextElement; }someStruct; so I have a list where 1 of these points to the second and a second points to the 3rd. I wish to remove the second so I have just set the pointer to point to the 3rd. But Id imagine this would give me problems with memory leaks maybe? I have used malloc each time to give me the memory, so how do I deallocate? Thanks in advance

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          1. NEVER use free or malloc in C++ 2. Never write your own linked list class for production, use std::list. 3. It's good to write one though, to learn. If you want to remove an item from the list, you delete the someStruct in question, and you take the item that is before it, and change it's nextElement pointer to the item after it. Christian Graus - Microsoft MVP - C++

          S P 2 Replies Last reply
          0
          • C Christian Graus

            1. NEVER use free or malloc in C++ 2. Never write your own linked list class for production, use std::list. 3. It's good to write one though, to learn. If you want to remove an item from the list, you delete the someStruct in question, and you take the item that is before it, and change it's nextElement pointer to the item after it. Christian Graus - Microsoft MVP - C++

            S Offline
            S Offline
            S Senthil Kumar
            wrote on last edited by
            #5

            Christian Graus wrote: 1. NEVER use free or malloc in C++ Unless you're overloading operator new :) Regards Senthil _____________________________ My Blog | My Articles | WinMacro

            1 Reply Last reply
            0
            • C Christian Graus

              NEVER use free unless you use malloc and NEVER use malloc if you're using C++. Christian Graus - Microsoft MVP - C++

              A Offline
              A Offline
              Abebe
              wrote on last edited by
              #6

              Why? I always automatically use 'new' and 'delete' in C++, but i'm just curious what your 'motivations' are for not using 'malloc' and 'free' in C++. Er zit een korstje op mijn aars.

              L D 2 Replies Last reply
              0
              • C Christian Graus

                1. NEVER use free or malloc in C++ 2. Never write your own linked list class for production, use std::list. 3. It's good to write one though, to learn. If you want to remove an item from the list, you delete the someStruct in question, and you take the item that is before it, and change it's nextElement pointer to the item after it. Christian Graus - Microsoft MVP - C++

                P Offline
                P Offline
                Prakash Nadar
                wrote on last edited by
                #7

                Hi might be doing his college project rite, so using list and other std templates would defeat the perpose of using the list.


                -prakash

                C 1 Reply Last reply
                0
                • A Abebe

                  Why? I always automatically use 'new' and 'delete' in C++, but i'm just curious what your 'motivations' are for not using 'malloc' and 'free' in C++. Er zit een korstje op mijn aars.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  new and delete use constructors/destructors and let you make full use of OO, malloc and free only control memory. Elaine :rose: The tigress is here :-D

                  1 Reply Last reply
                  0
                  • A Abebe

                    Why? I always automatically use 'new' and 'delete' in C++, but i'm just curious what your 'motivations' are for not using 'malloc' and 'free' in C++. Er zit een korstje op mijn aars.

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    If you are using objects, which have constructors and destructors, malloc() and free() will not work. The new and delete operators will, however. Allocating room for other types such as int, char, and double will work just fine with malloc() and free().


                    "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                    1 Reply Last reply
                    0
                    • P Prakash Nadar

                      Hi might be doing his college project rite, so using list and other std templates would defeat the perpose of using the list.


                      -prakash

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #10

                      Mr.Prakash wrote: Hi might be doing his college project Yes, that's why I said, it's a good idea to write these things to learn, but once he enters the real world, he shouldn't be using his own list class. Christian Graus - Microsoft MVP - C++

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups