9 Replies - 461 Views - Last Post: 13 April 2012 - 02:35 PM Rate Topic: -----

#1 WaffleByte  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 12-April 12

Memory sizes in stack

Posted 12 April 2012 - 09:34 PM

#include <stdio.h>

int main()
{
    int a = 5;
    int b = 10;
    int c = 15;
    printf("sizeof(int) = %i\n",sizeof(int));
    printf("%i %i %i\n",a,*(&a+1),*(&a+2));
}


Output:
sizeof(int) = 4
5 10 15

If an int is 4 bytes then why does incrementing memory by 1 instead of 4 display the next int value?
Is This A Good Question/Topic? 0
  • +

Replies To: Memory sizes in stack

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1418
  • View blog
  • Posts: 2,681
  • Joined: 30-May 10

Re: Memory sizes in stack

Posted 12 April 2012 - 09:45 PM

Because that's how pointers work in C.

If you have a pointer to type T, then incrementing the pointer makes it point to the next T, not the next byte.
Was This Post Helpful? 0
  • +
  • -

#3 WaffleByte  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 12-April 12

Re: Memory sizes in stack

Posted 12 April 2012 - 10:00 PM

View PostSalem_c, on 12 April 2012 - 09:45 PM, said:

Because that's how pointers work in C.

If you have a pointer to type T, then incrementing the pointer makes it point to the next T, not the next byte.

Then how can I directly control what byte I want if C automatically rounds it to the nearest typesize.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,555
  • Joined: 21-June 11

Re: Memory sizes in stack

Posted 13 April 2012 - 02:43 AM

View PostWaffleByte, on 13 April 2012 - 07:00 AM, said:

Then how can I directly control what byte I want if C automatically rounds it to the nearest typesize.


By casting to a a char pointer, a char always being a single byte. If you want to access a single byte, you'd have to cast char* (or unsigned char*) anyway because dereferencing an int* would give you sizeof(int) bytes, not a single byte.
Was This Post Helpful? 0
  • +
  • -

#5 turboscrew  Icon User is offline

  • D.I.C Regular

Reputation: 69
  • View blog
  • Posts: 460
  • Joined: 03-April 12

Re: Memory sizes in stack

Posted 13 April 2012 - 05:56 AM

Also, taking an address of a automatic variable is risky, and in some C implementations illegal. Think, if you take a pointer to a stack-variable and return that variable to the caller. The address stays even if the variable was deleted. If the pointer is then forwarded to another function, who knows what it then points to.

I guess most popular compilers notice if "address-of" is used, and allocates the variable to data memory instead of the stack (just like statics), or calculate the address from stack pointer and the offset.

Some compilers use the offset from the stack pointer as the "address" of automatic variable in which case the address doesn't make sense. I guess taking the address of a variable defined as "register" is (still) illegal regardless of the C compiler.
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,555
  • Joined: 21-June 11

Re: Memory sizes in stack

Posted 13 April 2012 - 06:26 AM

View Postturboscrew, on 13 April 2012 - 02:56 PM, said:

Also, taking an address of a automatic variable is risky, and in some C implementations illegal.


Any implementation in which it is not allowed to take the address of an automatic variable is broken and does not conform to the C standard.

What is illegal (that is, undefined behavior) is to perform pointer arithmetic that goes outside the bounds of an object. I.e. it is undefined to do `*(&a+1)` in the OP's code because you're accessing memory that is not part of a anymore.

Quote

Think, if you take a pointer to a stack-variable and return that variable to the caller.


Then dereferencing that pointer will invoke undefined behavior. That's the same thing as if you allocate memory using malloc, then call free on it and use the pointer afterwards. That doesn't mean using pointers to malloc-allocated memory is illegal in general.

Quote

I guess most popular compilers notice if "address-of" is used, and allocates the variable to data memory instead of the stack (just like statics), or calculate the address from stack pointer and the offset.


I don't think any C compiler does that.

This post has been edited by sepp2k: 13 April 2012 - 06:29 AM

Was This Post Helpful? 0
  • +
  • -

#7 turboscrew  Icon User is offline

  • D.I.C Regular

Reputation: 69
  • View blog
  • Posts: 460
  • Joined: 03-April 12

Re: Memory sizes in stack

Posted 13 April 2012 - 10:31 AM

At least:

Quote

A register declaration is an auto declaration, with a hint to the compiler that the objects declared will be heavily used. Whether the object is actually placed in fast storage is implementation defined. You cannot take the address of any part of an object declared with the register specifier.
:D

(From here.)

Also, there are quite many ANSI C standards and even old K&R-"standard". All C-compilers are even incompatible with any standard (typically microcontroller/DSP specific).

Anyway: taking an address of a automatic variable is STILL risky and not surely a good programming.

This post has been edited by turboscrew: 13 April 2012 - 10:34 AM

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon

Reputation: 3060
  • View blog
  • Posts: 9,309
  • Joined: 25-December 09

Re: Memory sizes in stack

Posted 13 April 2012 - 10:46 AM

Quote

taking an address of a automatic variable is STILL risky and not surely a good programming.

Why? If you can't "take" an address of a variable how would you use functions like scanf() which require the address of a variable?

Jim
Was This Post Helpful? 1
  • +
  • -

#9 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,555
  • Joined: 21-June 11

Re: Memory sizes in stack

Posted 13 April 2012 - 01:07 PM

View Postturboscrew, on 13 April 2012 - 07:31 PM, said:

Also, there are quite many ANSI C standards and even old K&R-"standard". All C-compilers are even incompatible with any standard (typically microcontroller/DSP specific).


Yes, and all of them allow you to take the address of automatic variables.

Quote

Anyway: taking an address of a automatic variable is STILL risky and not surely a good programming.


As I already pointed out, accessing malloced memory after it has been freed will lead to the same problems as accessing a pointer to a local variable that has gone out of scope. So I don't see why one would be more risky than the other. And as jimblumberg already pointed out, scanf (among other others) would be pretty useless if taking the address of local variables were to be avoided.
Was This Post Helpful? 0
  • +
  • -

#10 turboscrew  Icon User is offline

  • D.I.C Regular

Reputation: 69
  • View blog
  • Posts: 460
  • Joined: 03-April 12

Re: Memory sizes in stack

Posted 13 April 2012 - 02:35 PM

View Postjimblumberg, on 13 April 2012 - 10:46 AM, said:

Quote

taking an address of a automatic variable is STILL risky and not surely a good programming.

Why? If you can't "take" an address of a variable how would you use functions like scanf() which require the address of a variable?

Jim


Sorry, it seems that I've not been in my right mind today - I don't know what I was thinking. :crazy:
I've written some over 25000 line embedded programs in C and suddenly...
As if I didn't know better...

I've better have some serious sleep tonight...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1