NickDMax's Profile User Rating: *****

Reputation: 2247 Grandmaster
Group:
Alumni
Active Posts:
9,219 (3.52 per day)
Joined:
18-February 07
Profile Views:
78,042
Last Active:
User is offline Today, 02:47 PM
Currently:
Offline

Previous Fields

Country:
US
OS Preference:
Windows
Favorite Browser:
FireFox
Favorite Processor:
Intel
Favorite Gaming Platform:
PC
Your Car:
Nissan
Dream Kudos:
1250
Expert In:
Java/C++

Latest Visitors

Icon   NickDMax "somewhere beneath my feet is a weapon of days honeycombed with neurons come neuroses."

Posts I've Made

  1. In Topic: Is this use of pointer a bad practice?

    Posted 18 Apr 2014

    Lets explore what "const" is for...

    const is asking the compiler to enforce a policy that a particular bit of memory will not change. We might do this for a number of reasons:
    • Address data in ROM
    • Address data in Constant/Protected Data Segment (rodata and text segements)
    • To ensure data integrity
    • To simplify logic


    The first one is generally more of a concern for lower-level programming such as programming micro-controllers.

    The second one pops up quite often when you define static data. A program (at least a windows program) generally has a constant data

    The third one is most common:
    It is often the case that we would like to access memory with a "pointer to a const" to ensure that a function does not attempt to modify the memory. For example:

    void myputs(const char* msg); -- This function has no reason to modify msg. By making its argument const I can pass in a pointer to a string in ROM, or in a const data segment, or just some message that I plan to use again. I know (or have reasonable assurance) that this function will not attempt to modify the data.

    If you think about it this is VERY important in concurrency. Logic is VASTLY simplified if I don't need to preform locks or worry about atomic operations etc. things are much easier. (note that data being accessed as const does not necessarily mean that no locks are needed since other operations may still change the data - point is concurrency is a complicated topic but const does cone in to play simplifying logic).

    So can you assign a pointer to a constant to non-constant data? YES
    Is that a bad thing? Rarely.

    What IS a bad thing is circumventing the const - going the other way!!!
    const char* constMessage = "Hello world";
    
    char * nonConst = const_cast<char*>(constMessage); //This is bad - but sometimes necessary
    


    But sometimes necessary because "const correctness" is rarely followed. Heck I violate it all the time and get all bent out of shape when it turns around and bites me - I SHOULD KNOW BETTER! Luckily static analysis is becoming a big thing and there are tools that can help us get it right (or better).

    So the big issue with const-correctness is when one function violates it. For example in a parser I wrote not too long ago I had wanted to use a library function (in a template class) that was left mutable -- it took DAYS for me to discover the problem. Lucky the boost library I was using made the correction quickly but I learned that one little function in a long chain of classes and functions etc. can break const correctness and force you to cast a way a const just to use it.
  2. In Topic: Struct oR Func. error return?FIRST!

    Posted 16 Apr 2014

    The following compiles... not sure if it works but it compiles:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
     typedef struct sequence {
        char *sequence; //the larger sequence being searched
        int sizesequence; //size of the array sequence
        char *subseq; //the subsequence we are looking for
        int size_subseq; //size of the char array subseq
        int *locations;// a pointer to a list of locations where the string in subseq is found in sequence
        int numlocations;//size of the int array locations
    } Sequence;
    
    char* initseq() {
        //function to create space for the sequence, read in the  sequence from stdio, and
        //return a pointer to the sequence
        char *seq;
        printf("Please enter the sequence to search for: \n");
        seq = (char*)malloc(sizeof(char)*100);
        scanf("%s",seq);
        return seq;
    }
    
    
    
    int main(){
        Sequence DNAcid;
        DNAcid.sequence = initseq();
        return 0;
    }
    
    
  3. In Topic: Compiling problems

    Posted 16 Apr 2014

    This is not an error, it is a warning -- the executable output should have been created.

    The warning comes because you are comparing a signed and unsigned value and there are times when this can cause issues. An unsigned value might seem negative when compared to a signed value or a negative value may appear very large in an unsigned compare.

    If your values are small it does not matter but in general sizes in C++ should be of type size_t (unsgined int)
  4. In Topic: Read the contents of the file getline not working c++

    Posted 16 Apr 2014

    here is a little tighter version of your program - this one does not really work with the initial "17" in the file (although it does read it). You could continue to add lines and this would work.
    #include<iostream>
    #include<fstream>
    #include<cstdlib>
    #include<string>
    #include<cctype>
    #include<vector>
    #include <cmath>
    //if you have cmath you don't need math.h
    
    // you do need ctime for the time() function
    #include <ctime>
    
    using namespace std;
    
    int main () {
    	srand ((unsigned int) time(0));
    	string filename;
    
    	cout << "FILENAME? ";
    	//cin >> filename;
    	filename="beatles.txt";
    
    	ifstream fin(filename.c_str());
    
    	if (fin.is_open())
    	{
    		string line;
    		vector<string> list;
    
    		getline(fin, line);
    		int N = atoi(line.c_str()); //first line not really used but need to be read
    
    		while(getline(fin, line)) {
    			list.push_back(line);
    		}
    		//We put all the lines in the vector, the vector knows how many lines exist.
    		cout << "SENTENCE: " << list[rand() % list.size()] << endl;
    		
    		fin.close();
    	} else {
    		cout << "ERROR: " << filename << " - File not found!";
    	}
    
      return 0;
    }
    
    
  5. In Topic: Read the contents of the file getline not working c++

    Posted 16 Apr 2014

    ummmm... I commented out the cout << line << endl; since it served its purpose of showing the whole file was read.

    Then I ran the program and it seemed to work. I ran it in a loop and never got the error you see.

    does it always give you the same output? (or at least the same symptoms? i.e. a line and a some of the next line?)

My Information

Member Title:
Can grep dead trees!
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:
Location:
PA
Interests:
Mathematics, Programming, Calligraphy
Years Programming:
17
Programming Languages:
VB6, C, C++, IA86 Assembly, Mathematica, Java/JavaEE, Perl, Python

Contact Information

E-mail:
Private
Twitter:
NickDMax

Comments

  • (2 Pages)
  • +
  • 1
  • 2
  1. Photo

    jesicalbaby Icon

    16 Sep 2013 - 14:24
    Hello
    My name is Miss jesical,
    i saw your profile today
    and,i fill more interest to contact you ,
    i will like you to please send me
    an email via (jesicalduncan22@yahoo.com)
    for me to send you my picture
    jesicalduncan22@yahoo.com
  2. Photo

    jesicalbaby Icon

    16 Sep 2013 - 14:22
    Hello
    My name is Miss jesical,
    i saw your profile today
    and,i fill more interest to contact you ,
    i will like you to please send me
    an email via (jesicalduncan22@yahoo.com)
    for me to send you my picture
    jesicalduncan22@yahoo.com
  3. Photo

    ishkabible Icon

    20 Nov 2011 - 20:18
    just getting your attention, lolz
  4. Photo

    NickDMax Icon

    12 Sep 2011 - 22:21
    I don't really check my profile very often. So this is a really poor way to get my attention.
  5. Photo

    NickDMax Icon

    12 Sep 2011 - 22:20
    grep is a function (generally in linux/unix) that searches though files using regular expressions. - The expression "You can't grep dead trees." is a disdain for printed manuals (you of course can grep man pages but not a printed manual).
  6. Photo

    hulla Icon

    08 Sep 2011 - 02:35
    Can you reply so I don't look like a loner, please? :)
  7. Photo

    hulla Icon

    02 Sep 2011 - 01:33
    What does grep mean?
  8. Photo

    hulla Icon

    27 Aug 2011 - 07:33
    Your rep is the year I was born on (1997) :)
  9. Photo

    hulla Icon

    16 Aug 2011 - 01:44
    You should be like, a teacher or a tutor or something because you can Seriously communicate with students . . . Unless you already are one. I wouldn't be surprised if you were a college lecturer or something. :)
  10. Photo

    assert(C) Icon

    21 Jul 2011 - 07:27
    Thanks boss you are great
  11. Photo

    assert(C) Icon

    20 Jul 2011 - 08:18
    hey thanks for the reply in the post
  12. Photo

    hulla Icon

    02 Jul 2011 - 07:43
    Uhh, it's malicious? How?
  13. Photo

    NickDMax Icon

    22 Jun 2011 - 03:05
    @PlasticineGuy -- I don't mean to pick on anyone (usually).
    @ishkabible -- I have only really been posting in one forum so I don't expect it to last.
  14. Photo

    ishkabible Icon

    19 Jun 2011 - 13:33
    hey you topped macosxnerd in rep this month!! no small feat!!
  15. Photo

    PlasticineGuy Icon

    18 Jun 2011 - 06:49
    Every time I see you in a topic I've posted in I feel like I'm about to get a slap on the wrist!
  • (2 Pages)
  • +
  • 1
  • 2