0

I have a char* within a "news" struct like this:

typedef struct news{
        char *name;
}news;

I read from a file some text (in example I have ever the same line:hello). For the main and my function i wrote this code:

int insert(news **note, char *text,int i);

int main(){
   news *note;
   int i=0,j;
   note = malloc(sizeof(news));

   for (j=0;j<5;j++){
          i=insert(&not,"hello",i);
          printf("%s\n",note[i-1].name);
   }
   system("pause");
}

int insert(news **note, char *text,int i){
    (*note)[i].name = malloc(strlen(text)*sizeof(char));
    strcpy((*note)[i].name,text);
    note = realloc((*note),++i*sizeof(news));
    return i;
}

Why do I get a segmentation fault for j>2? What is wrong?

Jörg Beyer
  • 3,631
  • 21
  • 35
Pascal NoPascensor
  • 171
  • 1
  • 1
  • 14

3 Answers3

3

You have a problem with your allocation.

Instead of

not = realloc((*note),++i*sizeof(news));

use

*note = realloc((*note),(++i + 1) * sizeof(news));

is this working yet?


Edit:

Change also what @Michael Walz said in comment

Result of that is :

int insert(news **note, char *text,int i);

int main(){
   news *note = NULL;
   int i = 0;
   int j = 0;

   if ((note = malloc(sizeof(news))) == NULL) {
     return (-1);
   }

   for (j = 0; j < 5; ++j){
       i = insert(&note, "hello", i);

       printf("%s\n", note[i - 1].name);
   }

   system("pause");
}

int insert(news **note, char *text, int i) {
    (*note)[i].name = strdup(text);

    *note = realloc((*note), (++i + 1) * sizeof(news));

    return i;
}
Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
1

Probably the problem is here:

note = realloc((*note),++i*sizeof(news));

you are reallocating a static pointer because note is a news**

so use

*note = realloc((*note),(++i + 1)*sizeof(news));

thanks to RaNdoM_PoWneD

gior91
  • 1,745
  • 1
  • 16
  • 19
  • we are all giving the same answer ahaha :) – gior91 Mar 28 '14 at 10:49
  • 1
    He just forget to use *note instead of note. If he does what you suggest, the realloc will being lost in memory. Cause note is a local variable – Orelsanpls Mar 28 '14 at 10:50
  • I'm trying the program and the error persists. There is an another issue. – gior91 Mar 28 '14 at 10:52
  • 1
    allocate `code`++i * sizeof(news)`code` is equals to allocate 1*sizeof(news) because i is equals to 0 first time in loop (i equals 1 after ++i). He need to allocate `code`(++i + 1) * sizeof(news)`code` – Orelsanpls Mar 28 '14 at 10:59
1

RaNdoM_PoWneD has provided a technically correct answer.

But your codes are not good. which is why you got into this trouble.

  1. Why are you passing pointers-to-pointers?
  2. What is the purpose of j in your main for-loop?
  3. If you are intending to do things like printf, You need to make room for a /0 when you malloc the .name member. Better still, since you are dynamically allocating it; add another member for the size of .name to the news struct.
  4. Take the advice of DevSolar in the comments to the OP.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct news
{
    size_t size;
    char *name;
}
news;


news* insert(news *note, char *text, int i)
{
    //NB: if note is NULL realloc acts like malloc
    news* new_note=(news*)realloc(note, (i+1)*sizeof(news));
    if (new_note)
    {
        size_t size=strlen(text)*sizeof(char);
        new_note[i].size=size;
        new_note[i].name = malloc(size +1); //make room for /0
        strcpy(new_note[i].name,text); //copies trailing /0 as well
    }
    return (new_note);
}

int main()
{
    news *note=NULL;
    int j,k;

    for (j=0;j<5;j++)
    {
        news* new_note=insert(note,"hello",j);
        if (new_note)
        {
            note=new_note;
            printf("%s\n",note[j].name);
        }
        else //bork
            break;
   }


   //don't forget to free your allocations !
    for (k=0;k<j;k++)
        free(note[k].name);
    free(note);

   return 0;
}

http://ideone.com/DCmYpU

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
Brian Kernighan

violet313
  • 1,912
  • 1
  • 15
  • 19