![]() |
| | #1 |
| Registered User Join Date: Aug 2007
Posts: 81
| I thought pointers were pointers... Code: #include <stdio.h>
int main() {
char a[] = "kiera";
char b[69] = "kiera haha";
char* c = "keira rules";
printf("a = %d, b = %d, c = %d c_string = %s\n", sizeof(a), sizeof(b), sizeof(c), c);
return 0;
}
/** OUTPUT **
*
* a = 6, b = 69, c = 4, c_string = "keira rules"
*
*/
|
| keira is offline | |
| | #2 |
| Registered User Join Date: Jan 2002 Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,787
| Because c is a pointer and the size of a pointer is the size of the pointer and not the size(length) of the data it happens to point to. a and b are character arrays and therefore the size returned is the length of those arrays. If you wanted the length of the data pointed to by c to be output you'd have to print the return value of strlen(c).
__________________ On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. --Charles Babbage, 1792-1871 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 |
| hk_mp5kpdw is offline | |
| | #3 |
| Registered User Join Date: Jul 2006
Posts: 162
| i misunderstood... Last edited by simpleid; 08-15-2007 at 07:52 AM. |
| simpleid is offline | |
| | #4 | |
| Registered User Join Date: Aug 2007
Posts: 81
| Quote:
Also, I don't think you guys understand what I'm asking. I'm asking how does sizeof() know the lengths of the buffers allocated with the syntax char buffer[length], and not the syntax char* buffer = "some string". Personally, I think the reason is that sizeof is kind of like a preprocessor directive. I don't think there is a difference between the pointers at all, but the sizeof() command sees the pointers as different because of how they were declared. Could someone explain more? | |
| keira is offline | |
| | #5 |
| Registered User Join Date: Jul 2006
Posts: 162
| keira, i didn't say that memory wasn't stored as contiguous bytes did i? i told you to imagine something to sort out two aspects, length and size. but i misunderstood your problem to begin with so never mind what i said. |
| simpleid is offline | |
| | #6 | |
| Registered User Join Date: Jan 2002 Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,787
| Quote:
See: http://en.wikipedia.org/wiki/Sizeof
__________________ On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. --Charles Babbage, 1792-1871 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 | |
| hk_mp5kpdw is offline | |
| | #7 |
| Registered User Join Date: Jul 2006
Posts: 162
| i'm curious... how exactly does a computer manage an "array"? int hi[5], is the 5 stored somewhere in memory? and the the fact that "5" points to an int stored somewhere? and then what value each 5 int's point to contained by "hi"? how is all this information managed by only 1 keyword to reference the information, via "hi" ? int is what, 32bit value, and 5 of them, so the computer allocates 5*32 bits of space right?, so then how is the information about the array stored in memory? i'm a little confused... it seems that declaring an array is actually something like... an obscure data type. how does an array -actually- work... ? or am i over thinking it? or since you declare int, it just picks up every 32bit value, puts it in to an int for you, but how does the program know there's a const value attached to it (the size of the array declared)? can this value be mapped to the array name via some other variable? and can this be a work around for passing arrays through functions without needing two inputs, a pointer to the first item and size? Last edited by simpleid; 08-15-2007 at 09:47 AM. |
| simpleid is offline | |
| | #8 | ||
| System Novice Join Date: Jan 2006 Location: Tehran
Posts: 1,074
| For a static array: Code: int myArr[20]; For a pointer Code: int* myPtr = new int[20]; You can see that pointer and array are used in the same way. But remember that they are completely different. Quote:
Compiler knows the size of each array item. So for myArr[3] it will be myArr + 3 * sizeof(int) and means forth item. Quote:
__________________ Microsoft Visual Studio 2008 Professional (On Microsoft Windows XP SP2) Learn the language before using it. (C++ Books and C Books) Read the FAQ before making a problem. Then make a Google and Forum search. My code painter new version Version 0.97 DOWNLOAD NOW! (Let the pop up, pop!) SiavoshKC | ||
| siavoshkc is offline | |
| | #9 |
| Gawking at stupidity Join Date: Jul 2004
Posts: 2,289
| It just increases the stack size (for local automatic arrays) by sizeof(*array) * size bytes. If it's a char array, sizeof(*array) will be 1 (the size of one element of the array). If it's an int array, sizeof(*array) will be 4 (for 32-bit ints). The actual size of the array isn't stored in the executable or memory anywhere. I don't see why you'd think a bunch of information about the array would need to be stored anywhere. During compile time, the compiler knows the type of array, so it can handle all the memory adjustments it needs to. The executable just simply moves around in memory according to the instructions the compiler laid out. The finished executable doesn't even know an array existed. It just knows "I need to go to this memory location".
__________________ If you understand what you're doing, you're not learning anything. Ignore any "advice" esbo tries to give you. It's wrong. |
| itsme86 is offline | |
| | #10 |
| System Novice Join Date: Jan 2006 Location: Tehran
Posts: 1,074
| You can also read this in cplusplus in my articles. And here is another: The question is "Are these two statements equal?" Code: int var[20]; int *var = new int[20]; So why we can use these two statement for both? Code: var[12]; *(var + 12); But for the pointer and dynamic array: executive file does not know the address of the dynamic array's first element (var[0]) in the memory. But it knows the address of a pointer (pointer is a variable that holds a memory address) that points to the first element of the dynamic array. The formula for calculating effective address is: Effective Address = Address of var[0] + (12 * sizeof(var[0]) ) So where is the difference? It is in getting address of var[0]. In the case of pointer, Executive file does not know the address of var[0], it needs to look into the pointer to find that address. Look at the assembly codes for int a = var[12]: For static array: Code: mov eax,dword ptr [ebp-20h] //20h = 32 mov dword ptr [ebp-54h],eax //54h = 84 Code: mov eax,dword ptr [ebp-4] mov ecx,dword ptr [eax+30h] //30h = 48 mov dword ptr [ebp-8],ecx In our example program we have only one array and a variable 'a' in the stack. The first element of array is placed in the address ebp – 80 = ebp - (4 * 20) (Each integer is four bytes and the array has twenty elements) address of variable 'a' is ebp - "size of the whole array" - "size of int" = ebp - 80 – 4 = ebp - 84 For calculating address of var[12] we should subtract 48 from 80 that gives 32 and then minus it from ebp. So it is ebp – 32. [] means "the value that this address points to" it is like dereferencing sign in C/C++ (astrix "*"). First line says place the value which is placed in memory address "ebp - 32" in the eax register (Registers are some small memories in the CPU used for proccessing operations). Now we have var[12] in eax. Then it moves it into variable 'a': In the second line as it says place the value of eax in the memory address "ebp - 84" which is the address of 'a' as we calculated above. Now for the dynamic array case 'a' and 'var' are two local variables placed in the stack both are 32 bits (= 4 bytes). 'a' is an integer and 'var' is a pointer. In my compiler int is four bytes and in my machine pointers are 4 bytes too. The var is the first variable in the stack. Its address is "ebp - 4". The 'a' is placed right after it so has the address "ebp – 8". In the first line, it picks the value of the var (ebp - 4) which is the address of allocated memory returned by new operator. Then puts the address in eax register. After this it needs the value of var[12]. To calculate its address, it adds 48 to eax. Which means adding 48 (= 4 * 12) to address of allocated memory ([ebp - 4]). In the second line it puts the value of var[12] in ecx register. At last in the third line it puts the value of ecx in the 'a' (which has the address ebp - 8). You may ask why in the case of static array it subtract the offset from base address while for dynamic array it adds the offset to base address. Well, this is because of the way that stack and heap memory are managed and is out of scope. The static arrays are placed in the stack portion of memory while dynamic arrays are in the heap portion.
__________________ Microsoft Visual Studio 2008 Professional (On Microsoft Windows XP SP2) Learn the language before using it. (C++ Books and C Books) Read the FAQ before making a problem. Then make a Google and Forum search. My code painter new version Version 0.97 DOWNLOAD NOW! (Let the pop up, pop!) SiavoshKC |
| siavoshkc is offline | |
| | #11 | |
| Registered User Join Date: Aug 2007
Posts: 81
| Quote:
| |
| keira is offline | |
| | #12 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| a and b are arrays. http://c-faq.com/aryptr/index.html > however, c IS THE SAME AS A AND B. No they're not. c = a; is allowed. a = c; is not. Also, the different answers for sizeof tell you that they have to be different. There are a couple of really important exceptions to the "arrays just become pointers to the first element" rule, as explained by the c.l.c FAQ.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only Ģ5.99 a month! |
| Salem is offline | |
| | #13 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,355
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #14 |
| Registered User Join Date: Aug 2007
Posts: 81
| Does this differ in C++? In C++ are arrays not glorified pointers? To demonstrate: Code: #include <iostream>
int main() {
char pointer[] = "keira";
char* otherPointer = pointer;
std::cout << otherPointer << std::endl;
return 0;
}
/** OUTPUT **
*
* keira
*
*/
Last edited by keira; 08-15-2007 at 10:45 AM. |
| keira is offline | |
| | #15 | |
| Registered User Join Date: Aug 2007
Posts: 81
| Quote:
I am simply saying that 4 bytes of memory whose value is an address that is the first element of what we call an array is the same thing as a pointer. I didn't know the C compiler treated arrays differently and I will look into that, thanks guys. | |
| keira is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| sorting number | Leslie | C Programming | 8 | 05-20-2009 04:23 AM |
| 2D Array of Pointers | Slavakion | C++ Programming | 12 | 03-31-2004 05:05 PM |
| Passing pointers between functions | heygirls_uk | C Programming | 5 | 01-09-2004 06:58 PM |
| moving pointers to pointers | Benzakhar | C++ Programming | 9 | 12-27-2003 08:30 AM |
| Array of pointers | falconetti | C Programming | 5 | 01-11-2002 07:26 PM |