Thread: Segment fault on double whammy char pointer

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656

    Segment fault on double whammy char pointer

    I just don't understand what I'm really doing with the malloc double pointer char initialization! Aggghh..
    The error is segment fault. Any other pointers for what's wrong with my code would be greatly appreciated! Merry Christmas!
    Last edited by Kleid-0; 12-19-2004 at 11:27 PM. Reason: I forgot to say my error message! And I forgot to say Merry Christmas!

  2. #2
    Quote Originally Posted by Kleid-0
    I just don't understand what I'm really doing with the malloc double pointer char initialization! Aggghh..
    The error is segment fault. Any other pointers for what's wrong with my code would be greatly appreciated! Merry Christmas!
    The malloc() thing allocates room for an array of pointers, but the pointers of this array are not initialized. If you want to use them, you also must allocate some memory block and record its address in the array of pointers. You code must be completed.

    Pointers are the tricky point of the C language. I suggest you work on it mre deeply before going further. It must be crystal clear in your mind.
    Emmanuel Delahaye

    "C is a sharp tool"

  3. #3
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by Emmanuel Delaha
    The malloc() thing allocates room for an array of pointers, but the pointers of this array are not initialized. If you want to use them, you also must allocate some memory block and record its address in the array of pointers. You code must be completed.

    Pointers are the tricky point of the C language. I suggest you work on it mre deeply before going further. It must be crystal clear in your mind.

    Well I understand pointer basics, it's just the double whammies confuse me because it makes sense to me, just not to the compiler! Agh..

  4. #4
    Quote Originally Posted by Kleid-0
    Well I understand pointer basics, it's just the double whammies confuse me because it makes sense to me, just not to the compiler! Agh..
    Would you please copy and paste your code (with comments if you want) so that we can get your original code for chack and test, and not some by-hand-and-full-of-typos copy...

    The 'image' trick is just boring (and expensive in terms of disk space)
    Emmanuel Delahaye

    "C is a sharp tool"

  5. #5
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by Emmanuel Delaha
    Would you please copy and paste your code (with comments if you want) so that we can get your original code for chack and test, and not some by-hand-and-full-of-typos copy...

    The 'image' trick is just boring (and expensive in terms of disk space)
    I have no typos for one (because I am perfect!). I thought you guys would like the anti-aliased color coating! & plus some lucky HTML folks might see that I'm using GIF, remember to use GIF on images with not that many colors! I could kind of understand on disk space, but now'a days we've got like 80 GB hd's. Plus it's good typing practice! And it's not a by-hand copy, it's completely electronic! I don't understand! I know a pointer wiz can do this, BE the compiler, I have faith in you Emmanuel, you are the compiler soldier! I'm here for you every step!

  6. #6
    Quote Originally Posted by Kleid-0
    I have no typos for one (because I am perfect!).
    I don't doubt you are perfect, but I know I'm not. This is why I expect your code to be in a text mode because I am certainely not going to retype it. About the colors, my editor is big enough to colorize the C source according to my standards.
    Emmanuel Delahaye

    "C is a sharp tool"

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Hey long time no see Emmanuel

  8. #8
    Quote Originally Posted by Thantos
    Hey long time no see Emmanuel
    I'm glad you missed me. I was a little too busy. I'll try to come here a little more often...

    See ya!
    Emmanuel Delahaye

    "C is a sharp tool"

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The first thing I notice is that you are casting the return value of malloc(). This is a big no no. Also where are you #include's? If you are getting errors about the void* then you are compiling it as c++ and should be using new and delete instead. Speaking of delete where are you calling free to deallocate the memory you malloc'd?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Here's a tip - copy/paste your code from your text editor into a set of [code][/code] tags.
    sure it looks pretty, but it is absolutely USELESS to everyone else who want's to copy/paste your code for experiment or comment.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > char **Images = (char**)malloc(512);
    1. Remove the cast (see the FAQ)
    2. Add #include <stdlib.h>
    3. Use a C compiler (not a C++ compiler which your code seems to need)
    4. the size is totally bogus

    Code:
    char **Images = malloc( 512 * sizeof *Images );
    > for(i=0; i<sizeof(Images); i++) {
    What exactly are you hoping to achieve with this?
    If it's an attempt to count the number of strings, then it fails miserably.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Man, is that all the code is supposed to do?
    It's like a 5-second job in a shell script.

    Code:
    files="guard lobo"
    for i in $files ; do
      wget http://www.fffin.com/ikonit/ff6/enemies/$i.gif
    done

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    >Man, is that all the code is supposed to do?
    >It's like a 5-second job in a shell script.

    Heh. Well there is the whole learning experience too ya know.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > Well there is the whole learning experience too ya know.
    Yes, futility is a lesson I suppose - Like cutting the lawn with scissors.

    Another being recognising the best tool for the job.

  15. #15
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Thank you guys for all of that help! (Especially the shell script!). So i was working on Version 3 of the C programmed get Images program, and BAM! Segment fault, so I laughed at the screen thinking it would be incredibly easy for me to solve this sucker--it wasn't! I know where the segment fault is, & it is highlighted in the source of the txt file that I uploaded. free() just doesn't like to be free! :(.

    Could someone help me out with this segment fault?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM