Thread: problem with realloc

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    3

    problem with realloc

    hi there i got the following code and on the second run of the resizer function it crashes at position "*pointer[outer][inner] = 0;" don't really know why thought the memory was allocated for this a few lines above :/
    would be great if anybody could help

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int resizer(int*** pointer, int outer, int inner, int outerSize, int innerSize, int isNFirst);
    
    int main(){
        int counter = 0;
        int counter2 = 0;
        int** intP = NULL;
        while(counter < 50){
            if(counter2 == 10){
                counter = counter + 1;
                counter2 = 0;
            }
            resizer(&intP, counter, counter2, 0, 0, 0);
            counter2 = counter2 + 1;
        }
        return 0;
    }
    
    
    
    int resizer(int*** pointer, int outer, int inner, int outerSize, int innerSize, int isNFirst){
        int** tmpIntP = NULL;
        int* tmpInt = NULL;
        int tmpCoutner = 0;
        if( outerSize != outer + 1 ){
            tmpIntP = (int**)realloc(*pointer, (sizeof(int*) * (outer + 1)));
            if( tmpIntP == NULL ){
                return 1;
            }
            if( outer != 0 ){
                tmpIntP[outer] = NULL;
            }
            *pointer = tmpIntP;
        }
        if( isNFirst == 0 || innerSize < inner + 1){
            *pointer[outer] = NULL;
        }
        if( innerSize != inner + 1){
            tmpInt = (int*)realloc(*pointer[outer], (sizeof(int) * (inner + 1)));
            if( tmpInt == NULL ){
                return 1;
            }
            *pointer[outer] = tmpInt;
        }
        *pointer[outer][inner] = 0;
        return 0;
    }
    greets flasche

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Your variable names are really cryptic.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I do not really understand your logic, but your syntax probably has the following error:

    (*pointer)[outer]

    red is missing
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also, if this is C (as opposed to C++), then there should be no need to cast the result of realloc():
    Code:
         tmpIntP = (int**)realloc(*pointer, (sizeof(int*) * (outer + 1)));
    If it's C++, then you should be using C++ style new/delete, and again no need for a cast.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    3
    thanks alot guys the braces where mssing... that kept me gioing like forever...
    and it is c code and i know i dont have to cast the pointer but i tried everything to solve the problem :/
    like is said THANKS ALOT

    ahh damit ... that not quit it... ok i tryed to compress the code more.... i want to allocate a dynamic 2 dimensional array with the code with variaable sizes in each rows

    Code:
    int main(){
        int counter = 0;
        int counter2 = 0;
        int** pointer = NULL;
        int** intPP = NULL;
        int* intP = NULL;
        int** tmpP = NULL;
        while(counter < 50){
            printf("inner &#37;i, outer%i\n", counter2, counter);
            if(counter2 == 10){
                counter = counter + 1;
                counter2 = 0;
            }
            resizer(&tmpP, counter, counter2, -1, -1, 0);
            counter2 = counter2 + 1;
        }
        return 0;
    }
    
    
    int resizer(int*** pointer, int outer, int inner, int outerSize, int innerSize, int isFirst){
        printf("-----------\tresizer( pointer = %p, outer = %i, inner = %i, outerS = %i, innerS = %i, isNFirst = %i)\t--------------\n", *pointer, outer, inner, outerSize, innerSize,
    isFirst);
        int counter = outerSize;
        int counter2 = innerSize;
        if( inner == innerSize && outer == outerSize){
            return 0;
        }
        int** intPP = NULL;
        int* intP = NULL;
        if( outer != outerSize ){
            intPP = realloc(*pointer, (sizeof(int*) * (outer + 1)));
            if( intPP == NULL){
                return 1;
            }
            *pointer = intPP;
            if( isFirst != 0 ){
                (*pointer)[outer] = NULL;
            }
        }
        if( inner != innerSize ){
            intP = realloc((*pointer)[outer], (sizeof(int) * (inner + 1)));
            if( intP == NULL){
                return 1;
            }
            (*pointer)[outer] = intP;
            (*pointer)[outer][inner] = 0;
        }
        return 0;
    }
    Last edited by oooflascheooo; 07-31-2008 at 03:23 AM.

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    3
    ok finally found a solution thx again for all who helped

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. using realloc
    By bobthebullet990 in forum C Programming
    Replies: 14
    Last Post: 12-06-2005, 05:00 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM