LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-26-2005, 09:50 PM   #1
-0-
Member
 
Registered: Nov 2004
Location: London uk
Distribution: Slack 10
Posts: 67

Rep: Reputation: 15
A Couple of C++ questions, casting


Hey.

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.

Any hints/solutions much appreciated.

Thanks

-0-
 
Old 06-26-2005, 10:09 PM   #2
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
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?
 
Old 06-26-2005, 10:36 PM   #3
-0-
Member
 
Registered: Nov 2004
Location: London uk
Distribution: Slack 10
Posts: 67

Original Poster
Rep: Reputation: 15
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

sorry, perhaps i'm havng a dumb day.

Last edited by -0-; 06-26-2005 at 11:01 PM.
 
Old 06-27-2005, 03:03 AM   #4
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
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].
 
Old 06-27-2005, 08:33 AM   #5
-0-
Member
 
Registered: Nov 2004
Location: London uk
Distribution: Slack 10
Posts: 67

Original Poster
Rep: Reputation: 15
Well i have 2 ideas, could you tell me if one o them is on the right track

1)using some syntax unknown to me: tell the compiler what to do wit heach individual byte

2) cast it back to integer so i can set it's value (static_cast<int*>)

pseudo code of the above

Code:
for int i =0;i<no;i++){
         cast void to int
        assign number to void
        increment pointer
       (possibly ccast back to void)
}
dead wrong? or not?
(any idea about the second question by the way?)
-0-
 
Old 06-27-2005, 10:39 AM   #6
-0-
Member
 
Registered: Nov 2004
Location: London uk
Distribution: Slack 10
Posts: 67

Original Poster
Rep: Reputation: 15
sorry for the double post,

Here's what i've now got:
Code:
 #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.

the second question stll eludes me.

-0-
 
Old 06-27-2005, 11:51 AM   #7
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
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.
 
Old 06-27-2005, 12:14 PM   #8
-0-
Member
 
Registered: Nov 2004
Location: London uk
Distribution: Slack 10
Posts: 67

Original Poster
Rep: Reputation: 15
Cheers for the advice mate.

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.


Thanks for the help guys

-0-
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Couple questions. kuplo Linux - Newbie 0 11-26-2005 10:48 AM
couple questions... c_olin3404 Slackware 3 05-15-2005 05:20 PM
couple questions seifenblasen Linux - Newbie 1 03-25-2005 01:28 PM
A Couple of Questions zoso Linux - General 6 01-14-2005 09:29 AM
couple O' questions pathogenik Slackware 10 04-22-2002 01:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration