Segment fault on double whammy char pointer

This is a discussion on Segment fault on double whammy char pointer within the C Programming forums, part of the General Programming Boards category; I just don't understand what I'm really doing with the malloc double pointer char initialization! Aggghh.. The error is segment ...

  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!
    Attached Images Attached Images  
    Last edited by Kleid-0; 12-19-2004 at 10:27 PM. Reason: I forgot to say my error message! And I forgot to say Merry Christmas!

  2. #2
    Helper
    Join Date
    Jun 2004
    Posts
    255
    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
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Hey long time no see Emmanuel

  4. #4
    Helper
    Join Date
    Jun 2004
    Posts
    255
    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"

  5. #5
    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..
    Attached Images Attached Images  

  6. #6
    Helper
    Join Date
    Jun 2004
    Posts
    255
    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"

  7. #7
    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!

  8. #8
    Helper
    Join Date
    Jun 2004
    Posts
    255
    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"

  9. #9
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Now this thread looks ugly! :(

    Code:
    #define THE_START "wget http://www.fffin.com/ikonit/ff6/enemies/"
    #define THE_END ".gif"
    
    int main(int args, char **argc) {
    	char **Images = (char**)malloc(512);
    	(*Images) = (char*)malloc(255);
    	(*Images+1) = (char*)malloc(255);
    	strcpy((*Images), "guard");					
    	strcpy((*Images)+1, "lobo");
    	char *szExec = (char*)malloc(255);
    	int i;
    	for(i=0; i<sizeof(Images); i++) {
    		strcpy(szExec, THE_START);
    		strcat(szExec, Images[i]);
    		strcat(szExec, THE_END);
    		//system(szExec);
    		printf("%s", szExec);
    	}
    }

  10. #10
    & 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?

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    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.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    > 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.

  13. #13
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Kleid-0
    Now this thread looks ugly!

    Code:
    #define THE_START "wget http://www.fffin.com/ikonit/ff6/enemies/"
    #define THE_END ".gif"
    
    int main(int args, char **argc) {
    	char **Images = (char**)malloc(512);
    	(*Images) = (char*)malloc(255);
    	(*Images+1) = (char*)malloc(255);
    	strcpy((*Images), "guard");					
    	strcpy((*Images)+1, "lobo");
    	char *szExec = (char*)malloc(255);
    	int i;
    	for(i=0; i<sizeof(Images); i++) {
    		strcpy(szExec, THE_START);
    		strcat(szExec, Images[i]);
    		strcat(szExec, THE_END);
    		//system(szExec);
    		printf("%s", szExec);
    	}
    }
    Try that, and ask for details if you don't understand.
    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define THE_START "wget http://www.fffin.com/ikonit/ff6/enemies/"
    #define THE_END ".gif"

    int main ()
    {
       
    size_t nb 2;
       
    char **Images malloc (nb sizeof Images);

       if (
    Images != NULL)
       {
          
    Images[0] = malloc (64);

          if (
    Images[0] != NULL)
          {
             
    Images[1] = malloc (64);

             if (
    Images[1] != NULL)
             {
                
    strcpy ((Images[0]), "guard");
                
    strcpy (Images[1], "lobo");
                {
                   
    char *szExec malloc (255);
                   
    size_t i;

                   for (
    0nbi++)
                   {
                      
    strcpy (szExecTHE_START);
                      
    strcat (szExecImages[i]);
                      
    strcat (szExecTHE_END);
                      
    /* system(szExec); */
                      
    printf ("%s\n"szExec);
                   }
                }
                
    free (Images[1]), Images[1] = NULL;
             }
             
    free (Images[0]), Images[0] = NULL;
          }
          
    free (Images), Images NULL;
       }
       
       
    /* Dev-C++ trick */
       
    system ("pause");
       return 
    0;

    It produces:
    Code:
    wget http://www.fffin.com/ikonit/ff6/enemies/guard.gif
    wget http://www.fffin.com/ikonit/ff6/enemies/lobo.gif
    Appuyez sur une touche pour continuer . . .
    Note that if the only purpose of the program is to insert these strings into the url, it could be done with farless malloc() things. A simple array of strings would suffice...
    PHP Code:
    static char const *Images[] = 
    {
       
    "guard",
       
    "lobo"
    }; 
    Last edited by Emmanuel Delaha; 12-20-2004 at 01:54 AM.
    Emmanuel Delahaye

    "C is a sharp tool"

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    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

  15. #15
    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.

Page 1 of 2 12 LastLast
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, 12: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

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