6

I know this is probably a basic question, but i've never fully grasped the whole pointers concept in C.

My question is, say i have an int array and I pass that into a function like `

int main(){
    int *a = malloc(sizeof(int*)*4);

    // add values to a
    p(a);
}

void p(int *a){
   int *b = malloc(sizeof(int*)*4)
   b = a;
   // change values in b

   //print a b
}`

What is the correct way to do this so that whatever changes I make to b do not affect the values in a?

1
  • 2
    Tell me about it, it took 4.2 hours of asking random suits outside of CSC just to get the URL for this place! I guess there's always Bing, but, no... Commented Apr 10, 2014 at 0:29

5 Answers 5

7

In your 'p' method, you're assigning pointer b to be pointer a, or, in otherwords, you're making b point to what a points to. Any changes to b will cause changes to a since they both wind up pointing to the same block of memory.

Use memcpy to copy blocks of memory. It would look something like this:

#include <string.h>
#include <stdlib.h>
void p(int *a){
   int *b = (int*)malloc(sizeof(int)*4);
   memcpy(b, a, sizeof(int)*4);

    //make changes to b.
   b[0] = 6;
   b[1] = 7;
   b[2] = 8;
   b[3] = 9;
}

int main(int argc, char **argv)
{
    int *a = (int*)malloc(sizeof(int)*4);

    // add values to a
    a[0] = 1;
    a[1] = 2;
    a[2] = 3;
    a[3] = 4;

    p(a);

return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Just assigning the pointer means b is pointing to the same chunk of memory as a and the memory you just allocated "for b" has leaked. It's allocated but you can't free it any more.

To copy the array you need to well, copy it.

Easy way is lookup the various memcpy methods, or just do it the longer way

for (int i = 0; i < 4; i++) {
    b[i] = a[i];
}

You need to know about "shallow copy" and "deep copy" - since you have an array of int*, what I put above is a shallow copy. If you need to copy the contents that each int* points to, you'll need something more complex again.

1 Comment

Great point on leakage! I think it's pointless to point a pointer to an array pointer. :D
0

You are pointing a and b to two different blocks of memory and then assigning b to the block pointed to by a, causing a memory leak.

And since you are not returning anything from your function p(), you can allocate b on stack (I wonder what you are doing with it).

If your intention is to copy the data pointed to by these pointers, you can use memcpy or copy element by element as others have suggested.

Comments

0
void p(int *a){
   int *b = malloc(sizeof(int*)*4)
   int size=4;
   while(size>=0){
     b[size--]=a[size--];
   }
   // change values in b

   //print a b
}

This should work!

4 Comments

@John3136 It is the address that can be NULL not the value itself
You are right, I misinterpreted... But that makes it even worse: how will a ever be NULL when all you do is increment it! Infinite loop! I see you've edited so my first comment is true again.
@John3136 there is limited memory allocated to a. So if a is incremented, it will get pass the allocated memory eventually
@Manual Correct. That doesn't mean the value becomes 0, it means you are doing an "uninitialized memory read" (aka UMR). If you are lucky you'll read a 0 and stop the loop before you try to read something you are not allowed to and crash. Most likely scenario is either a crash or an infinite loop. Your basic assumption that there is a 0 in the last place of the array is just a convention for char[] when used as strings. It is not a enforced for any array types.
0

C does not support array assignment! You must copy data manually.

  • Use memcpy
  • Use memmove if a and b (could) overlap
  • Use for or while loop

Comments

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.