Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9266651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:22:08+00:00 2026-06-18T14:22:08+00:00

Im having some trouble understanding how the pass by value mechanism works in c

  • 0

Im having some trouble understanding how the pass by value mechanism works in c with pointers. Here is my contrived example…


In my main function, I malloc a pointer to an array of int:

int ** checkMe;
checkMe = malloc(sizeof(int *) * 10);

I understand that this operation sets a side a block of 10 chunks of memory, each block big enough to hold the pointer to an int pointer. I receive back the pointer at the start of this block of 10 chunks.

I have another function that takes that double pointer as an argument:

void test2dArray(int ** arr, int size) {
    int i, j;

    for (i = 0; i < size; i++) {
        // arr[i] = malloc(sizeof(int) * size);
        for (j = 0; j < size; j++) {
            arr[i][j] = i * j;
        }
    }
}

Whenever I leave the commented section as is, and try to malloc the space for the int in main like this:

int ** checkMe;
checkMe = malloc(sizeof(int *) * 10);

for (i = 0; i < 10; i++) {
    checkMe[i] = malloc(sizeof(int));
}

test2dArray(checkMe, 10);

I get memory clobbering whenever I iterate checkMe after the test2dArray call in main.

But if I malloc the space for the int in test2dArray instead (by uncommenting the commented line above) and change my call from main to this:

int ** checkMe;
checkMe = malloc(sizeof(int *) * 10);

test2dArray(checkMe, 10);

the memory clobbering goes away and I can reference checkMe just fine after the function call.


I understand that checkMe is being passed into test2dArray by value. I think this means that the address that is returned by checkMe = malloc(sizeof(int *) * 10); is copied into the function.

I don’t understand why the int *‘s that checkMe stores gets lost if I don’t malloc the space from within test2dArray

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:22:09+00:00Added an answer on June 18, 2026 at 2:22 pm
    for (i = 0; i < 10; i++) {
        checkMe[i] = malloc(sizeof(int));
    }
    

    You are only allocating memory for 1 int in each loop iteration. So you have an array of 10 pointers, each pointing to sizeof(int) bytes of memory.

    test2dArray(checkMe, 10);
    

    only works for arrays of 10 pointers pointing to at least 10*sizeof(int) memory. You should change the line above to checkMe[i] = malloc(sizeof(int)*10);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble understanding the IF clause at the end of this function
Having some trouble understanding the right approach here. I have a connection to a
I'm having some trouble understanding how codeigniters loading works. Well first you have the
I'm having some trouble understanding import This piece of code works properly. I from
I'm having some trouble understanding this. Here's what's happening. I'm spawning a new thread
I'm having some trouble understanding the search dialog in the Android SDK. The main
I'm having some trouble understanding how ko.mapping.fromJS and ko.mapping.toJS work. Here the explanation of
I'm a programming newbie and am having some trouble understanding an example from my
I am having some trouble understanding the following simple C code: int main(int argc,
I'm having a little trouble understanding the pass-by-reference properties of data.table . Some operations

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.