ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Recently i've been working my way through "Thinking in C++" by Bruce Eckel, in an attempt to teach myself C++.
In the third chapter (where he goes through the majority of relavent C syntax to C++, and a few C++ operators). I'm doing the exercises at the end, but a few ofthem have me really stumped. I asked a C coder friend (not much help, he knows no C++), and i've tried googe. I was wondering if anyone could help me.
The 2 questions getting me right now follow:
26. Define an array of int. Take the starting address of that array and use static_cast to convert it into an void*. Write a function that takes a void*, a number (indicating a number of bytes), and a value (indicating the value to which each byte should be set) as arguments. The function should set each byte in the specified range to the specified value. Try out the function on your array of int.
the code i have for this so far (non working, but i'm unsure of the method to follow):
Code:
#include<iostream>
//bad practise i know, but it saves typing when i'm only using std
using namespace std;
void func(void*, int, int);
void func(void* addr, int no, int val){
for (int i=0; i<no; i++){
addr[i]=val; //compiler errors here
}
}
int main() {
int a[10];
void* b =static_cast<void*>(&a);
func(&b,10,3);
for (int i=0;i<10;i++){
cout << a[i] << endl;
}
return 0;
}
Any ideas? I know i've got something wrong, but i'm unsure how to handle it.
Second question:
27. Create a const array of double and a volatile array of double. Index through each array and use const_cast to cast each element to non-const and non-volatile, respectively, and assign a value to each element.
my answer so far (not much of it i know, but hey):
Code:
//playing around with const cast
//ticpp vol 1 chap3 ex 27
#include<iostream>
using namespace std;
int main (){
const double a[10];
volatile double b[10];
for (int i =0;i<10;i++) {
a[i]=const_cast<double>(a[i]);
/*a[i]=i*i;
const_cast<double>(b[i]);
b[i]=i/2*/ //this commented while i try to figure out what i'm doing
}
return 0;
}
So far i've been able to understand most of what i've been doing. But casting, and bit-banging confuses me somewhat.
Well, you have to try and pretend like you are the compiler (well not exactly since the compiler doesn't exercise human reason, but I hope you get the idea). If you (as the compiler) look at the instructions for the function the programmer gave you, you are supposed to be able to generate machine code for it. So you start looking at it and think:
Ok, it's a function, takes 3 parameters-- a void pointer and two integers. So far so good.
Now he wants to initialize a local integer, i, to 0 and loop, incrementing i until it exceeds the parameter, no. Well, that's a mouthful, but it's simple enough.
Now in the body of the loop, he wants me to take the void pointer he passed in and apply pointer arithmetic to it-- Whoa! Now how am I supposed to do that? I mean, a void * points to a memory address somewhere, but if the programmer wants me to index into it, I need to know what the size of the elements are? I mean, if it was an int *, and ints were 2 bytes each, and the pointer started at address 10, then index 0 would be address 10, index 1 would be address 12, index 2 would be address 14, etc. But this guy hasn't told me *anything* about what's being pointed to! Hmm, looks like he's trying to assign an int to it, but he could have just made a typo. I mean, that is my job. I'm supposed to bark at him when he tries to assign one type to another, right? Well then...
**ERROR**
Does that help you make any sense of your problem?
IIRC an integer on g++ is 4 bytes (last time i checkd anyway)
would i need some kind of while loop, keeping an eye on the size of the array, each iteration adding one to the pointer till it reached `length of array`*4?
or is that way off?
EDIT: no i must admit i still don't get it...perhaps it's the fact it's 5am here.
I've had a think, i'll try again tomorrow, but a slightly bigger hint might help
Well, what carl was saying is, precisely, that despite you know the size of the integer, the compiler cannot, because as far as it knows addr is just a pointer: when you cast it and give it to the function, it "forgets" that it was a pointer to an integer array, and therefore it doesn't know what to do with addr[i].
#include<iostream>
using namespace std;
void func(void*, int, int);
void func(void* addr, int no, int val){
int* add=static_cast<int*>(addr);
for (int i=0; i<no; i++){
add[i]=val;
}
}
int main() {
int a[10];
void* b =static_cast<void*>(&a[0]);
func(&b,10,3);
for (int i=0;i<10;i++){
cout << a[i] << endl;
}
return 0;
}
i'm not even sure if that's the right method, but it works...with 1 caveat, the last value in the array doesn't get touched. it starts at the beggining of the array, indexes across 10, but still doesn't hit it.
from playing around i know that if no is set to 11 it works correctly.
Can anyone see what's wrong here. I've not been able to work it out.
What kind of book teaches static_cast's in chapter 3? That is rarely used in C++, and the function the author asked you to write is quite dumb, and look, now he has you stuck on something that you shouldn't even be concerned about in the first place. I would drop it, this isn't C although the author doesn't seem to know that...
Skip, and move on, preferably to a better book. The author might have hit the mark with his Java book, but I wish he wouldn't pretend to know everything.
As it is, i'd read good things about this book, that's why i started with it. I've decided that if i don't get any ideas anytime soon, i'm going to move onto the next chapter.
The problem I think is that it is kind of aimed at people who can already code (i can but only basics, i'd learnt a bit of c++ in uni before this and decided to get myself upto a decent standard)
with the exception of 5 or 6 questions out of 35 (by far the longest chapter in the book see) i could do most of them with barely any sweat. It's just that he goes over the c syntax in c++ in one long chapter, and introduces a few similar C++ operators, where they are "better" than the c alternative.
I'm with you on the question being rather dumb though, the ones i don't get often are something inane such as this, where i can't think of a decent use for it other than something much further low level that i know at this moment.
I'll leave the double question (and be happy with my almost there on the cast to void question), do the last excercise (writing a makefile, easy ) then move onto chapter 4.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.