Thread: Re: [Dev-C++] Malloc Problem
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Luigi S. <gi...@li...> - 2001-07-17 18:09:17
|
In a C program you can ask for memory in a "static fashion" (i.e. at compile time) and in a "dynamic fashion" (i.e. at execution time). When you define "linha1" by the line: char linha1[31]; /* 1 is for the terminating '\0' */ you are using static memory, this means that linha1 will be made of 30 chars during the whole execution, and its size is known at compile time. If you wish to use dynamic memory, then I suggest you use calloc instead of malloc (calloc is for arrays) char *linha1; ... linha1 = (char *)calloc(31, sizeof(char)); /* 1 is for the terminating '\0' */ When you no longer need the dynamically allocated variable linha1, you should release the memory by the following: free(linha1); Dynamic memory is used when the size of the string is not known in advance, since in your code, you already know that no string has more than 30 chars, you should use static memory. In my opinion your code should be: --- #define MAX_SIZE 31 char linha1[MAX_SIZE]; ... fgets(linha1, MAX_SIZE, script); ... --- At 12.51 17/07/01 -0300, you wrote: >I can't find any tutorial about Malloc for strings. >I want to read a string (line) from a file named 'script'. Each line has >one string with diferent size. >The MAX string will have 30 chars. >I know it is very simple, but I can't find the solution. > Thanks, > Iuri >--------- > char linha1 [30]; > char *linha1_ptr = linha1; > linha1_ptr = malloc (30); >... > fgets (linha1_ptr , sizeof(linha1_ptr) , script); > printf(linha1_ptr,"\n"); > fputs(linha1_ptr, tudo); > write(sockfd, linha1_ptr, sizeof(linha1_ptr)); > write(sockfd, "\n\r",2); >------------ --- gisan |
|
From: Luigi S. <gi...@li...> - 2001-07-19 17:26:15
|
Iuri-
Suppose that your problem is the following: you have a text file (say it=20
"script") with lines of different lengths, you wish to load a line and=20
print it on the screen until all the lines have been processed.
script
------
aaaaaa
bbbbb
iuri
------
1- assume that you know that no line has more than 10 chars
Here is how I would solve it.
#include <stdio.h>
#include <assert.h>
#define MAX_LEN 11
#define FILE_NAME "script"
int main()
{
char buf[MAX_LEN];
FILE *inp =3D fopen(FILE_NAME, "r");
assert(inp !=3D 0); /* a quick way to report errors */
while (fgets(buf, MAX_LEN, inp))
printf("%s", buf);
fclose(inp);
return 0;
}
As you can see the second argument of fgets is the size of the array where=
=20
the line will be stored, not the size of the line that will be read. Also=20
keep in mind that fgets will read only MAX_LEN - 1 =3D 10 chars, the last=
one=20
will be used by fgets as the place for the '\0'.
So when you write:
fgets(linha1_ptr, sizeof(linha1_ptr), script);
It is "strange" (because usually you already know how big a string is), but=
=20
OK, if by "sizeof(linha1_ptr)" you mean the length of the string linha1_ptr=
=20
before the call to fgets, it is not correct if you mean: "read the line and=
=20
get its length, return this length as the value of sizeof(linha1_ptr)".
Suppose you say: (assume linha1_ptr is an array of 31 chars)
fgets(linha1_ptr, 31, script); /* and load the string "123\n" */
/* now sizeof(linha1_ptr) is 4 */
fgets(linha1_ptr, sizeof(linha1_ptr), script); /* trying to load "iuri\n" */
/* fgets will read 3 chars, "iur", the fourth will be '\0' */
Of course when you say:
fgets(linha1_ptr, 31, script); /* and load the string "123\n" */
fgets(linha1_ptr, 31, script); /* trying to load "iuri\n" */
/* now "iuri" will be loaded as expected */
Hope this clearly explains what should be used as the second argument of=
fgets.
2 - assume that you do not know the size of the biggest line
Here is how I would solve it.
#include <stdio.h>
#include <assert.h>
#include <string.h>
#define FILE_NAME "script"
char *getline(FILE *);
int main(int argc, char *argv[])
{
char *buf;
FILE *inp =3D fopen(FILE_NAME, "r");
assert(inp !=3D 0);
while (buf =3D getline(inp))
{
printf("%s", buf);
free(buf);
}
fclose(inp);
return 0;
}
/*
getline =3D read a line from the file, return the line read.
The string that is returned is always terminated by a '\n'
*/
#define SIZE 6
#define GET_MEM (char *)calloc(len + 1, sizeof(char))
char *getline(FILE *file)
{
char buf[SIZE];
char *line =3D 0;
int len =3D 0;
while (fgets(buf, SIZE, file))
{
char *tmp =3D line;
int n =3D strlen(buf);
len +=3D n;
line =3D GET_MEM;
assert(line !=3D 0);
if (tmp)
{
strcpy(line, tmp);
free(tmp);
}
strcat(line, buf);
if (buf[n - 1] =3D=3D '\n') /* finished reading line */
break;
}
return line;
}
Hope this is of some help
At 14.46 18/07/01 -0300, you wrote:
>Dear Luigi,
> Thank you for your help. I tried to use calloc because de string=
=20
> read from the
>file string has diferent sizes. I.e. the first line of the file script is=
=20
>"iuri", when I write:
>fgets (linha1_ptr ,sizeof(linha1_ptr), script);
> It returns: iur
>If I write:
>fgets (linha1_ptr , 30 , script);
> It returns: iuri
>
>With static memory the problem is that it reads the hole thing:
>So if my string is:
> aaaaaaaaaa
> bbbb
>With the fgets it will read:
> aaaaaaaaaa
> bbbb\0aaaa
>
>I think with sizeof it shouldn't happen, but it does. He reads the size of=
=20
>the 1=BA line and
>returns this same size for every line, even if it's bigger.
>
> Thanks,
> Iuri
---
gisan
|
|
From: veenurs <ve...@ma...> - 2003-02-22 20:01:14
|
Hi !
It seems too simple .But what is it !?
if( (buffer = ( PBYTE )calloc( 1000 ,sizeof(BYTE) )) == NULL ){
------
-------
}
size = _msize( buffer );
The size I get it is not 1000 , but 1008 ! Any subsequent call to
realloc does not add the 8 bytes,though .
[ Dev Cpp Version 4.9.7.0 ]
What's happening ?
BTW - "Generate debugging info" is OFF .
Thanks in advance .
-Rahul.
Luigi Santillo wrote:
> Iuri-
> Suppose that your problem is the following: you have a text file (say
> it "script") with lines of different lengths, you wish to load a line
> and print it on the screen until all the lines have been processed.
>
> script
> ------
> aaaaaa
> bbbbb
> iuri
> ------
>
> 1- assume that you know that no line has more than 10 chars
> Here is how I would solve it.
>
> #include <stdio.h>
> #include <assert.h>
>
> #define MAX_LEN 11
> #define FILE_NAME "script"
>
> int main()
> {
> char buf[MAX_LEN];
> FILE *inp = fopen(FILE_NAME, "r");
>
> assert(inp != 0); /* a quick way to report errors */
>
> while (fgets(buf, MAX_LEN, inp))
> printf("%s", buf);
>
> fclose(inp);
>
> return 0;
> }
>
> As you can see the second argument of fgets is the size of the array
> where the line will be stored, not the size of the line that will be
> read. Also keep in mind that fgets will read only MAX_LEN - 1 = 10
> chars, the last one will be used by fgets as the place for the '\0'.
> So when you write:
>
> fgets(linha1_ptr, sizeof(linha1_ptr), script);
>
> It is "strange" (because usually you already know how big a string
> is), but OK, if by "sizeof(linha1_ptr)" you mean the length of the
> string linha1_ptr before the call to fgets, it is not correct if you
> mean: "read the line and get its length, return this length as the
> value of sizeof(linha1_ptr)".
>
> Suppose you say: (assume linha1_ptr is an array of 31 chars)
>
> fgets(linha1_ptr, 31, script); /* and load the string "123\n" */
> /* now sizeof(linha1_ptr) is 4 */
>
> fgets(linha1_ptr, sizeof(linha1_ptr), script); /* trying to load
> "iuri\n" */
> /* fgets will read 3 chars, "iur", the fourth will be '\0' */
>
>
> Of course when you say:
>
> fgets(linha1_ptr, 31, script); /* and load the string "123\n" */
> fgets(linha1_ptr, 31, script); /* trying to load "iuri\n" */
> /* now "iuri" will be loaded as expected */
>
> Hope this clearly explains what should be used as the second argument
> of fgets.
>
> 2 - assume that you do not know the size of the biggest line
> Here is how I would solve it.
>
> #include <stdio.h>
> #include <assert.h>
> #include <string.h>
>
> #define FILE_NAME "script"
> char *getline(FILE *);
>
> int main(int argc, char *argv[])
> {
> char *buf;
> FILE *inp = fopen(FILE_NAME, "r");
>
> assert(inp != 0);
>
> while (buf = getline(inp))
> {
> printf("%s", buf);
> free(buf);
> }
>
> fclose(inp);
> return 0;
> }
>
> /*
> getline = read a line from the file, return the line read.
> The string that is returned is always terminated by a '\n'
> */
>
> #define SIZE 6
> #define GET_MEM (char *)calloc(len + 1, sizeof(char))
>
> char *getline(FILE *file)
> {
> char buf[SIZE];
> char *line = 0;
> int len = 0;
>
> while (fgets(buf, SIZE, file))
> {
> char *tmp = line;
> int n = strlen(buf);
>
> len += n;
> line = GET_MEM;
> assert(line != 0);
>
> if (tmp)
> {
> strcpy(line, tmp);
> free(tmp);
> }
>
> strcat(line, buf);
>
> if (buf[n - 1] == '\n') /* finished reading line */
> break;
> }
>
> return line;
> }
>
> Hope this is of some help
>
> At 14.46 18/07/01 -0300, you wrote:
>
>> Dear Luigi,
>> Thank you for your help. I tried to use calloc because de
>> string read from the
>> file string has diferent sizes. I.e. the first line of the file
>> script is "iuri", when I write:
>> fgets (linha1_ptr ,sizeof(linha1_ptr), script);
>> It returns: iur
>> If I write:
>> fgets (linha1_ptr , 30 , script);
>> It returns: iuri
>>
>> With static memory the problem is that it reads the hole thing:
>> So if my string is:
>> aaaaaaaaaa
>> bbbb
>> With the fgets it will read:
>> aaaaaaaaaa
>> bbbb\0aaaa
>>
>> I think with sizeof it shouldn't happen, but it does. He reads the
>> size of the 1º line and
>> returns this same size for every line, even if it's bigger.
>>
>> Thanks,
>> Iuri
>
>
> ---
> gisan
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|