Thread: Memory Issues

  1. #1
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96

    Memory Issues

    Ok I don't really understand. Almost whenever I write a program that requires user input I get this error saying the program caused an error in MSVCRT.DLL and will now close. From previous experience, that is a memory error because of some memory issues. But the type of information the program is storing when it hits the error is int. Is it necessary to "allocate" memory for a int type too!? If someone could please explain this to me that would be a great help. Also, when storing a string, what is better to use? An array like this:
    Code:
    char array[5000];
    
    
    array[5000] = "This is a string";
    or a pointer like this:

    Code:
    char *pointer;
    
    
    pointer = (char*)malloc(5000);
    
    pointer = "This is a pointer";
    Thanks a bunch.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    12
    umm, you could try using strcpy (string copy) to set your array, becuase just doing this array[5000] = "this " will not work and that could be causing your program to crash becuase if have any other place where you are reading in data from the input buffer it will take what is not copied, unless you clear the buffer and your array will never have any valid data in it so , try this when setting an array

    strcpy(array, "This is a string");



    hope that helps you

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    12
    when you are storing a string it is better to use the first method becuase if you ever want to change a string or find the length, etc it will be easier to do

  4. #4
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Ok thanks. But about the first question.... Do you know why my program is crashing?

    I don't really understand. Almost whenever I write a program that requires user input I get this error saying the program caused an error in MSVCRT.DLL and will now close. From previous experience, that is a memory error because of some memory issues. But the type of information the program is storing when it hits the error is int. Is it necessary to "allocate" memory for a int type too!?

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    12
    post your program or at least the part that you believe is causing the crash

  6. #6
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Code:
    int main()
    {
    	char *name = (char*)malloc(50), *job = (char*)malloc(1000);
    	int id, info;
    
    	puts("Enter the ID:");
    	scanf("%d",id);
    
    	puts("Enter employee name:");
    	fgets(name,sizeof(name),stdin);
    
    	puts("Enter employee's duty:");
    	fgets(job,sizeof(job),stdin);
    
    	info = writeinfo(name,id,job); /* This function is contained in a header file of mine
    but I don't think it has anything to do with the error anyway */
    
    	if(info!=0)
    	{
    		puts("Operation Failed");
    		return 1;
    	}
    	else
    	{
    		puts("Success!");
    		return 0;
    	}
    }
    What's happening is that it gets to the part where it says "Enter the ID:" and when I enter any number whatsoever it causes the error and crashes.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    12
    okay try this, make your varibles arrays

    such as char name[50];

    and

    make sure you have at the top of your program

    #include<string.h>

    and

    change your fgets to read like this

    fgets(name);

    and

    try to have one return statement only, this requires an extra varible, but it's a good programming habit

    hope any of these suggestions helps you

  8. #8
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Ok thanks. But what about that integer? It's int id that is causing the error. When scanf enters a value into it the program crashes.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > fgets(name);
    It takes 3 parameters - the original calls were almost correct

    > fgets(job,sizeof(job),stdin);
    sizeof() on a pointer is the size of the pointer itself, not the amount of memory it points to

    Just stick with
    char job[50];
    fgets(job,sizeof(job),stdin);

    > scanf("%d",id);
    This probably explains all your crashes - you forgot the &
    as in
    scanf("%d", &id);

    But you'll want to ditch your use of scanf() if you're also using fgets()
    Use
    char buff[100];
    fgets( buff, sizeof buff, stdin );
    sscanf( buff, "%d", &id);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Ahhhhh Salem. Thank you VERY much. Fixed all those pesky errors. And I can't believe I forgot the &! Show's I'm still new! There's one more little error I've been having... My code looks like this:
    Code:
    fprintf(file_ptr,"id: %d Name: %s Job: %s",id,name,job);
    It puts the info into the file properly but the file looks like this
    id: 100 Name: Test
    Job: Tester Master
    And I have no newline after the name part. So it's supposed to look like this:
    id: 100 Name: Test Job: Tester Master
    Also, one last thing, Just out of curiosity, when is it appropriate to use pointers to store strings? My book seemed to use them a lot but whenever it used them it got it right. Whenever I do, I mess it up. Thanks again.

  11. #11
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by Padawan
    There's one more little error I've been having... My code looks like this:
    Code:
    fprintf(file_ptr,"id: %d Name: %s Job: %s",id,name,job);
    It puts the info into the file properly but the file looks like this

    And I have no newline after the name part. So it's supposed to look like this:
    You have this:
    Code:
    puts("Enter employee name:");
    	fgets(name,sizeof(name),stdin);
    Did you try checking for the newline? Give this a try:

    Code:
    char *p;
    
    fgets(name, sizeof(name), stdin);
    if ((p = strchr(name, '\n')) != NULL) {	 /* check if last element is a newline */
        *p = '\0';		 	         /* if it is, make last element a null character */
    }
    Of course to use strchr(), you need to include string.h

    strchr() finds the first occurrence of the specified character, and returns a pointer to the instance of the character searched for in the string - if it finds nothing, it returns NULL. In this case, if it finds '\n' it returns a pointer to that character, which is then assigned to p. From there it is a matter of replacing '\n' with '\0'

    ~/

  12. #12
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Thanks a lot kermit. That did it! How did the \n get there in the first place?

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You printed the newline in your file right? Well, it was there when it read it back in. Or in the event you're reading from the keyboard, you did hit enter, correct? Well fgets reads that in.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory usage and memory leaks
    By vsanandan in forum C Programming
    Replies: 1
    Last Post: 05-03-2008, 05:45 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Memory Leak Help
    By (TNT) in forum Windows Programming
    Replies: 3
    Last Post: 06-19-2006, 11:22 AM
  4. Memmory Issues and Threads
    By ddod in forum Windows Programming
    Replies: 2
    Last Post: 08-13-2004, 10:30 AM
  5. Memory issues under win 98...
    By deathstryke in forum Tech Board
    Replies: 5
    Last Post: 06-11-2004, 10:03 AM