read file dynamically and don't use unnecessary memory in C
I need to write a program which read a text file for example:
Peter
Jack
John
In here main aim is dont's use extra memory. And so i wrote this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(){
FILE *file_pointer;
file_pointer = fopen("humans.txt","r");
value = (char *) malloc(sizeof(char)); // I think its most possible
//writing with don't use extra memory
while( fscanf(file_pointer,"%s", value) != EOF ){
printf("Name: %s Size of name: %d",value,strlen(value));
}
}
when i run with some text file sample, there was no problem but when i run with valgrind, it gave me this error:
==22726== Invalid read of size 4
==22726== at 0x4EA21BD: __isoc99_fscanf (in /usr/lib64/libc-2.17.so)
==22726== by 0x400716: main (in /mnt//ogr/bxxxxx/a)
==22726== Address 0x0 is not stack'd, malloc'd or (recently) free'd
So, how can i write this code efficiently, how can i allocate memory don't use extra memory to read text file ?
c malloc valgrind
add a comment |
I need to write a program which read a text file for example:
Peter
Jack
John
In here main aim is dont's use extra memory. And so i wrote this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(){
FILE *file_pointer;
file_pointer = fopen("humans.txt","r");
value = (char *) malloc(sizeof(char)); // I think its most possible
//writing with don't use extra memory
while( fscanf(file_pointer,"%s", value) != EOF ){
printf("Name: %s Size of name: %d",value,strlen(value));
}
}
when i run with some text file sample, there was no problem but when i run with valgrind, it gave me this error:
==22726== Invalid read of size 4
==22726== at 0x4EA21BD: __isoc99_fscanf (in /usr/lib64/libc-2.17.so)
==22726== by 0x400716: main (in /mnt//ogr/bxxxxx/a)
==22726== Address 0x0 is not stack'd, malloc'd or (recently) free'd
So, how can i write this code efficiently, how can i allocate memory don't use extra memory to read text file ?
c malloc valgrind
You seem to have forgotten thatcharstrings in C are really called null-terminated byte strings. That null-terminated bit is crucial and marks the end of the string. It also means that a string of N characters needs space for N+1 to fit the terminator. Sincescanfwill read at least one character and then add the terminator, it will write at least two bytes to the memory pointed to by the argument. If you don't know the size, you need to read one character at a time, and reallocate as needed.
– Some programmer dude
Nov 18 '18 at 8:36
I have forgotten this, thanks to remark
– okydoky
Nov 18 '18 at 8:41
add a comment |
I need to write a program which read a text file for example:
Peter
Jack
John
In here main aim is dont's use extra memory. And so i wrote this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(){
FILE *file_pointer;
file_pointer = fopen("humans.txt","r");
value = (char *) malloc(sizeof(char)); // I think its most possible
//writing with don't use extra memory
while( fscanf(file_pointer,"%s", value) != EOF ){
printf("Name: %s Size of name: %d",value,strlen(value));
}
}
when i run with some text file sample, there was no problem but when i run with valgrind, it gave me this error:
==22726== Invalid read of size 4
==22726== at 0x4EA21BD: __isoc99_fscanf (in /usr/lib64/libc-2.17.so)
==22726== by 0x400716: main (in /mnt//ogr/bxxxxx/a)
==22726== Address 0x0 is not stack'd, malloc'd or (recently) free'd
So, how can i write this code efficiently, how can i allocate memory don't use extra memory to read text file ?
c malloc valgrind
I need to write a program which read a text file for example:
Peter
Jack
John
In here main aim is dont's use extra memory. And so i wrote this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(){
FILE *file_pointer;
file_pointer = fopen("humans.txt","r");
value = (char *) malloc(sizeof(char)); // I think its most possible
//writing with don't use extra memory
while( fscanf(file_pointer,"%s", value) != EOF ){
printf("Name: %s Size of name: %d",value,strlen(value));
}
}
when i run with some text file sample, there was no problem but when i run with valgrind, it gave me this error:
==22726== Invalid read of size 4
==22726== at 0x4EA21BD: __isoc99_fscanf (in /usr/lib64/libc-2.17.so)
==22726== by 0x400716: main (in /mnt//ogr/bxxxxx/a)
==22726== Address 0x0 is not stack'd, malloc'd or (recently) free'd
So, how can i write this code efficiently, how can i allocate memory don't use extra memory to read text file ?
c malloc valgrind
c malloc valgrind
asked Nov 18 '18 at 8:31
okydokyokydoky
136
136
You seem to have forgotten thatcharstrings in C are really called null-terminated byte strings. That null-terminated bit is crucial and marks the end of the string. It also means that a string of N characters needs space for N+1 to fit the terminator. Sincescanfwill read at least one character and then add the terminator, it will write at least two bytes to the memory pointed to by the argument. If you don't know the size, you need to read one character at a time, and reallocate as needed.
– Some programmer dude
Nov 18 '18 at 8:36
I have forgotten this, thanks to remark
– okydoky
Nov 18 '18 at 8:41
add a comment |
You seem to have forgotten thatcharstrings in C are really called null-terminated byte strings. That null-terminated bit is crucial and marks the end of the string. It also means that a string of N characters needs space for N+1 to fit the terminator. Sincescanfwill read at least one character and then add the terminator, it will write at least two bytes to the memory pointed to by the argument. If you don't know the size, you need to read one character at a time, and reallocate as needed.
– Some programmer dude
Nov 18 '18 at 8:36
I have forgotten this, thanks to remark
– okydoky
Nov 18 '18 at 8:41
You seem to have forgotten that
char strings in C are really called null-terminated byte strings. That null-terminated bit is crucial and marks the end of the string. It also means that a string of N characters needs space for N+1 to fit the terminator. Since scanf will read at least one character and then add the terminator, it will write at least two bytes to the memory pointed to by the argument. If you don't know the size, you need to read one character at a time, and reallocate as needed.– Some programmer dude
Nov 18 '18 at 8:36
You seem to have forgotten that
char strings in C are really called null-terminated byte strings. That null-terminated bit is crucial and marks the end of the string. It also means that a string of N characters needs space for N+1 to fit the terminator. Since scanf will read at least one character and then add the terminator, it will write at least two bytes to the memory pointed to by the argument. If you don't know the size, you need to read one character at a time, and reallocate as needed.– Some programmer dude
Nov 18 '18 at 8:36
I have forgotten this, thanks to remark
– okydoky
Nov 18 '18 at 8:41
I have forgotten this, thanks to remark
– okydoky
Nov 18 '18 at 8:41
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53359106%2fread-file-dynamically-and-dont-use-unnecessary-memory-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53359106%2fread-file-dynamically-and-dont-use-unnecessary-memory-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You seem to have forgotten that
charstrings in C are really called null-terminated byte strings. That null-terminated bit is crucial and marks the end of the string. It also means that a string of N characters needs space for N+1 to fit the terminator. Sincescanfwill read at least one character and then add the terminator, it will write at least two bytes to the memory pointed to by the argument. If you don't know the size, you need to read one character at a time, and reallocate as needed.– Some programmer dude
Nov 18 '18 at 8:36
I have forgotten this, thanks to remark
– okydoky
Nov 18 '18 at 8:41