![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
For one thing, you can only refer to it with a pointer, not by its actual memory location (which is what the 'name' of an array represents). The compiler does a nice job of simulation. When you refer directly to an array, the array location and a pointer to the array location are often treated similarly. This is an illusion, for your ostensible benefit, as you will see if you refer to the emitted machine code. You might wish to see my tutorial, the sections, "Confustoids", and "What is NOT a pointer".
EDIT: For another thing, if you look at your own example of a "2D array", above, you will see that it is no such thing. It is not contiguous. You cannot derive the address of a given row arithmetically from the origin.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#12 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
climbnorth, I appreciate your enthusiasm in convincing Dietrich to use vectors.
My teammates at work were most amused.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#13 | ||
|
Newbie
Join Date: Oct 2006
Posts: 16
Rep Power: 0
![]() |
Do you have something against me??
I am just mentioning what I like to use and how I use it to accomplish my goals when using an array. My comment about resizing (what I considered to be) an array was posted to be constructive. If you know a better way- please share so we can all learn something. If you know what is wrong with what I had posted, please explain (thats why I started reading this forum, not to be ridiculed over array semantics). DaWei- If you could, please elaborate on your comment: Quote:
Quote:
I am still unsure where my fault is.. if you can, please point out explicitly where my fault is- I am very confused now what is wrong with this code. Also, if it is within the scope of this thread, please also elaborate on the difference between an array and memory allocated and referenced as an array. If you can, please exlain how the offset to the memory is calculated (register names, etc) because I was wondering this and did not find any info online. |
||
|
|
|
|
|
#14 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You cannot address the array you demonstrated in your earlier post with the notation, "array [i][j]." That's one reason it isn't a true array.
The comment regarding the reversal of array notation has noting to do with the subject at hand. When you fuck up, do not whine when corrected (except to your mama). Narue has nothing against you but your tendency to expose your ignorance while proclaiming that another person (Piz) is mistaken. You are the mistaken one. This is the C++ forum. If you care to push the beauties of plain, vanilla C, there is a forum for that. If you care to document your beliefs rationally, feel free to do so. I will not comply with the request in your last paragraph in this thread. Ask your questions explicitly in a new thread. I did address that issue in the tutorial, but it seems as if I have gained a reputation as "cryptic".
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#15 | |
|
Newbie
Join Date: Oct 2006
Posts: 16
Rep Power: 0
![]() |
In response to:
Quote:
|
|
|
|
|
|
|
#16 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Quote:
As Narue said, a pointer can be used to simulate an array i.e. the language treats a pointer as equivalent to an array in some contexts. An actual (as opposed to simulated) array has contiguous elements. So, if v is an array it's elements are placed one after the other in memory (i.e. v[i] is immediately after v[i-1] in memory). This notion carries over to 2D arrays, except that a 2D array is actually a contiguous array of 1D arrays. The example given by climbnorth does not have that property. Instead, an array of pointers is dynamically allocated (and the address of that array of pointers is stored in a pointer to a pointer). Each pointer in that array is then assigned to a dynamically allocated array. Because of this, it simulates a 2D array (in the sense that elements can be accessed by the notation array[i][j]). However, it is no a 2D array, because the individual 1D arrays (accessed by the notation array[i]) are not contiguous. In the end; 1) climbnorth, your example has some properties that make it act like a 2D array, but it is not a 2D array because it's elements are not contiguous. Your example is a method sometimes used in C to simulate a 2D array. It works in C++, but is rarely used in C++ because the C++ library supports a vector type. That's, incidentally, the reason Narue was amused by your post: you gave a response that would be expected from someone who (in her words) came "to C++ from a ten year bender in C". ![]() 2) DaWei, you were correct in pointing out that climbnorth's example is not an array, but mistaken in the statement that it's elements cannot be accessed using the "array[i][j]" notation. 3) Narue is probably chuckling at the way this thread has gone. :beard: In C++, a std::vector<std::vector<int> > is an easier way to simulate a 2D array. |
|
|
|
|
|
|
#17 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
See my apology in the C thread
. I am currently slapping my wrist with a ruler, just like the nuns used to do, because I hate giving misinformation.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#18 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>Do you have something against me??
No. >If you know a better way- please share so we can all learn something. I know a better way, and I've already shared it. Use a vector to avoid the joy of managing memory for a dynamic array. >If you know what is wrong with what I had posted There's nothing wrong with your logic when certain assumptions are in place. Since this is the C++ forum I'll let slide the fact that you cast the return value of malloc, but I'll question why you're using malloc in the first place. Shall we continue? >blah[i] = (char*)malloc(sizeof(char) * lengthofstring); First, sizeof(char) is always 1. Also, unless lengthofstring includes room for the null character, your code has a bug. >memset(blah[i], 0, sizeof(char) * lengthofstring); >memcpy(blah[i], sourcestring, sizeof(char) * lengthofstring); So you're copying the same string into every row of the array? That's somewhat odd, and I'm wondering why you use a memset/memcpy combo when strcpy does the same thing with less overhead (assuming lengthofstring is the same as strlen(sourcestring)+1). I'll assume you did it this way because you're not accounting for the null character when you allocate memory. >please also elaborate on the difference between an array and memory allocated and referenced as an array http://elf.torek.net/torek/c/index.html
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#19 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 242
Rep Power: 3
![]() |
imo stick with vectors, as the overhead is negligible benefiting from lower developing and debuging time. Although double pointers are an alternative option to vectors, (and in all fairness I have also used a similar method before in applications that required dynamic arrays), the learning curve is a lot steeper imo, aswell as being very error prone.
|
|
|
|
|
|
#20 | |||||
|
Newbie
Join Date: Oct 2006
Posts: 16
Rep Power: 0
![]() |
Seif:
I agree, it is a nice luxury to use vectors, but like I had mentioned (and mention several times below) I was using this as an example. This example was posed as an alternate method that some people may like to understand but are still a little shakey on the detail of how this is implemented. I also feel this understanding is essential for people who are still uncertain how to use pointers- and this type of usage will help. Narue: I feel you have misinformed people in your previous set of comments. Also, please stop trying to "retaliate" because you think I'm wrong. I feel it is only confusing people since you keep trying to find flaws in my comments and examples. Below I comment on the damaging statemnts made regarding my examples: Quote:
Here is an example in the spirit of my previous examples, this time using "dynamic binding": const char *hello = "Hello World!";
int main(){
int lenthofstring = strlen(hello)+1;
void *ptr = (char*)malloc(sizeof(char) * lengthofstring);
memset(ptr, 0, lengthofstring);
memcpy(ptr, hello, lengthofstring);
printf((const char*)ptr); // Must cast here
return 0;
}Quote:
Quote:
Quote:
Quote:
|
|||||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| c strings in an array problem | monkijunki2 | C | 6 | Nov 15th, 2005 11:42 AM |
| How to make an array of pointers-to-char point to strings | aznluvsmc | C | 38 | Sep 21st, 2005 8:45 AM |
| Installing IPB 2.03 | bh4575 | Other Web Development Languages | 0 | Apr 23rd, 2005 3:36 AM |