Thread: char** in struct amd malloc()

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    char** in struct and malloc()

    Hi, I am trying to pass data to a .DLL I want to use. DLL accepts this structure for its data.

    Code:
    PTR 01FB317C    -> 01EEB208 -> ASCII "DATA0"
                       018947A0 ->          01F72f30 -> ASCII "DATA1"
                                     +8     01F74558 -> ASCII "DATA2"
                                     +12    01F742E8 -> ASCII "DATA3"
               			 +16    01F73FF8 -> ASCII "DATA4"
                                     +20    01FD3CF8 -> ASCII "DATA5"
              		         +24    01FD4EC8 -> ASCII "DATA6"
                			 +28    01F724D8 -> ASCII "DATA7"
                     		 +32             -> ASCII "DATA8"
                			 +36             -> ASCII "DATA9"
                			 +40             -> ASCII "DATA10"
    	    			 +44             -> ASCII "DATA11"
                			 +48             -> ASCII "DATA12"
    Pointers are from a call from real application.ASCII strings are of size 256.


    So i created this structure:


    Code:
    typedef struct
    	{
    	     char *s1;
                 char ** ss1;
    	} Rec1;

    Now I am trying to malloc before I put data on it


    Code:
          Rec1 *a;
    	
            a = (Rec1 *)malloc(sizeof(Rec1));
    	
    	a->s1 = (char*)malloc(16*sizeof(char));
    	
    	strcpy(a->s1, "DATA0"); <--- This works OK as seen on debugger
    	
    	a->ss1 = (char**) malloc(48);  <--- !CRASH!!! ; Trying to allocate space for pointers
    	
    	for( i=0; i < 12; i++)
    	a->ss1[i] = (char *)malloc(256);
    	
    	
    	strcpy(a->ss1[0], "DATA1");
    
            .......
    
    
            free(a);
    	for( i=0; i < 12; i++)
                 free( a->ss1[i] );
    	free( a->ss1 );
    Problem is the above malloc() crashes with C0000005

    Can you help me how is the correct way to do it?


    PS: On 1st declaration of structure it is char *........1; I keep editing the message and it keeps ignoring my edit always showing ......1; !!!! @#!
    PS2: I managed to do it
    Last edited by alexopth1512; 10-27-2010 at 03:56 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It might help if you defined ss1 in your struct.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    OK I solved it.
    Problem created by

    Code:
    strcpy(a->s1, "RI");
    that preceded the malloc()

    Now works like a charm, I ll check the strcpy calls, maybe change them with strncpy

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    In C there is no typecast for malloc needed. For constants you should use defines.
    Code:
    #define MAXSTRSIZE 257
    #define NUMPOINTER 12
    
          Rec1 *a;
    	
            a = malloc(sizeof*a);
    	
    	a->s1 = malloc(MAXSTRSIZE);
    	
    	strcpy(a->s1, "DATA0"); <--- This works OK as seen on debugger
    	
    	a->ss1 = malloc(NUMPOINTER * sizeof *a->ss1); 
    	
    	for( i=0; i < NUMPOINTER ; i++)
    	  a->ss1[i] = malloc(MAXSTRSIZE);
    	
    	
    	strcpy(a->ss1[0], "DATA1");
    
            .......
    
    
    	for( i=0; i < NUMPOINTER ; i++)
                 free( a->ss1[i] );
    	free( a->ss1 );
            free(a->s1);
            free(a);

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > a->ss1 = (char**) malloc(48); <--- !CRASH!!! ; Trying to allocate space for pointers
    Assuming pointers have a particular size, and not using sizeof is the biggest problem here.

    BillyTKid's answer looks about right.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Thanx guys for your help.
    malloc(48) was changed for test, i use now sizeof.
    everything works fine now, i changed strcpy with strncpy.
    Concerning typecast i dont seem to be able to get rid of it, compiler throws error, i use MS Visual C++ 2010 express

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    On VStudio you must work with *.c files for C programming, otherwise your code will compiled as C++!

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Quote Originally Posted by BillyTKid View Post
    On VStudio you must work with *.c files for C programming, otherwise your code will compiled as C++!
    You are very correct!

    But you see, i am not a professional C programmer, i just make some utilities to make my work life easier. However I need it more and more.
    This little project is finished now and I am so happy
    On first i tried .c files but compiling failed, i guess there must be some options i have to set. I also dont know how to get rid of debugging library it needs all the time. It is very useful for debugging purposes but later I guess there is no need for debugging info.
    Anyway,
    I have searched for the topic I asked and couldn't find really useful info, I hope that this thread will help others.
    I wish I had more time to watch the forum but I have so little free time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. casting struct to struct
    By klmdb in forum C Programming
    Replies: 6
    Last Post: 08-14-2010, 02:29 PM
  2. C problem - Create a Personal Library
    By Harliqueen in forum C Programming
    Replies: 33
    Last Post: 04-20-2010, 11:27 PM
  3. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 05:38 PM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 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

Tags for this Thread