Thread: problems with reacating memory

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    11

    Smile problems with reallocating memory

    hi ppl,

    i have this problem with reallocating memory. First to mention that I'm a beginner



    so the problem is, when i do this :

    Code:
    typedef struct xxx{
    	char **test;
    } XX;
    
    
    XX *var;
    var = (XX *)malloc(sizeof(XX))
    var->test = (char **)malloc(sizeof(char *));
    //and then in the loop i would like to extend my var->test 
    while(){
    var->test = (char **)realloc(var->test,20*sizeof(char *));
    }
    it does not work (Segmentation fault) if i allocate the sizeof(char *) in the beginning and then try to realloc the pointer by 20 ( keap in mind that a already have some data in the var->test[i] (i = 0..2) )
    Code:
    var->test = (char **)realloc(20*sizeof(char *))
    but if i allocate the (
    Code:
    var->test = (char **)malloc(20*sizeof(char *))
    ) in the beginning then it works (at least i think so).

    how do U reallocate the size of the pointer to a char pointer so that i can keap adding characters to my
    Code:
    var->test[i]
    arrays


    thank you

    baxy
    Last edited by baxy77bax; 04-03-2011 at 12:00 PM.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You say you have data in var->test[i] with i being in the range 0-2 (at least, I think you're saying that). But you only allocated space for one char*, so accessing var->test[1] and [2] is illegal. You very likely destroyed some memory that your allocator depends on.

    I'm not sure if you're aware of this, either, but realloc() isn't used (solely) to increase the amount of allocated memory, but rather to allocate a different amount. So:
    Code:
    x = malloc(50);
    x = realloc(x, 100);
    Now you have 100 bytes, not 150.

    Also, in the future, please do not describe a problem by saying "it does not work". This phrase is next to meaningless. You should explain how it doesn't work.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    11
    Quote Originally Posted by cas View Post
    But you only allocated space for one char*, so accessing var->test[1] and [2] is illegal.

    Ok, this is embarrassing, thanks for noticing, actually i just wrote that without paying to much attention to what exactly i am writing, but that is the case. I was reallocating the size of sub-arrays (second dimension) but not the first ( at least not correctly) and that is why the 'Segmentation fault' kept appearing over and over again.

    Thanks

    baxy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory problems with map(), need an urgent reply
    By abdulazizrehan in forum C++ Programming
    Replies: 9
    Last Post: 04-24-2010, 01:06 PM
  2. Problem with custom dynamic memory allocation routines
    By BLauritson in forum C++ Programming
    Replies: 12
    Last Post: 03-11-2010, 07:26 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  4. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  5. Weird memory problems (MS-VC++)
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2006, 08:01 AM