Thread: Reallocing help

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    21

    Unhappy Reallocing help

    I am trying to split a string inputted into a 3d array, is soon as a new entry has been found when strtoking it then reallocing will come into play to compensate for the entry.

    If it detects a '>' in the string inputted, the row will increase by 1 and realloc the array.
    If it detects a another entry delimited by a ' ' space, it will realloc the element count enclosed in each row by 1.

    I want to use dynamic memory allocation so that unlim entries can be added to the memory.

    E.g: input= 12 22 33 44 53 > 111 233 31 4 25 33 11 > 24 32 24 3 21 32

    First block
    First entry: [0][0]
    Second entry: [0][1]
    .....

    Second block
    First entry: [1][0]
    Second entry: [1][1]
    .....

    etc.....

    Code:
    /* Headers here	*/
    #define ROWS 1
    
    main(int argc, char **argv)
    
    {
    
    	char ***commands;
    	char *temp;
    	char *split =NULL;
    	int commandinc =0,element=0;
    
    
    	/*	File read into the temp variable here	*/
    
    	/*	Malloc a 1 row array here	*/
    	commands = malloc(ROWS*sizeof(char *));
    
    	split = strtok(temp, " ");
    	
    	while(split != NULL )
    
    	{
    	
    		if(strcmp(split,">") != NULL)
    		{
    			element =0;
    			/* Add new row by one [e.g: commands[1] */
    			commandinc++;
    		}
    
    		/* Realloc the element count and add a element to the row*/
    		commands[commandinc][element] = line;
    		split = strtok(NULL, "\n");
    
    		element++;
    	}	
    return 0;
    }
    Any help will be greatly appreciated, thanks

    it01y2

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You have a neat idea but didn't mention a problem.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    21

    Problem

    My real problem is the realloc and mallocing issue, I am not sure what i am doing wrong, I have used various commands but keep on getting malloc/realloc errors.

    Can i PM you my full code to test and look at, as this is the only issue i have in my code.

    If i use a static array i do not get this issue but I am limited to a certain ammount of space.

    Thanks, your help will be greatly appreciated.

    it01y2

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    type *p1, *p2;
    ...
    ptr = malloc( foo )
    ...
    p2 = realloc( p1, bar )
    if( p2 )
        p1 = p2;
    There's a general rundown of how realloc works. What sort of issue are you having using it? You do know that strtok doesn't actually allocate any space, right? That's probably what your issue is. At least that's what I'd guess by looking at what you're showing.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    21

    Realloc

    I have left out realloc as I am unsure how to use it correctly, I have looked at the man pages but cant implement it a 3d array effectively so it works.

    I keep on getting various memory related errors which I am not sure what they mean.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by it01y2 View Post
    My real problem is the realloc and mallocing issue, I am not sure what i am doing wrong, I have used various commands but keep on getting malloc/realloc errors.

    Can i PM you my full code to test and look at, as this is the only issue i have in my code.
    No, sorry. You do not have to use that much code to test realloc() -- which you didn't in the first post.

    Anyway, here's an example of using realloc() to grow an int array 5 units at a time:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
    	int i, *array = malloc(sizeof(int)*5), *tmp;
    
    	for (i=1;i<5;i++) {
    		tmp = (array = realloc(array, (5+i*5)*sizeof(int)));   // typo see #8
    		if (!tmp) {
    			puts("OUT OF MEMORY!");
    			return -1;
    		}
    		array = tmp;
    	}
    
    	free(array);
    	return(0);
    }
    Last edited by MK27; 02-22-2010 at 06:42 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    21

    Bug

    Running the code, I get this error, I think that reallocating is not working:

    I have previously malloced the ammount of commands to 2, realloc should come into play and allow the extra commands to be placed into the memory, but it isnt.

    Code:
    sh: ls -l -l -l -l -l
    *** glibc detected *** ./sh: free(): invalid next size (fast): 0x0000000000777b30 ***
    ======= Backtrace: =========
    /lib/libc.so.6[0x7f554186edd6]
    /lib/libc.so.6(cfree+0x6c)[0x7f554187374c]
    /lib/libreadline.so.5(rl_delete_text+0xa8)[0x7f5541b90998]
    /lib/libreadline.so.5(rl_do_undo+0x190)[0x7f5541b8d8b0]
    /lib/libreadline.so.5(rl_revert_line+0x15)[0x7f5541b8d9b5]
    /lib/libreadline.so.5(readline_internal_teardown+0x67)[0x7f5541b7b847]
    /lib/libreadline.so.5(readline+0x64)[0x7f5541b7bb54]
    ./sh[0x40175a]
    /lib/libc.so.6(__libc_start_main+0xfd)[0x7f5541817abd]
    ./sh[0x400d99]
    ======= Memory map: ========
    00400000-00402000 r-xp 00000000 08:01 180380                             /home/user/Downloads/sh
    00601000-00602000 r--p 00001000 08:01 180380                             /home/user/Downloads/sh
    00602000-00603000 rw-p 00002000 08:01 180380                             /home/user/Downloads/sh
    0075b000-0077c000 rw-p 00000000 00:00 0                                  [heap]
    7f553c000000-7f553c021000 rw-p 00000000 00:00 0 
    7f553c021000-7f5540000000 ---p 00000000 00:00 0 
    7f554119b000-7f55411b1000 r-xp 00000000 08:01 5627                       /lib/libgcc_s.so.1
    7f55411b1000-7f55413b0000 ---p 00016000 08:01 5627                       /lib/libgcc_s.so.1
    7f55413b0000-7f55413b1000 r--p 00015000 08:01 5627                       /lib/libgcc_s.so.1
    7f55413b1000-7f55413b2000 rw-p 00016000 08:01 5627                       /lib/libgcc_s.so.1
    7f55413b2000-7f55413b4000 r-xp 00000000 08:01 5489                       /lib/libdl-2.10.1.so
    7f55413b4000-7f55415b4000 ---p 00002000 08:01 5489                       /lib/libdl-2.10.1.so
    7f55415b4000-7f55415b5000 r--p 00002000 08:01 5489                       /lib/libdl-2.10.1.so
    7f55415b5000-7f55415b6000 rw-p 00003000 08:01 5489                       /lib/libdl-2.10.1.so
    7f55415b6000-7f55415f4000 r-xp 00000000 08:01 1954                       /lib/libncurses.so.5.7
    7f55415f4000-7f55417f4000 ---p 0003e000 08:01 1954                       /lib/libncurses.so.5.7
    7f55417f4000-7f55417f8000 r--p 0003e000 08:01 1954                       /lib/libncurses.so.5.7
    7f55417f8000-7f55417f9000 rw-p 00042000 08:01 1954                       /lib/libncurses.so.5.7
    7f55417f9000-7f554195f000 r-xp 00000000 08:01 5457                       /lib/libc-2.10.1.so
    7f554195f000-7f5541b5e000 ---p 00166000 08:01 5457                       /lib/libc-2.10.1.so
    7f5541b5e000-7f5541b62000 r--p 00165000 08:01 5457                       /lib/libc-2.10.1.so
    7f5541b62000-7f5541b63000 rw-p 00169000 08:01 5457                       /lib/libc-2.10.1.so
    7f5541b63000-7f5541b68000 rw-p 00000000 00:00 0 
    7f5541b68000-7f5541b9e000 r-xp 00000000 08:01 4876                       /lib/libreadline.so.5.2
    7f5541b9e000-7f5541d9d000 ---p 00036000 08:01 4876                       /lib/libreadline.so.5.2
    7f5541d9d000-7f5541d9f000 r--p 00035000 08:01 4876                       /lib/libreadline.so.5.2
    7f5541d9f000-7f5541da5000 rw-p 00037000 08:01 4876                       /lib/libreadline.so.5.2
    7f5541da5000-7f5541da6000 rw-p 00000000 00:00 0 
    7f5541da6000-7f5541dc5000 r-xp 00000000 08:01 5391                       /lib/ld-2.10.1.so
    7f5541f68000-7f5541fa7000 r--p 00000000 08:01 13357                      /usr/lib/locale/en_GB.utf8/LC_CTYPE
    7f5541fa7000-7f5541faa000 rw-p 00000000 00:00 0 
    7f5541fb9000-7f5541fba000 rw-p 00000000 00:00 0 
    7f5541fba000-7f5541fc1000 r--s 00000000 08:01 160                        /usr/lib/gconv/gconv-modules.cache
    7f5541fc1000-7f5541fc4000 rw-p 00000000 00:00 0 
    7f5541fc4000-7f5541fc5000 r--p 0001e000 08:01 5391                       /lib/ld-2.10.1.so
    7f5541fc5000-7f5541fc6000 rw-p 0001f000 08:01 5391                       /lib/ld-2.10.1.so
    7fff602c4000-7fff602d9000 rw-p 00000000 00:00 0                          [stack]
    7fff602e7000-7fff602e8000 r-xp 00000000 00:00 0                          [vdso]
    ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
    Aborted

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by it01y2 View Post
    Running the code, I get this error, I think that reallocating is not working:
    Whoops! There is a typo in that post. It runs either way way for me so I didn't catch it, but probably this could make tmp invalid:
    Code:
    tmp = (array = realloc(array, (5+i*5)*sizeof(int)));
    should be:
    Code:
    tmp = realloc(array, (5+i*5)*sizeof(int));
    I added the tmp pointer for error checking secondarily and wasn't paying enough attention. (Generally if you were actually doing something in the loop you would probably catch that, methinks.)

    Hopefully it works now!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    21

    Post Realloc

    I have managed to realloc the elements in the array perfectly, however I am trying still to realloc the rows in the array, i keep on getting on getting a segmentation fault?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    As shown above...
    Code:
    type ** lines, **tmp;
    ...
    lines = malloc( NumStartingLines * sizeof *lines );
    ...
    tmp = realloc( lines, NewNumOfLines * sizeof *lines );
    if( tmp )
        lines = tmp;
    Allocate a new number of pointers, if realloc didn't fail, tmp will not be NULL, so you're ok to use it. If it did fail, you'll still have your old pointer.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Dec 2009
    Posts
    21

    Error

    I have tried the above but keep on getting an error....

    Thanks for the pointers anyway mate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reallocing gives me a segmentation fault...
    By Shogun in forum C Programming
    Replies: 5
    Last Post: 08-08-2003, 01:17 PM