Thread: Having Run time check failure #2-stack around the variable "numbers"

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    30

    Having Run time check failure #2-stack around the variable "numbers"

    Having Run time check failure #2-stack around the variable "numbers"
    Need help thank you
    my input file is below

    5
    8
    10



    below is my code

    Code:
    ..
    
    #include "stdafx.h"
    #include <ctype.h>
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	FILE *test;
    	int j=0;
    	int numbers[]={0};
    	int record[]={0};
    	int i=0;
    	char *array1;
     	if((test=fopen("Input1.txt","r"))==NULL)
    	{
    		printf("File could not be opened\n");
    	}
    	else
    	{
    		array1 = (char*)malloc(1*sizeof (char));
    		if((test=fopen("Input1.txt","r"))==NULL)
    		{
    			printf("File could not be opened\n");
    		}
    		else
    		{
    			while(fgets(array1,(sizeof array1)-1,test)!=NULL) 
    			{
    				numbers[i]=atoi(array1);
    				i++;
    			}
    			for(i=0;i<sizeof(array1)-1;i++)
    			{
    				printf("%d\n",numbers[i]);
    			}
    		}
    	fclose (test);
    	}
    	system("pause");
    	return 0;
    	free(array1);
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    In the following snippet:
    Code:
    int numbers[]={0};
        int record[]={0};
    What do you think the size of these arrays will be?
    Also:
    Code:
    array1 = (char*)malloc(1*sizeof (char));
    How many characters are you allocating for this array? Do you think you are actually allocating enough memory for a C-string.

    Jim
    Last edited by jimblumberg; 07-04-2012 at 08:10 PM.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    I want the size of array depending on the file line. so i write like this so that the size of array is vary.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    You must supply a size for the array. Also in order to use atoi() your array1 array must contain at least two characters one space for a single digit, and one character for the end of string character.

    Jim

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    Thanks Jim. I solve the stack error. but can u further explain for the atoi()? not sure what you mean by it. Thank you very much.

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    And Jim, if i really dont know the size of array, what i should do? as my text file will be very long. this is just make me easy to code and debug only

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    Any help?

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    The following:
    Code:
    array1 = (char*)malloc(1*sizeof (char));
    Allocates space for exactly 1 character. In C strings are arrays of char terminated with the end of string character '\0'. The atoi() function works with C strings. Therefore allocating exactly one character for your read buffer does not allow room for a character plus this end of string character. Since this is just a buffer for reading one line of your text file I would skip the malloc() call altogether and just use a fixed sized C string for your buffer. Maybe a string with a length of BUFSIZ which is a value defined in stdio.h. You should also not cast the return value of malloc in a C program and since sizeof(char) is one you don't actually need the multiplication either.
    Code:
     char buffer[BUFSIZ]; // Declare a buffer to use for extracting a line from a file.
    char *array1 = malloc(1024);  // Declare and allocate 1024 bytes to the array array1.
    If you don't know how many numbers will be in your file you will need to use the malloc/calloc/realloc to allocate the memory for your array of numbers.

    Jim

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    Jim, sorry for troublesome you again. I go to website study about buffer[BUFSIZ], but i cannot read it into a variable and store it. and for the

    char *array1 = malloc(1024); // Declare and allocate 1024 bytes to the array array1.
    the malloc is void and how it going to assigned to char.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by zhengkoon8
    the malloc is void and how it going to assigned to char.
    The return type of malloc is void*, not void. There is an implicit conversion from void* to char* (and indeed to any other pointer to object type).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    ok. if i am using realloc, how this function be used?

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by zhengkoon8 View Post
    ok. if i am using realloc, how this function be used?
    Probably very badly, if the track record of posters on this forum is anything to go by.

    How about finding out the length of the file first, then allocating the whole amount using just malloc, then reading the file into that?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
        return 0;
        free(array1);
    }
    The return statement will exit the function/program and the call to free will not happen. The free call must be before the return statement.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-10-2011, 06:00 PM
  2. Replies: 6
    Last Post: 11-05-2010, 10:34 AM
  3. Replies: 9
    Last Post: 09-14-2010, 07:16 AM
  4. Replies: 14
    Last Post: 11-17-2008, 12:31 PM
  5. Replies: 3
    Last Post: 05-22-2007, 11:42 PM