Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Pointers

realloc

  • Please log in to reply
22 replies to this topic

#1 fread

fread

    Programming God

  • Senior Member
  • PipPipPipPipPipPip
  • 897 posts
  • Location:Trinidad and Tobago
  • Learning:C, Java, C++, C#, PHP, Python, PL/SQL

Posted 12 March 2010 - 06:54 PM

I have a long pointer a follows
long **a;

and i do this

a = malloc(x_size * sizeof(long *));

then this

for (i = 0; i < x_size; i++)
    {
        a[i] = malloc(y_size * sizeof(long));
    }
can i in a later part of the program do this

a[i] = realloc(a[i], (atracker[i] + 2) * sizeof(long ));
I am trying to allocate more space in the array for just this specific pointer...cant seem to get it to work. Already made checks for null. If i print the contents of
a[i], it will print out correctly.. but to add new numbers i need to increase the current size. thats where it fails
  • 0

#2 Sysop_fb

Sysop_fb

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 151 posts
  • Location:Missouri

Posted 12 March 2010 - 09:00 PM

I'm not understanding the question you just want to know if you can realloc memory? Yes you can reallocate the block of memory later in your program as long as you have a way to keep track of how big it is.


Also

a[i] = realloc(a[i], (atracker[i] + 2) * sizeof(long ));

That's a bad habbit to get into. realloc isn't required to return a pointer to your old block of code if it fails or free the old block of allocated memory if it fails.
  • 0
"The best optimizer is between your ears" - Michael Abrash
Saying you can optimize a program is like saying you understand how a program works on every level of every facet on a specific machines configuration.

#3 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 12 March 2010 - 10:50 PM

realloc() returns a pointer but it might be null. You should assign the return value to a temporary pointer, check for a null, and if the returned value isn't null, assign that to a.

Edited by dargueta, 21 March 2010 - 04:04 PM.
BBCode fail

  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#4 fread

fread

    Programming God

  • Senior Member
  • PipPipPipPipPipPip
  • 897 posts
  • Location:Trinidad and Tobago
  • Learning:C, Java, C++, C#, PHP, Python, PL/SQL

Posted 13 March 2010 - 04:23 AM

a[i] = realloc(a[i], (atracker[i] + 2) * sizeof(long ));
That's a bad habbit to get into. realloc isn't required to return a pointer to your old block of code if it fails or free the old block of allocated memory if it fails.

Ok, so if i malloc memory for 3 rows of pointers each of which points to a pointer array of long integers, if i decide to increase the length of one of the pointer arrays with realloc, u are saying that if realloc moves this block of memory in order to create more space it may not return a pointer to my old one. One of which is fixed in my three rows of pointers...

More or less the idea is creating a 2d array where some columns grow dynamically. For example row one has 10 columns, row two may have 100 and row 3 may have 2000. But none of this is know at compile time.
  • 0

#5 Sysop_fb

Sysop_fb

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 151 posts
  • Location:Missouri

Posted 13 March 2010 - 04:56 AM

No I'm saying if the call fails it's not required to clean up the already allocated memory block for you.
So you have a[i] pointing at a block of memory. realloc fails, doesn't clean up the old allocation and returns null. Now a[i] is pointing at null and you have no way to free the old memory.
Dargueta already explained how to fix it.

From the man page.
The [B]realloc[/B]() function returns a pointer, possibly identical to ptr, to
     the allocated memory if successful; otherwise a NULL pointer is returned,
     and errno is set to ENOMEM if the error was the result of an allocation
     failure.  The [B]realloc[/B]() function always leaves the original buffer intact
     when an error occurs.




*Stop reading if you don't want to look under the hood*


Now for implementation specifics..
Realloc just extends your memory allocated, it doesn't copy data. If the memory can't be extended it does nothing. realloc can also 'shrink' the memory (actually to the point of freeing it or reallocing to 0) however since realloc doesn't copy data if the new memory area is somewhere other then the old area your data is now gone. On a windows platform you're less then likely to ever run into this problem but on other systems you could since now we're getting into the world of implementation specific.
  • 0
"The best optimizer is between your ears" - Michael Abrash
Saying you can optimize a program is like saying you understand how a program works on every level of every facet on a specific machines configuration.

#6 fread

fread

    Programming God

  • Senior Member
  • PipPipPipPipPipPip
  • 897 posts
  • Location:Trinidad and Tobago
  • Learning:C, Java, C++, C#, PHP, Python, PL/SQL

Posted 13 March 2010 - 05:06 AM

I am not evening getting to trap the return of realloc; the program crashes on that realloc line. I tried to trap with
long * temp = realloc(blah...)
This is the output when the program executes that line:
Apps$ ./out
*** glibc detected *** ./out: realloc(): invalid next size: 0x081cc2f0 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7eea604]
/lib/tls/i686/cmov/libc.so.6[0xb7eee1b1]
/lib/tls/i686/cmov/libc.so.6(realloc+0x106)[0xb7eeeee6]
/lib/tls/i686/cmov/libc.so.6(realloc+0x42)[0xb7eeee22]
./out[0x80485fc]
./out[0x8048a5e]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe5)[0xb7e91775]
./out[0x80484f1]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:11 607        /media/05788052/school/Apps/out
08049000-0804a000 r--p 00000000 08:11 607        /media/05788052/school/Apps/out
0804a000-0804b000 rw-p 00001000 08:11 607        /media/05788052/school/Apps/out
081cc000-081ed000 rw-p 081cc000 00:00 0          [heap]
b7d00000-b7d21000 rw-p b7d00000 00:00 0 
b7d21000-b7e00000 ---p b7d21000 00:00 0 
b7e7a000-b7e7b000 rw-p b7e7a000 00:00 0 
b7e7b000-b7fd7000 r-xp 00000000 08:05 2657       /lib/tls/i686/cmov/libc-2.9.so
b7fd7000-b7fd8000 ---p 0015c000 08:05 2657       /lib/tls/i686/cmov/libc-2.9.so
b7fd8000-b7fda000 r--p 0015c000 08:05 2657       /lib/tls/i686/cmov/libc-2.9.so
b7fda000-b7fdb000 rw-p 0015e000 08:05 2657       /lib/tls/i686/cmov/libc-2.9.so
b7fdb000-b7fde000 rw-p b7fdb000 00:00 0 
b7fe1000-b7fee000 r-xp 00000000 08:05 2601       /lib/libgcc_s.so.1
b7fee000-b7fef000 r--p 0000c000 08:05 2601       /lib/libgcc_s.so.1
b7fef000-b7ff0000 rw-p 0000d000 08:05 2601       /lib/libgcc_s.so.1
b7ff0000-b7ff3000 rw-p b7ff0000 00:00 0 
b7ff3000-b7ff4000 r-xp b7ff3000 00:00 0          [vdso]
b7ff4000-b8010000 r-xp 00000000 08:05 11185      /lib/ld-2.9.so
b8010000-b8011000 r--p 0001b000 08:05 11185      /lib/ld-2.9.so
b8011000-b8012000 rw-p 0001c000 08:05 11185      /lib/ld-2.9.so
bfd45000-bfd5a000 rw-p bffeb000 00:00 0          [stack]
Aborted

  • 0

#7 Sysop_fb

Sysop_fb

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 151 posts
  • Location:Missouri

Posted 13 March 2010 - 07:21 AM

Going to need to see more of your code but at a guess I'd say somewhere you're writing to memory that you shouldn't be. -buffer overruns-
  • 0
"The best optimizer is between your ears" - Michael Abrash
Saying you can optimize a program is like saying you understand how a program works on every level of every facet on a specific machines configuration.

#8 fread

fread

    Programming God

  • Senior Member
  • PipPipPipPipPipPip
  • 897 posts
  • Location:Trinidad and Tobago
  • Learning:C, Java, C++, C#, PHP, Python, PL/SQL

Posted 13 March 2010 - 09:40 AM

It is actually a bit untidy since since i never finished before time. But i still want to clean it up.
What im trying to solve is the coin problem. The implementation is slightly different for this assignment.

The idea is one finds the best way to make change using a given set of coins. Let say u make change for 1,2,3,...,99,100 and u cannot make change for 101 using the given coins then 100 is the answer..
Showing you piece of the code will not help. So here the whole thing..Bare in mind its still under contruction so there are things that are not that efficient...

#include <stdio.h>
#include <stdlib.h>

#define MaxSize 500
#define true 1
#define false 0;


long getMinimum(long t1, long t2)
{    
    if (t1 > t2)         
        return t2;    
    return t1;
}

long fillPosition(long *atracker, long **a,long temp_array[], int i , int j)
{
    static int count = 0;
    if (i == 1) return j;
    printf("count: %d\n",++count);
    if( atracker[i - 1] >= j) //there exist a value at [i-1][j] in the matrix; we process as normal
    {    
        printf("atracker at i : %d %ld count: %d\n",i,atracker[i - 1],count)    ;
        atracker[i] = atracker[i] + 1;        
        if(j - temp_array[i] < 0) return a[i-1][j];        
        return  getMinimum( a[i-1][j], (a[i][j - temp_array[i]] + 1) );
    }    
    long temp =  a[i][j - temp_array[i]] + 1;
    
    if (a[i] == NULL) printf("null pointer");
    a[i] = realloc(a[i], (atracker[i] + 1) * sizeof(long ));    //make space for the new number
    
    
    atracker[i] = atracker[i] + 1;                            //update the tracker
    
    return 1;
}

void initializeArray(long *atracker,long **a,long n, long k)
{    //N = the amount of different numbers. each in its own row
    //K = the number limit. Used for now. we will dynamically allocate the rest
    
    //printf("xsize: %ld, ysize: %ld\n",n,k);
    long i,j;
    long count = 0;
    for(i = 0; i < n; i++){
        for(j = 0; j <= k; j++)
        {                
                if (i == 1 && j != 0)
                {
                    a[i][j] = j;
                    
                }
                else    
                if (i == 0 || j == 0)
                {
                    a[i][j] = 0;
                    count++;
                }
                else a[i][j] = -1;
                
                
        }    
        //printf("j stopped at: %ld  count: %ld",j,count);    
    }
    //printf("\n");
    atracker[1] = k ;

}


void mergeSort(long A[],long lo,long hi){
  void merge(long [],long ,long, long);
  if (lo < hi){
     long mid = (lo + hi) / 2;  //get the midpoint subscript
     mergeSort(A,lo,mid);      //sort first half
     mergeSort(A,mid+1,hi);    //sort second half
     merge(A,lo,mid,hi);      //merge sorted halves
     }
}
void merge(long A[],long lo,long mid,long hi)
{
  //A[lo..mid] and A[mid+1..hi] are sorted;
  //merge the pieces so that A[lo..hi] are sorted;
    static long T[MaxSize];
    long i = lo;
    long j = mid + 1;
    long k = lo;
  
    while (i <=mid || j <= hi)
    {
        if (i > mid) T[k++] = A[j++];     //A[lo..mid] completely processed
        else if (j > hi) T[k++] = A[i++]; //A[mid+1..hi] completely processed
        else if (A[i] < A[j]) T[k++] = A[i++]; //neither part completed
        else T[k++] = A[j++];
    }
    for(j = lo; j <=hi; j++) A[j] = T[j]; //copy merge elements back to A  
}


void initializeArray_1d(long A[], long lo, long hi, long value)
{
    int i;
    for ( i = 0; i < hi; i++)
        A[i] = value;
}

int main()
{
    FILE * pFile;
    long **a;     /* this is the array name */
    long *atracker;  //used to keep track of the amount on a given row
    long K;
    long N;    
    long y_size;
    long x_size;
    long temp_array[MaxSize];    //used to load stamp values
    
    long i;
    long j;
    
    
    
    pFile = fopen("INPUTC.txt","r");
    if(pFile == NULL)
        perror ("The following error occurred: ");
        
    fscanf(pFile,"%ld",&K);
    fscanf(pFile,"%ld",&N); 
    
    x_size = N + 1;
    y_size = K + 1;
    
    //printf("xsize: %ld, ysize: %ld\n",x_size,y_size);
    
    /*  allocate storage for an array of pointers */
    a = malloc(x_size * sizeof(long *));
    atracker = malloc(x_size * sizeof(long));        //initial amount is set to the amount of coins
    
    /* for each pointer, allocate storage for an array of ints */
    for (i = 0; i < x_size; i++)
    {
        a[i] = malloc(y_size * sizeof(long));
    }
        
    initializeArray_1d(temp_array,0,MaxSize,10001);    
    initializeArray_1d(atracker,0,y_size,0);
    initializeArray(atracker,a,x_size,y_size);
    
    
    //printf("%ld \n",atracker[1]);
    temp_array[0] = 0;
    for(i = 1; i <= x_size; i++)
    {
        fscanf(pFile,"%ld",&temp_array[i]); 
    }    
    mergeSort(temp_array,0,MaxSize -1 );      
    
    /* Copy temp array into ist column of 2d array starting at pos 1 
    for(i = 1; i < x_size; i++)
    {
        a[i][0] = temp_array[i-1];
    }*/
    
    //for(i = 0; i < x_size; i++)
        //printf("%ld  %ld \n",i,temp_array[i]);
        
        
    long hold = 0;
    /*//i = 11;*/
    for(i = 2; i <= x_size -1;i++)
    //for(i = 2; i < 9; i++)
    {
        j = 1;
        hold = 0;
        while(hold <= K)
        {  

            hold = fillPosition(atracker,a,temp_array,i,j);
            a[i][j] = hold;
            j++;                        
        
        }
    }
    
    
    
    
    
        
        
    /*
    long t = 0;
    for(i = 0; i < x_size; i++)
    {
        printf("array[%ld][%ld]  = %ld \n",i,t,temp_array[i]);
    }    */
    
    /**/
    for(i = 0; i < x_size; i++)
    {
        for (j = 0; j < atracker[i]; j++)
            printf("%ld ",a[i][j]);
        printf("\n\n");
    }
    
    free(a);
    fclose (pFile);
    return 0;
}
and the input file would look something like

40 3
1 2 5

where 1 2 5 are the coins that can be used...at any given point in time u may never used more than 40 coins and 3 the amount of coins given.
  • 0

#9 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 13 March 2010 - 01:34 PM

Ok, here's what you need to do:


  • Get valgrind. If you're running Ubuntu you can get it by executing sudo apt-get install valgrind.
  • Recompile your program with gcc and make sure you pass the -pg option. This will profile your code at runtime. Do not run it yet.
  • Run your program using valgrind. I recommend you call it like this:
    valgrind -v --log-file=mylogfile.txt --leak-check=full --track-origins=yes yourprogram <your program'soptions>
It's going to run a lot slower, but you'll be able to figure out what's going wrong much better.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#10 fread

fread

    Programming God

  • Senior Member
  • PipPipPipPipPipPip
  • 897 posts
  • Location:Trinidad and Tobago
  • Learning:C, Java, C++, C#, PHP, Python, PL/SQL

Posted 14 March 2010 - 06:43 AM

im not to familiar with debugging at this level...so im not very clear on interpreting what im seeing... the program seems to be caught in an infinte loop. Judging from a print statement. If that is infact the same print statement. it prints
count: i
and i goes from some small values and increases without stop...this is the output from the log file

==18678== Memcheck, a memory error detector.
==18678== Copyright (C) 2002-2008, and GNU GPL'd, by Julian Seward et al.
==18678== Using LibVEX rev 1884, a library for dynamic binary translation.
==18678== Copyright (C) 2004-2008, and GNU GPL'd, by OpenWorks LLP.
==18678== Using valgrind-3.4.1-Debian, a dynamic binary instrumentation framework.
==18678== Copyright (C) 2000-2008, and GNU GPL'd, by Julian Seward et al.
==18678== 
==18678== My PID = 18678, parent PID = 3800.  Prog and args are:
==18678==    ./out
==18678== 
--18678-- 
--18678-- Command line
--18678--    ./out
--18678-- Startup, with flags:
--18678--    --suppressions=/usr/lib/valgrind/debian-libc6-dbg.supp
--18678--    -v
--18678--    --log-file=mylogfile.txt
--18678--    --leak-check=full
--18678--    --track-origins=yes
--18678-- Contents of /proc/version:
--18678--   Linux version 2.6.28-18-generic (buildd@rothera) (gcc version 4.3.3 (Ubuntu 4.3.3-5ubuntu4) ) #59-Ubuntu SMP Thu Jan 28 01:23:03 UTC 2010
--18678-- Arch and hwcaps: X86, x86-sse1-sse2
--18678-- Page sizes: currently 4096, max supported 4096
--18678-- Valgrind library directory: /usr/lib/valgrind
--18678-- Reading syms from /lib/ld-2.9.so (0x4000000)
--18678-- Reading debug info from /lib/ld-2.9.so ..
--18678-- .. CRC mismatch (computed 0755dd8f wanted fd1af95b)
--18678--    object doesn't have a symbol table
--18678-- Reading syms from /media/05788052/school/Apps/out (0x8048000)
--18678-- Reading syms from /usr/lib/valgrind/x86-linux/memcheck (0x38000000)
--18678--    object doesn't have a dynamic symbol table
--18678-- Reading suppressions file: /usr/lib/valgrind/debian-libc6-dbg.supp
--18678-- Reading suppressions file: /usr/lib/valgrind/default.supp
--18678-- Reading syms from /usr/lib/valgrind/x86-linux/vgpreload_core.so (0x4020000)
--18678-- Reading syms from /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so (0x4023000)
--18678-- Reading syms from /lib/tls/i686/cmov/libc-2.9.so (0x403f000)
--18678-- Reading debug info from /lib/tls/i686/cmov/libc-2.9.so ..
--18678-- .. CRC mismatch (computed 6de3199f wanted 8d898f0d)
--18678--    object doesn't have a symbol table
--18678-- REDIR: 0x40b2600 (calloc) redirected to 0x4024fd0 (calloc)
--18678-- REDIR: 0x40b6a60 (rindex) redirected to 0x4027330 (rindex)
--18678-- REDIR: 0x40b2930 (malloc) redirected to 0x4026f20 (malloc)
==18678== Invalid write of size 4
==18678==    at 0x8048B3B: initializeArray_1d (in /media/05788052/school/Apps/out)
==18678==    by 0x8048C6D: main (in /media/05788052/school/Apps/out)
==18678==  Address 0x41a3eb8 is 0 bytes after a block of size 16 alloc'd
==18678==    at 0x4026FDE: malloc (vg_replace_malloc.c:207)
==18678==    by 0x8048BF2: main (in /media/05788052/school/Apps/out)
==18678== 
==18678== Invalid write of size 4
==18678==    at 0x804893D: initializeArray (in /media/05788052/school/Apps/out)
==18678==    by 0x8048C8D: main (in /media/05788052/school/Apps/out)
==18678==  Address 0x41a3f8c is 0 bytes after a block of size 164 alloc'd
==18678==    at 0x4026FDE: malloc (vg_replace_malloc.c:207)
==18678==    by 0x8048C17: main (in /media/05788052/school/Apps/out)
==18678== 
==18678== Invalid write of size 4
==18678==    at 0x8048919: initializeArray (in /media/05788052/school/Apps/out)
==18678==    by 0x8048C8D: main (in /media/05788052/school/Apps/out)
==18678==  Address 0x41a4064 is 0 bytes after a block of size 164 alloc'd
==18678==    at 0x4026FDE: malloc (vg_replace_malloc.c:207)
==18678==    by 0x8048C17: main (in /media/05788052/school/Apps/out)
==18678== 
==18678== Invalid write of size 4
==18678==    at 0x804895D: initializeArray (in /media/05788052/school/Apps/out)
==18678==    by 0x8048C8D: main (in /media/05788052/school/Apps/out)
==18678==  Address 0x41a413c is 0 bytes after a block of size 164 alloc'd
==18678==    at 0x4026FDE: malloc (vg_replace_malloc.c:207)
==18678==    by 0x8048C17: main (in /media/05788052/school/Apps/out)
--18678-- REDIR: 0x40bb390 (strchrnul) redirected to 0x40286c0 (strchrnul)
==18678== 
==18678== Invalid read of size 4
==18678==    at 0x8048814: fillPosition (in /media/05788052/school/Apps/out)
==18678==    by 0x8048D3D: main (in /media/05788052/school/Apps/out)
==18678==  Address 0x41a4064 is 0 bytes after a block of size 164 alloc'd
==18678==    at 0x4026FDE: malloc (vg_replace_malloc.c:207)
==18678==    by 0x8048C17: main (in /media/05788052/school/Apps/out)
==18678== 
==18678== Invalid write of size 4
==18678==    at 0x8048D57: main (in /media/05788052/school/Apps/out)
==18678==  Address 0x41a413c is 0 bytes after a block of size 164 alloc'd
==18678==    at 0x4026FDE: malloc (vg_replace_malloc.c:207)
==18678==    by 0x8048C17: main (in /media/05788052/school/Apps/out)
--18678-- REDIR: 0x40b2de0 (realloc) redirected to 0x4027030 (realloc)
==18678== 
==18678== Process terminating with default action of signal 2 (SIGINT)
==18678==    at 0x40007F0: (within /lib/ld-2.9.so)
==18678==    by 0x40AAE56: _IO_do_write (in /lib/tls/i686/cmov/libc-2.9.so)
==18678==    by 0x40AA82F: _IO_file_overflow (in /lib/tls/i686/cmov/libc-2.9.so)
==18678==    by 0x40A9944: _IO_file_xsputn (in /lib/tls/i686/cmov/libc-2.9.so)
==18678==    by 0x407EF22: vfprintf (in /lib/tls/i686/cmov/libc-2.9.so)
==18678==    by 0x4088B5F: printf (in /lib/tls/i686/cmov/libc-2.9.so)
==18678==    by 0x8048728: fillPosition (in /media/05788052/school/Apps/out)
==18678==    by 0x8048D3D: main (in /media/05788052/school/Apps/out)
--18678-- REDIR: 0x40b0520 (free) redirected to 0x4025d40 (free)
==18678== 
==18678== ERROR SUMMARY: 45434 errors from 6 contexts (suppressed: 11 from 1)
==18678== 
==18678== 1 errors in context 1 of 6:
==18678== Invalid read of size 4
==18678==    at 0x8048814: fillPosition (in /media/05788052/school/Apps/out)
==18678==    by 0x8048D3D: main (in /media/05788052/school/Apps/out)
==18678==  Address 0x41a4064 is 0 bytes after a block of size 164 alloc'd
==18678==    at 0x4026FDE: malloc (vg_replace_malloc.c:207)
==18678==    by 0x8048C17: main (in /media/05788052/school/Apps/out)
==18678== 
==18678== 1 errors in context 2 of 6:
==18678== Invalid write of size 4
==18678==    at 0x8048919: initializeArray (in /media/05788052/school/Apps/out)
==18678==    by 0x8048C8D: main (in /media/05788052/school/Apps/out)
==18678==  Address 0x41a4064 is 0 bytes after a block of size 164 alloc'd
==18678==    at 0x4026FDE: malloc (vg_replace_malloc.c:207)
==18678==    by 0x8048C17: main (in /media/05788052/school/Apps/out)
==18678== 
==18678== 1 errors in context 3 of 6:
==18678== Invalid write of size 4
==18678==    at 0x804893D: initializeArray (in /media/05788052/school/Apps/out)
==18678==    by 0x8048C8D: main (in /media/05788052/school/Apps/out)
==18678==  Address 0x41a3f8c is 0 bytes after a block of size 164 alloc'd
==18678==    at 0x4026FDE: malloc (vg_replace_malloc.c:207)
==18678==    by 0x8048C17: main (in /media/05788052/school/Apps/out)
==18678== 
==18678== 2 errors in context 4 of 6:
==18678== Invalid write of size 4
==18678==    at 0x804895D: initializeArray (in /media/05788052/school/Apps/out)
==18678==    by 0x8048C8D: main (in /media/05788052/school/Apps/out)
==18678==  Address 0x41a413c is 0 bytes after a block of size 164 alloc'd
==18678==    at 0x4026FDE: malloc (vg_replace_malloc.c:207)
==18678==    by 0x8048C17: main (in /media/05788052/school/Apps/out)
==18678== 
==18678== 12 errors in context 5 of 6:
==18678== Invalid write of size 4
==18678==    at 0x8048B3B: initializeArray_1d (in /media/05788052/school/Apps/out)
==18678==    by 0x8048C6D: main (in /media/05788052/school/Apps/out)
==18678==  Address 0x41a3eb8 is 0 bytes after a block of size 16 alloc'd
==18678==    at 0x4026FDE: malloc (vg_replace_malloc.c:207)
==18678==    by 0x8048BF2: main (in /media/05788052/school/Apps/out)
==18678== 
==18678== 45417 errors in context 6 of 6:
==18678== Invalid write of size 4
==18678==    at 0x8048D57: main (in /media/05788052/school/Apps/out)
==18678==  Address 0x41a413c is 0 bytes after a block of size 164 alloc'd
==18678==    at 0x4026FDE: malloc (vg_replace_malloc.c:207)
==18678==    by 0x8048C17: main (in /media/05788052/school/Apps/out)
--18678-- 
--18678-- supp:     11 Debian libc6 (2.9.x) stripped dynamic linker
==18678== 
==18678== IN SUMMARY: 45434 errors from 6 contexts (suppressed: 11 from 1)
==18678== 
==18678== malloc/free: in use at exit: 185,904 bytes in 8 blocks.
==18678== malloc/free: 45,424 allocs, 45,416 frees, 4,132,769,408 bytes allocated.
==18678== 
==18678== searching for pointers to 8 not-freed blocks.
==18678== checked 62,636 bytes.
==18678== 
==18678== LEAK SUMMARY:
==18678==    definitely lost: 0 bytes in 0 blocks.
==18678==      possibly lost: 0 bytes in 0 blocks.
==18678==    still reachable: 185,904 bytes in 8 blocks.
==18678==         suppressed: 0 bytes in 0 blocks.
==18678== Reachable blocks (those to which a pointer was found) are not shown.
==18678== To see them, rerun with: --leak-check=full --show-reachable=yes
--18678--  memcheck: sanity checks: 99 cheap, 5 expensive
--18678--  memcheck: auxmaps: 0 auxmap entries (0k, 0M) in use
--18678--  memcheck: auxmaps_L1: 0 searches, 0 cmps, ratio 0:10
--18678--  memcheck: auxmaps_L2: 0 searches, 0 nodes
--18678--  memcheck: SMs: n_issued      = 25633 (410128k, 400M)
--18678--  memcheck: SMs: n_deissued    = 25552 (408832k, 399M)
--18678--  memcheck: SMs: max_noaccess  = 65535 (1048560k, 1023M)
--18678--  memcheck: SMs: max_undefined = 0 (0k, 0M)
--18678--  memcheck: SMs: max_defined   = 23 (368k, 0M)
--18678--  memcheck: SMs: max_non_DSM   = 200 (3200k, 3M)
--18678--  memcheck: max sec V bit nodes:    0 (0k, 0M)
--18678--  memcheck: set_sec_vbits8 calls: 0 (new: 0, updates: 0)
--18678--  memcheck: max shadow mem size:   3504k, 3M
--18678--  ocacheL1: 1,117,242,543 refs        447,675 misses (0 lossage)
--18678--  ocacheL1: 1,116,188,375 at 0        606,493 at 1
--18678--  ocacheL1:            0 at 2+       452,414 move-fwds
--18678--  ocacheL1:   92,274,688 sizeB    67,108,864 useful
--18678--  ocacheL2:      447,675 refs        447,675 misses
--18678--  ocacheL2:            0 max nodes         0 curr nodes
--18678--  niacache:            0 refs              0 misses
--18678-- translate:            fast SP updates identified: 2,118 ( 89.2%)
--18678-- translate:   generic_known SP updates identified: 138 (  5.8%)
--18678-- translate: generic_unknown SP updates identified: 118 (  4.9%)
--18678--     tt/tc: 4,436 tt lookups requiring 4,495 probes
--18678--     tt/tc: 4,436 fast-cache updates, 2 flushes
--18678--  transtab: new        2,211 (48,653 -> 1,063,661; ratio 218:10) [0 scs]
--18678--  transtab: dumped     0 (0 -> ??)
--18678--  transtab: discarded  0 (0 -> ??)
--18678-- scheduler: 9,923,513 jumps (bb entries).
--18678-- scheduler: 99/97,793 major/minor sched events.
--18678--    sanity: 100 cheap, 5 expensive checks.
--18678--    exectx: 1,543 lists, 1,258 contexts (avg 0 per list)
--18678--    exectx: 140,003 searches, 139,502 full compares (996 per 1000)
--18678--    exectx: 17 cmp2, 45,469 cmp4, 0 cmpAll
--18678--  errormgr: 18 supplist searches, 1,998 comparisons during search
--18678--  errormgr: 45,445 errlist searches, 45,505 comparisons during search

  • 0

#11 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 14 March 2010 - 01:03 PM

Ok, this means that your issue is in initializeArray() and you're writing four bytes past the end of your array. My guess is that this line:
for(j = 0; j <= k; j++)
Should be:
for(j = 0; [COLOR=Red]j < k[/COLOR]; j++)

Try that.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#12 fread

fread

    Programming God

  • Senior Member
  • PipPipPipPipPipPip
  • 897 posts
  • Location:Trinidad and Tobago
  • Learning:C, Java, C++, C#, PHP, Python, PL/SQL

Posted 15 March 2010 - 03:47 PM

Im kind of far from my pc right now...will try as soon as i get home...
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download