Thread: why the memory is not getting alocated to this pls help

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    2

    Post why the memory is not getting alocated to this pls help

    Code:
    #include<stdio.h>
    void allocate(int *,int);
    int main(){
    int* p =NULL;
    allocate(p,100);
    printf("%d",*p);
    }
    void allocate(int* p ,int x){
    p = (int*)malloc(sizeof(int));
    *p = x;
    return 0;
    }
    compiled but when running its reporting crash dump
    Last edited by naren26051991; 08-04-2012 at 09:00 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Consider this program:
    Code:
    #include <stdio.h>
    
    void assign(int x);
    
    int main(void) {
        int x = 0;
        assign(x);
        printf("%d\n", x);
        return 0;
    }
    
    void assign(int x) {
        x = 1;
    }
    Why is the output 0 even though I assigned 1 to x in the assign function?

    Once you understand why, it should become clear to you that in your allocate function, this:
    Code:
    p = (int*)malloc(sizeof(int));
    does not change the value of p in the main function. Hence, you should either be passing a pointer to a pointer, or returning the pointer.

    By the way, since you declared allocate as having void return type, you should not be returning 0. Also, please indent your code properly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    This program as such compiles and runs fine.
    BUT i guess you try to use "p" after that line "allocate(p,100)" and of course dereferencing NULL fails because your function doesn't actually change the pointer value.
    What you want is:
    Code:
    int main(void) {
      ...
      allocate(&p, 100);
      ...
    }
    void allocate(int **p, int x) {...}
    At last don't forget to include stdlib.h for malloc.

    PS: I wrote the reply at the same time than laserlight so sorry for the redundant info.
    Last edited by root4; 08-04-2012 at 09:06 AM.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    The variable 'p' in main is different from 'p' in allocate. Any changes to 'p' in allocate are not seen by main.

    There are a few other problems I saw in your code as well:

    Your main function doesn't have a return statement.

    allocate does have a return value but it shouldn't.

    You should not cast the return value of malloc, and you should include <stdlib.h> for malloc's prototype.

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    2
    tell me how actually this thing works

    actually i am passing the pointer type variable to allocate(p,100)

    i am allocating memory through malloc function and

    when i am testing it through printf in the allocate function its working but when i am testing the memory allocated through printf in main function its compiling but again crash dump reported pls explain this concept clearly

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Both main and allocate have variables named 'p'. They are two different variables. Changing p in allocate does not affect p in main. Function calls in C are pass by value, which means the value of main's p (NULL) is passed in to allocate as its own copy, which is also named p. Changing p in allocate changes only allocate's copy of p.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay, I will say it plainly then: your code fails because you assign the result of malloc to a local variable. As such, the pointer named p in the main function is not changed, i.e., it is still a null pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Passing a pointer to an int would be fine if you just wanted to modify the value of an int variable in main. However you want to modify the value of a pointer to an int, so you have to pass a pointer to a pointer to an int, in order to modify it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2012, 04:28 AM
  2. Dynamic memory and realloc(), freeing memory
    By C_Sparky in forum C Programming
    Replies: 6
    Last Post: 10-06-2010, 07:55 PM
  3. Allocate memory inside allocated memory block?
    By Heidi_Nayak in forum C Programming
    Replies: 14
    Last Post: 04-15-2009, 04:19 PM
  4. Finding memory address using memory pattern
    By MindWorX in forum C++ Programming
    Replies: 1
    Last Post: 05-25-2008, 07:20 AM
  5. Replies: 2
    Last Post: 09-28-2006, 01:06 PM