1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

C address in memory

Discussion in 'Software Development' started by L3v3L, Jan 23, 2011.

Thread Status:
Not open for further replies.
  1. L3v3L

    L3v3L Thread Starter

    Feb 25, 2010
    int sqr() {
      int *square = NULL;
      square = malloc(sizeof(int)*4);
      printf("%d \n",&square);
    int main() {
      int i;
      for (i=0;i<100;i++){
    The address that is outputed is always the same.
    Can some one tell me why the address is always the same, and how i could fix this?
  2. mt2002


    Sep 14, 2005

    Your code will display the address of the pointer, square, itself, which is on the program stack. Thus the only way to "change" this is by either padding (create more local variables) or changing the stack base (can be done in Linker settings, not recommended for general use.)

    Do you really need the pointer itself to not always reside at the same location? Did you mean to do printf ("0x%x", square) instead (displays address of where square points to)?

    I can go into more detail of the "why" using assembly if requested.
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/976498

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice