Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #75256

Re: Memorry error?

From "Osmium" <r124c4u102@comcast.net>
Newsgroups comp.lang.c
Subject Re: Memorry error?
Date 2015-11-07 07:33 -0600
Message-ID <da6cskFckduU1@mid.individual.net> (permalink)
References <892c6f15-df18-4132-a798-5fed68982edc@googlegroups.com>

Show all headers | View raw


"Alla _" > I shudder to think how much scolding (hopefully friendly one as 
usual)
> I will receive upon posting this thread :))) Forestalling that, I would
> like to add that I am running further; the issue that has just popped up
> is unexpected, and has not been discussed in any materials I have studied
> thus far. Therefore, although I'd love to learn more and more, I am not
> intending to get into too much of technical details on this issue.
>
> Below is a tiny program that uses a structure. The program compiles, but
> when I run it in Terminal it produces the result in the unexpected format, 
> and
> also shows an error message.
>
> The program defines a structure 'student' that contains name and house
> variables. Defined are 3 such structures. The goal is to ask for
> student's name and "house", and then print out the result. Please, ignore
> the amount of for loops; it is possible to do the same within one loop, 
> but
> I copy the program from the cs50 source, and these loops are used for
> clarity only.
>
> Result (name is one line, house is on another + error message):
>
> Student's name: David
> Student's house: DC
> Student's name: Laura
> Student's house: Seattle
> Student's name: Kim
> Student's house: Boston
>
> David
> is in DC
> .
> Laura
> is in Seattle
> .
> Kim
> is in Boston
> .
> struct-0(511) malloc: *** error for object 0x7fff68dd39f8: pointer being 
> freed was not allocated
> *** set a breakpoint in malloc_error_break to debug
> Abort trap: 6
>
> I did the following to solve this issue (got this from Google search):
>
> in the Breakpoint Navigator (View->Navigators->Show Breakpoint Navigator),
> entered malloc_error_break in the Symbol field at "Add Symbolic 
> Breakpoint".
>
> I have no idea what all this means, yet, but it didn't help :)
>
> // Student database
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
>
>
> #define STUDENTS 3
>
> // structure representing a student
> struct student
> {
>    char name[80];
>    char house[80];
> };
>
> int main(void)
> {
>    struct student students[STUDENTS];
>    /** [STUDENTS] is the size 3 which is value of
>    constant STUDENTS declared by #define STUDENTS 3 **/
>
>   /** of course we can use only one bigger for loop, but
>   the following three loops demostrate clearily what happens **/
>    for (int i = 0; i < STUDENTS; i++)
>    {
>        printf("Student's name: ");
>        fgets(students[i].name, sizeof students[i].name, stdin);
>
>        printf("Student's house: ");
>        fgets(students[i].house, sizeof students[i].house, stdin);
>    }
>
>    for (int i = 0; i < STUDENTS; i++)
>    {
>        printf("%s is in %s.\n", students[i].name, students[i].house);
>    }
>
>    for (int i = 0; i < STUDENTS; i++)
>    {
>        free(students[i].name);
>        free(students[i].house);
>    }
>
>    return 0;
>
> }

free pairs with malloc (and others), you didn't use malloc  So the array is 
on the stack, it will automatically be freed. Just take the free stuff out 
and throw it away. 

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Memorry error? Alla _ <modelling.data@gmail.com> - 2015-11-07 05:18 -0800
  Re: Memorry error? Alla _ <modelling.data@gmail.com> - 2015-11-07 05:19 -0800
  Re: Memorry error? Alla _ <modelling.data@gmail.com> - 2015-11-07 05:27 -0800
  Re: Memorry error? Alla _ <modelling.data@gmail.com> - 2015-11-07 05:30 -0800
    Re: Memorry error? Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-07 08:36 -0500
      Re: Memorry error? Alla _ <modelling.data@gmail.com> - 2015-11-07 06:43 -0800
        Re: Memorry error? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-07 17:22 +0000
  Re: Memorry error? "Osmium" <r124c4u102@comcast.net> - 2015-11-07 07:33 -0600
  Re: Memorry error? Alla _ <modelling.data@gmail.com> - 2015-11-07 06:47 -0800
    Re: Memorry error? Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-07 10:36 -0500
    Re: Memorry error? Keith Thompson <kst-u@mib.org> - 2015-11-07 13:37 -0800
    Re: Memorry error? Barry Schwarz <schwarzb@dqel.com> - 2015-11-07 16:26 -0800
      Re: Memorry error? Alla _ <modelling.data@gmail.com> - 2015-11-08 23:03 -0800
        Re: Memorry error? Barry Schwarz <schwarzb@dqel.com> - 2015-11-09 01:01 -0800
          Re: Memorry error? Keith Thompson <kst-u@mib.org> - 2015-11-09 08:27 -0800
            Re: Memorry error? raltbos@xs4all.nl (Richard Bos) - 2015-11-17 22:23 +0000
              Re: Memorry error? Keith Thompson <kst-u@mib.org> - 2015-11-17 15:31 -0800
                Re: Memorry error? raltbos@xs4all.nl (Richard Bos) - 2015-11-19 10:41 +0000
      Re: Memorry error? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-11-09 10:26 +0000

csiph-web