•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,798 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,489 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 7303 | Replies: 11
![]() |
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,548
Reputation:
Rep Power: 36
Solved Threads: 860
>>*pch = ch[0];
Example 1-1
Above line is incorrect because pointer pch is uninitialized.
>>ptr = &n[0];
Example 1-3.
This can also be simplified like below: The & operator is not necessary.
ptr = n;
>>ca[3] = '\0';
Figure 2-1
The above is wrong because array ca only has 3 elements, not 4.
>>ptr = &text[0]; // not recommen
Example 2-2
Why is this not recommended? You did not explain that statement.
Example 2-3: I disagree -- it seems to add a lot more complexity to such a simple and elementry operation. The example in Example 2-2 works just find and is the one most frequently used.
>>ptr = (char *)malloc(6);]
Code 2-1
malloc() should not be typecase in C, but is required in C++
Example 3-1 is a very good example of why not to use strcpy() -- because it can easily cause buffer overrun and scribble all over the program's memory.
>> ptr = (char **)malloc(rows * (sizeof *ptr)); // allocate room for rows
Code 3-1
sizeof(*ptr) will result most likely always return 1 becuase sizeof(char) is always 1. This is clearly not what you are attempting to do. The program needs to allocate room for rows number of pointers, like this:
>> ptr[i] = (char *)malloc(strlen(word[i]) + 1 * (sizeof **ptr));
Same problem as above. Should be this:
Figure 4-1 needs some rework to fix up the sizeof() errors, as identified above. sizeof(char*) is not the same as sizeof(char) on most operating systems. And the sizeof(*Book) is the same thing as sizeof(char) -- guarenteed by C standards to always be 1.
Overall, give you B+ for good tutorial. Correct the mistakes and it can be a great tutorial.
Example 1-1
Above line is incorrect because pointer pch is uninitialized.
>>ptr = &n[0];
Example 1-3.
This can also be simplified like below: The & operator is not necessary.
ptr = n;
>>ca[3] = '\0';
Figure 2-1
The above is wrong because array ca only has 3 elements, not 4.
>>ptr = &text[0]; // not recommen
Example 2-2
Why is this not recommended? You did not explain that statement.
Example 2-3: I disagree -- it seems to add a lot more complexity to such a simple and elementry operation. The example in Example 2-2 works just find and is the one most frequently used.
>>ptr = (char *)malloc(6);]
Code 2-1
malloc() should not be typecase in C, but is required in C++
Example 3-1 is a very good example of why not to use strcpy() -- because it can easily cause buffer overrun and scribble all over the program's memory.
>> ptr = (char **)malloc(rows * (sizeof *ptr)); // allocate room for rows
Code 3-1
sizeof(*ptr) will result most likely always return 1 becuase sizeof(char) is always 1. This is clearly not what you are attempting to do. The program needs to allocate room for rows number of pointers, like this:
ptr = malloc(rows * sizeof (char*)) // allocate room for rows
>> ptr[i] = (char *)malloc(strlen(word[i]) + 1 * (sizeof **ptr));
Same problem as above. Should be this:
ptr[i] = malloc(strlen(word[i]) + 1 * sizeof(char **)));
Figure 4-1 needs some rework to fix up the sizeof() errors, as identified above. sizeof(char*) is not the same as sizeof(char) on most operating systems. And the sizeof(*Book) is the same thing as sizeof(char) -- guarenteed by C standards to always be 1.
Overall, give you B+ for good tutorial. Correct the mistakes and it can be a great tutorial.
Last edited by Ancient Dragon : Oct 30th, 2006 at 1:17 pm.
•
•
Join Date: Apr 2008
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
ptr[i] = malloc(strlen(word[i]) + 1 * sizeof(char **)));
Overall, give you B+ for good tutorial. Correct the mistakes and it can be a great tutorial.
nah, this was wrong anyway.
SO BE CAREFUL FOLKS! it has to be:
ptr[i] = malloc((strlen(word[i])+1) * sizeof(char **)));
ptr[i] = malloc(strlen(word[i]) + 1 * sizeof(char **)));
damn someone should change this in the tutorial... I took me some nerves to find out why my app was crashing :-P
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- <help> pointers? (C)
- simpe pointer arithmetic (C)
- Hi i'm new here (C++)
- C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays (C++)
- Pointers (C++)
- Floating point numbers (C++)
- Pointers (Part II) (C)
- ambigious part 4 (C)
Other Threads in the C++ Forum
- Previous Thread: Reading from file
- Next Thread: Quick Sort



Linear Mode