0

As I ask how can I read values from the memory which was put into a memory block by malloc/realloc?

I have a program which creates 2 memory blocks at first and extends until reach 32.Its working fine but in order not to lose values after realloc I start my scanf session with an integer called koru.Koru starts with 0 at the beginning and in every realloc I make its number half of the total amount of memory so I expect not to lose the values entered before realloc sessions but now I need to read what I wrote the memory so I will be sure that I didnt overwrite how can I do that ?

    #include "stdio.h"
    #include "stdlib.h"
    int main()
    {
      int size=2;
      int* p1;
      int* p2;
      int n;
      p1=(int *)malloc((size*sizeof(int)));
      int b=0;
      int i=0;
      int koru=0;   
      while(size<32) 
 { 
    if(i<size){           
    for(i=koru;i<size;i++)
 {      
    printf("\nEnter value:");
    scanf("%d",p1);
     }
     }
    else if(boyut=i)         
     {  
        size=size*2;    
        p2 = (int*) realloc (p1,size*sizeof(int));
        p1=p2;  
        koru=size/2;
        printf("\nNOt enough size we make it %d also we protect %d that amount of memory"size,koru);
     }
    }    
    return 0;
    }

I thought putting a array[i]=p1; and reading it after while loop will solve the problem but later I figured out that its cheating I am not reading from the memory block just reading from another array so this wont work too can somebody lead the way for me please ?

What my program does:

2 blocks memory malloc takes values and fills it When it fills improves the memory size multiplying by 2 realloc starts reading from half number so it dont rewite continues this progress until it reaches 32

Edit:Simplified question:

I put lots of values in the memory after while loop how can I read them ?

I tried to read it like this but all I get is 0

int counter =0;
while (counter<koru)
{
printf("\n%d",p1[counter]);
counter++;
}
  return 0;
}

The correct code which creates a memory block with malloc and extends it with realloc after 32 it exits while loop and writes everything from the p1 pointer(from memory) thanks for help.

Working Code below:

#include "stdio.h"
#include "stdlib.h"
int main()
{
  int boyut=2;
  int* p1;
  int* p2;
  int n;
  p1=(int *)malloc((boyut*sizeof(int)));

  int b=0;
  int i=0;
  int koru=0;

while(boyut<16)
    {
    if(i<boyut){          
        for(i=koru;i<boyut;i++){    
          printf("\nDeğer Girin:");
        scanf("%d", &p1[i]);
     }
}
    else if(boyut=i)        
    {   
     boyut=boyut*2;    
     p2 = (int*) realloc (p1,boyut*sizeof(int));
     p1=p2;   
     koru=boyut/2;
     printf("\nBoyut yetersiz yeni boyutu %d yapıyoruz önceki %d kadar alanı korumaya aldık",boyut,koru);
 }
}
int counter =0;
while (counter<koru)
{
printf("\n%d",p1[counter]);
counter++;
}
  return 0;
} 
3
  • Please format the code, this looks bad! Commented Jan 4, 2014 at 17:33
  • I will convert my values to english hold on a second. Commented Jan 4, 2014 at 17:38
  • please check the new code Commented Jan 4, 2014 at 17:48

2 Answers 2

1

I'm not sure exactly what you are asking here, but to access the values in the memory, just use p1 as an array. p1[3] would give you the 4th integer in the memory block.

Just be sure to respect the array bounds.

Sign up to request clarification or add additional context in comments.

4 Comments

printf(%d,p1[3]); outputs 0 zero is it working or Am I doing a mistake ?
if I can read from p1[0-32] my problem will be solved but its giving me an error when I compile please help
This is the problem: scanf("%d",p1); You need to scanf into a pointer to an integer, then assign it to a slot in p1. Alternatively, scanf("%d", &p1[i]);
scanf("%d", &p1[i]); thats what I want now my testing code works too thank you soo much.
1

Bonus problem: you're not even filling the array, you're overwriting the first element each time you input.

The simple solution is to remove p2 entirely and treat p1 as an array:

...
    scanf("%d", &p1[i]);
...

Then to read:

int n;
for (n=0; n<i; n++)
    printf("%d ", p1[n]);

However, if you want to do it with two pointers instead of array indexing, you need one pointer to track the whole array for malloc and realloc, and the other to track where the current value is for reading/writing.

Initially the array is empty, so they are the same:

p1 = (int *)malloc(boyut * sizeof(int));
p2 = p1;
...

Input only uses the read/write pointer, and increments it each time:

...
    scanf("%d", p2++);
...

Memory management only uses the "base" pointer

...
    p1 = (int *)realloc(p1, boyut * sizeof(int));
...

(note you should strictly use a temporary variable for the return value of realloc() to check for failure, otherwise you could lose the value of p1)

Then to read we reset the read/write pointer to the start, and iterate through:

int n;
p2 = p1;
for (n=0; n<i; n++)
    printf("%d ", *p2++);

3 Comments

What but my testing code below gives me the all numbers I input (after putting scanf("%d", &p1[i]);) so how can I "overwriting the first element each time you input." ? Its working now :D
@BugraSezer the original code: scanf("%d", p1) always overwrites the value at p1, which always points to the start of the array. It's hardly surprising that it works after changing that line to something that does the right thing ;) In my defence, when I started typing this the edit and other comments weren't there. I'm slow.
of course of course it overwrites I agree with you the correct one is above but thanks anyway I give you point too...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.