I am a new guy in C. But I have read stuffs about Malloc, Realloc, how to pass pointers to the function...
If I am writing this code in single program, it works perfectly...but smart programmmer guide says write in compact form.
So with user defined function this is not giving me desired output. I dont know what mistake I am going in this simple program.
*pointer takes all the values correctly in function resize_long_pointer(...), but when it comes to main function it is not working..even it is crashing with large number for N_Cell_total....
Please Help me regarding this problem
Hear is my programe....
Code:
#include <stdafx.h>
#include <stdio.h>
#include <malloc.h>
#include <math.h>
#include <stdlib.h>
void main()
{
size_t size;
long *CF5,i;
long N_Cell_total;
void resize_long_pointer(long *,long *);
N_Cell_total = 1;
CF5 = (long *)malloc((N_Cell_total + 2) * sizeof(long));
for(i=1;i<=N_Cell_total;i++)
{
CF5[i] = i;
printf("\ntest1 %ld",CF5[i]);
}
N_Cell_total = 20000;
resize_long_pointer(CF5,&N_Cell_total);
for(i=1;i<=N_Cell_total;i++)
{
printf("\ntest2 i %ld CF5 %ld",i,CF5[i]);
}
}
void resize_long_pointer(long *pointer,long *add_size)
{
long int_tmp,i;
size_t size;
int_tmp = *add_size;
printf("\nAdditional size :: %ld",int_tmp);
if( (pointer = (long *) realloc( pointer, (int_tmp + 2) * sizeof(long) )) == NULL)
printf("\nPointer resizing problem :: add_size : %ld",*add_size);
for(i=1;i<= int_tmp;i++)
{
*pointer = i;
}
printf("\ntest2 pointer[%ld] %ld ",i-1,pointer[i-1]);
}
output::
Code:
test1 1
Additional size :: 100
function test1 i 1 pointer 1
function test1 i 2 pointer 2
.......
function test1 i 3 pointer 100
test2 pointer[100] 100
test2 i 1 CF5 1
test2 i 2 CF5 -842150451
test2 i 3 CF5 -33686019
.......
test2 i 100 CF5 43807