-3

In the code below, when writing to the memory why does this line of code:

temp = ((int*) mem_allocate + i); 

not increment the memory locations in consecutive 4 bytes? The memory locations resulted due to this are:

0x20000818 
0x20000828
0x20000848 
...

and so on.

I wanted to write the data in

0x20000818
0x2000081C
0x20000820 
...

and so on.

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

int main()
{
   int n = 1024; 
   int* mem_allocate;
   int loop = 0;
   int i = 0; 

   mem_allocate = (int*) malloc(n*sizeof(int));    

   for(i=0; i<40; i++) 
   {
      int* temp; 
      temp = ((int*) mem_allocate + i);
      i=i+3; 
      *temp =0xAAAAAAAAu; 
      *mem_allocate = *temp; 
       mem_allocate = temp;

      if (i == 40)
      {
          loop = 1; 
      }
   }

   if (loop == 1) 
   {
      free(mem_allocate); 
   }
   return 0; 
}
too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Wheel60
  • 19
  • 1
  • 11
  • 3
    `0x` prefix is for hexadecimal. I would guess you want it in binary, which is `0xAAAAAAAA`. – Eugene Sh. Jul 20 '18 at 13:02
  • 1
    `*temp = (int*)memory;` here are you trying to get value of memory to *temp? If so you should try `*temp =*(int*)memory; ` – Keshava GN Jul 20 '18 at 13:04
  • 1
    .. or `*temp = memory;` – chux - Reinstate Monica Jul 20 '18 at 13:04
  • 1
    `*temp = (int*)memory;` -- why are you casting `memory` to a pointer? – Ajay Brahmakshatriya Jul 20 '18 at 13:05
  • You need to recheck this part of the code or let us know what you need to achieve here: `int* temp; temp = ((int*) mem_allocate + i); *temp = (int*)memory; *mem_allocate = temp; ` – Keshava GN Jul 20 '18 at 13:07
  • 0x is a hex prefix. your line expands out to 0001000000010000 etc. You want the binary prefix, '0b10101010'. Or like @EugeneSh. said, 0xAAAAAAAA would work too. – K_Trenholm Jul 20 '18 at 13:07
  • 2
    There are many issues. What are you _actually_ trying to achieve? – Jabberwocky Jul 20 '18 at 13:08
  • @Wheel60 Curious, who or what text suggested casting the return value of `malloc()`, as in `mem_allocate = (int*) malloc(n*sizeof(int));` vs a more simple `mem_allocate = malloc(n*sizeof(int));` or even better `mem_allocate = malloc(sizeof *mem_allocate * n);`? – chux - Reinstate Monica Jul 20 '18 at 13:09
  • Don't cast the result of `malloc` & friends. – too honest for this site Jul 20 '18 at 13:10
  • 1
    `mem_allocate` already *is* a pointer to int, thus `mem_allocate + i` is as well, any you don't need to cast again! You should stop casting everything to anything, you are just hiding errors this way or even producing them... – Aconcagua Jul 20 '18 at 13:10
  • 1
    @K_Trenholm Note, the `0b` prefix is not standard and might not be supported by the specific compiler. – Eugene Sh. Jul 20 '18 at 13:12
  • [Casting result of malloc](https://stackoverflow.com/a/14879184/1312382): There are different oppinions to, I rather recommend having a look at both sides' arguments and make up a mind of your own... – Aconcagua Jul 20 '18 at 13:13
  • Note: to generate an alternating bit pattern for an `int`, try `int memory = (-1u)/3;` – chux - Reinstate Monica Jul 20 '18 at 13:15
  • I updated the question. and made the changes, thank you all of you. – Wheel60 Jul 20 '18 at 14:34
  • @chux can you look at the updated question? – Wheel60 Jul 20 '18 at 15:09
  • Why is "`ADD.W R2,R0,R1, LSL #2`" relevant to the question or the behaviour. I suspect you are over complicating the issue by trying to answer your own question and asking about the answer your arrived at rather than just asking the much simpler and rather more obvious question. Either way, if you are going to post assembler, use code mark-up. – Clifford Jul 20 '18 at 15:29
  • The question in the title differs from the question in the body text, and is irellevant to your problem in any case. You gave conflated at least three questions. Surely your question is simply why the addresses increment by 16 rather then 4? – Clifford Jul 20 '18 at 15:31
  • For what it is worth `ADD.W R2,R0,R1,LSL #2` means `R2 = R0 + R1 x 4`. Shift left 2 effects a multiply by 4. That is the `sizeof(int)` and no doubt R0 relates to `i` and is incremented by 4, so the total increment is 16. – Clifford Jul 20 '18 at 15:38
  • This question could easily be fixed - I suggest you do so rather than let it get closed or down voted. – Clifford Jul 20 '18 at 15:41
  • Never mind; I fixed it for you. – Clifford Jul 20 '18 at 15:48
  • @Wheel60 I ask [question](https://stackoverflow.com/questions/51443140/why-is-this-pointer-incrementing-by-0x10-not-0x04?noredirect=1#comment89856385_51443140) - no response to that. You ask for [review](https://stackoverflow.com/questions/51443140/why-is-this-pointer-incrementing-by-0x10-not-0x04?noredirect=1#comment89861248_51443140) - I'll follow your lead. – chux - Reinstate Monica Jul 20 '18 at 16:19
  • @chux i'm sorry. I didn't look at the comments. and I was using stackoverflow after a while, so i didn't notice the question change. I thought the body itself is the question. My mistake. – Wheel60 Jul 20 '18 at 16:52

1 Answers1

1

Your loop control variable i is incremented by one in the for loop:

for(i=0; i<40; i++) 

and then by a further 3 by:

i=i+3;

So i is overall incremented by 4 in each iteration. Pointer arithmetic accounts for the size of the object pointed to. Here you are pointing to a 32 bit (4 byte) integer, and incrementing by 4 each time, so the address is incremented by 4 x 4 = 16 bytes.

You should have:

   for( i = 0; i < 10; i++ ) 
   {
      int* temp = mem_allocate + i ;
      // i=i+3; REMOVE THIS!
      ...

Note that the cast was unnecessary; mem_allocate was already an int* as is the type of the expression mem_allocate + i.

Your code is flawed in other ways, unrelated to the question you have asked - specifically the ill-advised modification of mem_allocate - if you modify it, any attempt to free it will be invalid.

Consider:

#include <stdint.h>
#include <stdlib.h>

int main()
{
   const int n = 1024; 
   uint32_t* mem_allocate = malloc( n * sizeof(*mem_allocate) ) ;

   for( int i = 0; i < n; i++ ) 
   {
      mem_allocate[i] = 0xAAAAAAAAu; 
   }


   free( mem_allocate ) ; 

   return 0 ; 
}

As you can see you made a simple thing unnecessarily complicated.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • I think i should replace the "i" in "int* temp = mem_allocate + i ;" with 1. and Yeah the cast is unnecessary (i changed it), thanks. and what is this instruction mean? "ADD.W R2,R0,R1, LSL #2" – Wheel60 Jul 20 '18 at 15:33
  • @Wheel60 : Your should not change `i` to `1` - instead you should not modify `mem_allocate`!. I answered the question about the assember instruction in comments - it seemed entirely irrelevant to the question (and I removed it from the question in any case). Keep posts to a single question and ensure the title and the question address the same issue - I fixed that too. – Clifford Jul 20 '18 at 16:10