2

I have following two questions

  1. Given the statement like char * str = "999999999999"; how does compiler determine how much space to allocate on stack?

  2. How can i iterate over the memory pointed by str and determine the value of various bits inside it?

Jimm
  • 8,165
  • 16
  • 69
  • 118
  • 1
    1) sizeof(char*) on stack. – BLUEPIXY Apr 10 '13 at 15:56
  • about 1. : At compile time, the compiler will be able to know what "char" represent on your machine and locale, and so how many bytes to use to store "999999999999" (+ the terminating NULL character, which also varies amongst machines/implementations and is therefore machine dependant). – Olivier Dulac Apr 10 '13 at 15:58
  • @OlivierDulac it is char * and not char. char * should be able to point to any arbitrary sized memory. I am interested in understanding how compiler determines the pointed to size and not size of pointer itself – Jimm Apr 10 '13 at 15:59
  • @Jimm: like i said, it knows the size of 1 char, and how many there are in the "999999999999" static string you give it, and the size of the terminating null. ergo it knows at compile time the size of it. (forgive me if i misunderstood your comment?). but cmd has it right: the string itself is not on the stack ^^. sorry. – Olivier Dulac Apr 10 '13 at 16:19

3 Answers3

4
  1. "999999999999" is not on the stack. The pointer may be on the stack, the compiler knows how big the pointer should be. The "999999999999" really has a null termination that lets you know when it is at the end.

  2. To iterate over each bit, maybe something like:

    for(i=0; str[i]; i++)  // str[i] will evaluate to false(0) when that character is the null
        for(j=7; j>=0; j--)
            printf("%d", (str[i] >> j) & 0x01)
    

    but I prefer looking at bits in hex representation instead of binary so I would just

    for(i=0; str[i]; i++)
        print("%X ", str[i])
    
cmd
  • 5,754
  • 16
  • 30
  • i would like bit pattern of each byte allocated on heap pointed by str – Jimm Apr 10 '13 at 16:03
  • 3
    `"99.."` isn't allocated on the heap. It is a string literal and is likely to exist in a data section. [This previous question](http://stackoverflow.com/q/2589949/311966) offers more info. – simonc Apr 10 '13 at 16:12
  • @Jimm changed for bit pattern – cmd Apr 10 '13 at 16:15
  • `sizeof` operator does not return size of operand measured in bits. – Nemanja Boric Apr 10 '13 at 16:16
  • lol, you guys really like catching stuff in the few seconds they are in – cmd Apr 10 '13 at 16:19
  • +1 for correcting the "not on the stack", and for the neat and simple loops to display the bit representation – Olivier Dulac Apr 10 '13 at 16:23
  • but maybe wrong: outer loop uses "i++" and then inner loop loops on "sizeof(char)" : this assumes that char is always 1 byte?... – Olivier Dulac Apr 10 '13 at 16:26
  • @OlivierDulac not sure, havent done any international work in C. but dont you need to use wchar to get each char larger then a byte? – cmd Apr 10 '13 at 16:34
  • @cmd I know that generally you can't do *any* assumption on *any* (char, int, etc. and also NULL) internal representations... got this from the C faq (the abridged version is a MUST READ : http://www.faqs.org/faqs/C-faq/abridged/ ). But I see that you changed the loops, and it should now be more portable (if it works at all ^^) – Olivier Dulac Apr 10 '13 at 16:39
  • the other versions of the FAQ are **[there](http://c-faq.com/versions.html)** (the unabridged is also a good read, but, of course, longer ^^) – Olivier Dulac Apr 10 '13 at 16:47
1

1) In C, strings are terminated by byte which is set to zero. So, in your example, 13 bytes will be allocated for this string - 12 bytes for 12 ascii characters and one byte for '\0' character (NULL terminator).

2) You could just iterate over it and include some bitwise arithmetics:

for(i = 0; i < strlen(str); i++)
{
    for(j = 7; j >= 0; j--)
     printf("%d", (str[i] & (1 << j)) == 0 ? 0 : 1);
    printf(" ");
}
Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91
0
  1. Is a string litteral and the compiler can see the literal and calculate the needed space + the null terminator

  2. str is a pointer that points the first character in the array of characters you enumerate over it just as you would any array. The std library offers a slew of functions to help with character arrays.

rerun
  • 25,014
  • 6
  • 48
  • 78