Anthony Aj Williams

author
+ Follow
since Jun 10, 2011
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
6
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Anthony Aj Williams

Yes, that's more idiomatic C: the return value indicates success/failure (and possibly an error code for failure if you return int), while the struct to hold the output data is passed in by pointer.
5 years ago
You need



on the failure path and



on the success path.

You can't implicitly convert from an integer or boolean to a struct in C.
5 years ago
If the return type of your function is "struct Coffee", then all return statements must return a value of type struct Coffee. The "failure" return path must therefore specify its failure in the return type itself, e.g. with special values, or via another means such as errno or by adding an extra function parameter which is a pointer to a success/failure value.
5 years ago
It appears that you are thoroughly confused about C++. I would strongly suggest that you go away and read a proper tutorial. Bjarne Stroustrup's tour of C++ is probably a good place to start.

Anyway, to address your problems:



Looking at the rest of the code, this should probably be setAge, not Age. This is a member function declaration. The void specifies that there is no return value, and the (int) specifies that it takes one argument of type int.



This is an out-of-class definition for the member function setAge. The :: is the scope operator. It tells the compiler that this is the setAge member function of the Age class, rather than just a free function that does not belong to any class.



This won't work on several levels. Firstly, the data member is ages, which is all lower case --- C++ is case-sensitive. Secondly, the ages data member is private, so cannot be accessed outside class member functions.

In order to retrieve the value you could either make the ages member public, which would eliminate the need for the setAge member function, or you could add a getAge member function as you show below.

here is my question why is this need to be => private:
int ages;
need to be public ?



It is considered good practice to make your data members private, if there is any behaviour associated with your class. If it is merely a data holder then they should be public.


also do i need need to put this?


what happen if forget about that?



int and public are all lower-case in C++.

This function would allow non-member functions such as main to retrieve the value of the ages data member, while keeping the data member itself private. If you omit the function then you cannot read the value of ages from outside member functions of the class.
9 years ago

Jose Kampilan wrote:


i've tried removing the asterisk from the iterator (i.e. deletion = w)



That won't work: deletion is of type Student, whereas w is an iterator.

Jose Kampilan wrote:
and also tried to copy the syntax of the example from list::erase C++ (w = input.erase(w)) but sadly both of them didnt work.



What do you mean by "didn't work" in this case? w=input.erase(w) is what you should use here. However, this updates w to refer to the next element, so the w++ in the loop header is now wrong, and will skip elements, or move off the end of the list.

What you need is to only increment w if the entry was not found:




11 years ago

J Shankar wrote:Please help me to understand "how the pointer is represented for Char and String" in the above scenario.



A string literal produces a "null-terminated string" in the generated code. This is a series of characters followed by a zero byte. So, "I am a string" takes up 14 bytes: one for each of the 13 characters in the string, plus a final zero byte.

A pointer holds an address. On 32-bit architectures this is usually a single 32-bit value which the processor can use to identify the location of some data. If you say



Then ptr is declared as a pointer to char. In this case, the pointer holds the address of the first character in the string, I. Since the subsequent characters in the string are stored at adjacent locations, your code can find the rest of the string from this pointer to the first character, checking each character in turn until it finds the zero byte at the end of the string --- this is why the string is null-terminated.

The pointer-dereference operator * retrieves the value stored at the pointed-to location. Since ptr is a pointer-to-char, the value stored is a character, in this case the [b]I[/bb] that is the first character in the string.

When you print things with printf, %c says to print a character, and %s to print a null-terminated string identified by a pointer to its first character, so you get the I I am a string output you describe.
11 years ago

Mahesh Chandran wrote:Hi all,
I am put together the following code segments from his book.

#include <iostream>
#include <map>
#include <string>
#include <iterator>
using namespace std;

map<string, int>histogram;
void record (const string &s)
{
histogram[s]++; //record frequency of "s"
cout<<"recorded:"<<s<<" occurence = "<<histogram[s]<<"\n";
}

void print (const pair<const string, int>& r)
{
cout<<r.first<<' '<<r.second<<'\n';
}

bool gt_42(const pair<const string, int>& r)
{
return r.second>42;
}

void f(map<string, int>& m)
{
typedef map<string, int>::const_iterator MI;
MI i = find_if(m.begin(), m.end(), gt_42);
cout<<i->first<<' '<<i->second; //this gives an error Exception: STATUS_ACCESS_VIOLATION
}

int main () {
istream_iterator<string> ii(cin);
istream_iterator<string> eos;
cout<<"input end\n";

for_each(ii, eos, record);

//typedef pair <string, int> String_Int_Pair;
//histogram.insert(String_Int_Pair("42", 1));
//histogram.insert(String_Int_Pair("44", 1));


//for_each(histogram.begin(), histogram.end(), print);
f(histogram);

}

I get an error - Exception: STATUS_ACCESS_VIOLATION, I think in the referencing i->first, i->second, I guess. Can someone help me out find out what the issue might be. Also if you can suggest some alternate C++ forums that will be helpful as well.



It does indeed look likely that the problem is in the reference to i->first and i->second in f(). The problem is that if there is no matching element, find_if/[tt] returns the end iterator for the range passed to it, in this case [tt]m.end(). The end iterator for a container is not dereferencable --- it is "one past the end" rather than the last item, so that m.begin()==m.end() is an empty range. You therefore need to only dereference it if an element was found:

12 years ago

Punit Jain wrote:can you suggest me any good book for c and c++??



For C++ I would recommend "Accelerated C++" by Koenig and Moo. I can't think of a good introductory book for C, and I'm inclined to say that it's not worth learning C separately until you really need to, as good C idioms are quite different from good C++ idioms, and learning both will lead to confusion, and make your C++ worse.
12 years ago

Punit Jain wrote:i want to know how do i create linked list using array, and how can i create array using linked list???
with example...



Creating a linked list from an array is straightforward:



Creating an array from a linked list is less so --- you need to dynamically allocate your array and copy in the data:



Using dynamic arrays is generally a bad idea though. It's better just to use std::vector instead, which manages the memory for you automatically.
12 years ago

Robert Heath wrote:I wonder if there would be a scheduler for Java to be in the JVM in addition to the scheduler for the native OS. Plus where do the concurrency primitives live for Java, in the JVM or the native OS? In C++ I would expect to see the only scheduler to be in the OS. But I really don't know.

Has anyone seen performance benchmarks to measure the time from suspending one process, because of concurrency primitives (like semaphores), and beginning the next process in C++ and Java.



The details will clearly depend on the JVM, the C++ compiler and runtime, and the OS.

I am not aware of any such benchmarks.
12 years ago

Ankit Yadav wrote:not exactly NUMA, or may be, I have little knowledge about NUMA
i think i forgot to mention that I were talking in reference to multi-core/processor system, with having both shared & exclusive memory/cache.

I am asking that, how does the memory hierarchy say RAM, L1/L2/L3 cache, affects the concurrency model, especially in terms of performance & data integrity?
moreover, does having more leyers of either shared-memory or non-shared-memory has any effect over the performance of overall system?



C++11 assumes that all memory is accessible by all threads.

It is possible that different memory regions are "closer" to some cores than others, so accesses from that core are faster than from others. However, the C++11 model doesn't provide any facilities to determine which core your thread is running on, or provide processor/core affinity for threads or memory allocations. For that, you are left to the whim of your OS.
12 years ago

Ankit Yadav wrote:thanks for your valuable response. One more doubt I am having is that,
how does the different level/hierarchy of exclusive memory and shared-memory restricts/facilitates the implementation of concurrency?



I am sorry, but I'm not sure I understand exactly what you are asking. Could you elaborate on what you mean by exclusive memory and shared memory. Are you asking about NUMA systems?
12 years ago

Tina Smith wrote:What's different about threading/concurrency in C++11 vs previous versions?



Prior to C++11, standard C++ didn't acknowledge the existence of concurrency, so any threading facilities provided were implementation-specific extensions.

In C++11, not only is there a multithreading-aware memory model, but the standard library now has the basic threading facilities --- mutexes, condition variables, and threads --- as well as some more advanced ones --- futures, promises, and async tasks.

Tina Smith wrote:Is there any functionality that Java or C++ provides over the other as far as concurrency is concerned?
Which, in your opinion, is easier/better to use?



The Java standard library provides facilities such as containers designed for concurrent access which are not part of the standard C++ library, but must be obtained from third parties or written yourself.

I prefer the way that C++ handles threading to the Java way, but a large part of that is due to the difference in structure between the languages. C++11 has lambdas, where Java only has inner classes, and C++ has the concept of "callable objects", whereas in Java you have to derive from an interface and implement a method.
12 years ago

Hussein Baghdadi wrote:Hi,
How do you compare C++11 concurrency support to Grand Central Dispatch?
Any plans or possibilities to include GCD as a part of C++11?
Thanks.



As I understand it, GCD provides a thread pool implementation: you submit tasks, and the runtime executes them concurrently when possible. The basics of this are covered by std::async, but it's not a complete match. Thread pools are currently something under discussion for the next C++ standard, and there are existing C++ libraries that provide them, but GCD will not be included as-is --- for one thing, it's a C level API that does not deal with exceptions.
12 years ago

Pradeep bhatt wrote:It has been several years since I worked on cpp. Where can I gets started. which free cpp software can i download ? I am using a windows 7 laptop.



gcc is free, and ported to a wide variety of OSes, including Windows. The best Windows port of gcc is the TDM build.

You can also get the free express edition of Microsoft Visual C++
12 years ago