|
The format specifier you used in scanf is wrong: Change from
Quote: scanf("%f",&width); to
scanf("%lf",&width);
See scanf - C++ Reference[^]
Of course you need to make modify the other scanf call as well.
|
|
|
|
|
In constructor of my class WorkPackage , I am allocating a stack to each instance of workpackage and adding it to a queue. When a workpackage gets executed, the allocated stack get released in class destructor. The problem is that my destructor gets called before the execution of workpackage and program crashes with segmentation fault. I looked it up and thought that it is because of when i get the package from the queue:
PackageQueue.cpp
WorkPackage PackageQueue::GetWorkPackage(){
if (isEmpty())
{
return WorkPackage();
}
pthread_mutex_lock(&getlock);
WorkPackage data=WorkPackageQueue[front];
if (front == rear)
{
rear = -1;
front = -1;
}
else if (front == 0)
front = size-1;
else
front--;
pthread_mutex_unlock(&getlock);
return WorkPackage(data);
}
I created a copy contructor to allocate a new stack to the instance in case of deletion after GetWorkPackage function
WorkPackage::WorkPackage(const WorkPackage& rhs){
Wp_localstack.local_stack= Stack::make_stack();
m_action=rhs.m_action;
m_arguments=rhs.m_arguments;
}
But the problem is still there..Need hints to how can i do this correctly. I will provide more code if needed
modified 21-Sep-18 12:07pm.
|
|
|
|
|
What is rear?
What is front?
What is size?
What is data?
how is WorkPackage ctor defined?
|
|
|
|
|
rear and front are the indices of a circular array and size is queue length. data is the instance of the WorkPackage that is being retrieved and returned back for execution.
WorkPackage ctor
WorkPackage::WorkPackage(void (*action)(void*), void* arguments) {
Wp_localstack.local_stack= Stack::make_stack();
m_action = action;
m_arguments = arguments;
m_context = make_fcontext(Wp_localstack.local_stack, 1000, m_action);
WorkPackage dtor
WorkPackage::~WorkPackage(){
Stack::release(Wp_localstack.local_stack);
}
|
|
|
|
|
Then your code is doing something wrong, or in the wrong order. Your destructor needs a method of checking whether the stack has been correctly allocated before trying to release it.
|
|
|
|
|
Will need to look at the destructor code.
Since you're returning temporary objects, you could try changing the function signature to WorkPackage&& PackageQueue::GetWorkPackage
«_Superman_»
I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) (October 2009 - September 2013) Polymorphism in C
|
|
|
|
|
Destructor is as follows.
WorkPackage::~WorkPackage(){
if (Wp_localstack.local_stack != nullptr)
{
Stack::release(Wp_localstack.local_stack);
}
}
and release() is follows.
void Stack::release(void * stack ){
int core_id= coreNumber::getInstance()->getCoreID();
WPManagerProviderSingleton& singletonObj =
WPManagerProviderSingleton::getInstance();
WorkPackageManager &wpm = singletonObj.getWpmInstance(core_id);
wpm.PerThreadMemPool.DeAllocate(stack);
}
|
|
|
|
|
|
yaseen ramzan wrote:
is it a correct about destructors Yes.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Hey guys. I don't know much about coding, but I'd like to be a software developer. Lofty goal for sure considering my lack of knowledge. I'm prepared to work at this however. I need to know where to start.
I'll look at reviews for a given book. Some say it's great, some say it's terrible. Some folks say an IDE is fine, while others say that it on obfuscates aspects of programming that the coder should absolutely be familiar with. I haven't a clue!
I like the idea of learning without an IDE holding my hand. Can someone recommend a book that teaches C using a text editor such as Gedit for example? Or any book that would be a good starting point. Thank you in advance.
|
|
|
|
|
|
Thank you very much. I see this book recommended a lot, but the publication date is in 1988. The guy assumes I'm using UNIX. How do I translate that to today? What would I program on?
|
|
|
|
|
Quickbeam1213 wrote: The guy assumes I'm using UNIX C was originally developed on Unix which is why it has so many references to it. But it all works just the same on Windows; and as I gave you the link for the online compiler it does not matter what system you are on. Once you have developed a reasonable understanding you can install one of the free IDEs or compilers on your own system.
|
|
|
|
|
Sorry. I didn't see that link at first. Thank you.
|
|
|
|
|
If you are using Windows and you intend to write your learning code for Windows then you would start with Visual Studio from Microsoft 2017.
1.) It's free for solo developer, just register and download
2.) It's a modern IDE with good debugger and intellisense alone will help you with silly errors.
3.) Opening a console app template to do some of those easy startup lesson is a couple of mouse clicks.
4.) It's the O/S you are familiar with.
I seriously wouldn't consider anything else because it's just a whole pile of junk learning like make etc that you will never use for those other systems. You will also struggle with any unix/linux code as many of the API calls will not be represented under windows and you will need to incorporate posix library substitutes just to get your sample code to compile.
I use GCC, Keil, Green Hills and Eclipse for a lot of other work but they are all much more complex to run on Windows than Visual Studio and when you are starting out what you want is simplicity. You want to concentrate on the C language itself not the toolchain systems.
You will have compiled and run your first programs on VS before you have even worked out how to install and setup many of the other systems.
In vino veritas
modified 17-Sep-18 8:04am.
|
|
|
|
|
To start get a Linux box, the gcc compiler and a text editor (real programmers use vi ).
|
|
|
|
|
ROFL just no, I am assuming that was a joke
I still have an operational punch card reader if anyone still programs like that .. it could be yours for a reasonable cost.
modified 17-Sep-18 11:48am.
|
|
|
|
|
Well, I code every day using vim and gcc on a Linux box.
As you know very well, Windows programming is another topic.
|
|
|
|
|
Oh you were serious
Ok lets be realistic you may do it but even since 2016 there is actually even more programmers using mac's than linux, it's dropped down to number 3. This is not a war about O/S's it's just a user numbers and linux is falling, windows is also falling and mac is picking up the losses. If you go by actual O/S mac is probably now number one because windows O/S use is broken into 7 & 10 with roughly equal numbers and a few still on XP. There is probably 2018 numbers out, but I now that is roughly what it was in 2017.
Even on linux for a new user I would suggest Studio Code, Eclipse or something more updated because if they do go into the serious commercial world it is unlikely an company hiring would be running on vi.
The normal however is that you program on the machine you are most familiar with and use
In vino veritas
modified 17-Sep-18 12:24pm.
|
|
|
|
|
A basic C (console) programmer doesn't benefit from a IDE.
|
|
|
|
|
As per below I respectfully disagree, if they have anything like Intellisense on them they can help a newbie immensely.
In vino veritas
|
|
|
|
|
Nobody said anything about an O/S or popularity of a platform for developing.
If you REALLY want to learn C and, more importantly, how to debug the code you write, doing it without an IDE is a good way to do it. It forces you to go back and research every line of code you write and really understand how that line works.
I started by "writing code", literally with paper and pencil, from books on CPU's and machines that I would never have access to. I've written code in environments without the benefit of a debugger and had to learn to use other statements in the code to help with peeking into the variables. When .NET 1.0 came out as a beta, I was writing code in Notepad and using the command line compilers to learn how this whole new world worked.
Debuggers and IDE are luxuries. Learn the skills without them and you won't be screwed when you're confronted by an environment where you don't have them. You'd be surprised at how often that happens.
|
|
|
|
|
As I said your personal experience doesn't and shouldn't come into it, the OP asked what is the best way.
The best way is the one that is most familiar to the person learning and with the easiest method and that is
usually the O/S they are comfortable. Everything outside that slows the learning process down as they are also
learning O/S stuff unrelated to actual programming itself. An IDE does nothing either way it's simply a front
to the compiler and doesn't change a single thing with the C code you write. All I suggest he does is stay on
the O/S they normally use and see if an IDE makes it easier.
I don't take your claim seriously that a programmer would be get so stuck to an IDE they couldn't work without
it. It sounds more like a story by those who don't have an IDE and trying to justify why they don't. Given that
opensource IDE's like Visual Studio Code are available for every platform it is now also a dead argument as you
can run those sort of IDE's on every platform and they look and work the same on every platform and are free.
Where the really new IDE's are great these days is many have what Microsoft trademarked as intellisense
that is they predict the function as you start typing so you don't make typos and they show you the function
parameters as you type them in and then they will even show you after you type if you have an error before
you even attempt to compile. Again I suspect you are adding personal bias into a very complicated answer.
I personally think IDE use is down to does it make it easier for the person learning.
In vino veritas
modified 17-Sep-18 23:38pm.
|
|
|
|
|
You are correct that the best way is the one that makes sense to the OP.
What I've found, working with other people, a GOOD way is to force them to read documentation and experiment. That's all I was saying.
leon de boer wrote: I don't take your claim seriously that a programmer would be get so stuck to an IDE they couldn't work without
it.
HAHAHA! We have definitely work/worked with different people. I've work with both people who could easily work without an IDE and those who couldn't wipe their ass without the aid of a tour guide. There are plenty of situations where you don't get the aid of an IDE or Intellisense, so how is your newb going to get by if he's completely reliant on it, aka the tour guide?
The IDE, and Intellisense, is no replacement for the development of research skills. THAT is the most important skill you can develop as developer. No IDE or Intellisense is going to help you with that. Putting the cursor on a keyword and hitting F1 is not research.
There plenty of evidence to this right here on CP. You need only to look at the questions in QA.
|
|
|
|
|
Even if I accepted your view there is such people, when or why would they not be able to use the IDE ???
I am fascinated what circumstance lead them to not being able to use the IDE? I actually can't think of a work situation it would ever occur and that is why even if someone was dependent you would never know because they would have it installed.
Lets take CPallani who apparently loves his vi as his editor, but you can get vi for every O/S. So under what situation could you stop him using vi and that programming need become a problem? In his case it really is no different to any IDE he is dependent on an Editor which is just the same as an IDE.
That is why I don't take you answer seriously because I actually can't think how you could ever get that problem to arise. You seem to be naking a mountain out of a molehill that basically could never happen.
In vino veritas
modified 18-Sep-18 9:29am.
|
|
|
|