1

I am working on a short program that reads a .txt file. Intially, I was playing around in main function, and I had gotten to my code to work just fine. Later, I decided to abstract it to a function. Now, I cannot seem to get my code to work, and I have been hung up on this problem for quite some time.

I think my biggest issue is that I don't really understand what is going on at a memory/hardware level. I understand that a pointer simply holds a memory address, and a pointer to a pointer simply holds a memory address to an another memory address, a short breadcrumb trail to what we really want.

Yet, now that I am introducing malloc() to expand the amount of memory allocated, I seem to lose sight of whats going on. In fact, I am not really sure how to think of memory at all anymore.

So, a char takes up a single byte, correct? If I understand correctly, then by a char* takes up a single byte of memory? If we were to have a:

char* str = "hello"

Would it be say safe to assume that it takes up 6 bytes of memory (including the null character)?

And, if we wanted to allocate memory for some "size" unknown at compile time, then we would need to dynamically allocate memory.

int size = determine_size();
char* str = NULL;
str = (char*)malloc(size * sizeof(char));

Is this syntactically correct so far? Now, if you would judge my interpretation. We are telling the compiler that we need "size" number of contiguous memory reserved for chars. If size was equal to 10, then str* would point to the first address of 10 memory addresses, correct?

Now, if we could go one step further.

int size = determine_size();
char* str = NULL;
file_read("filename.txt", size, &str);

This is where my feet start to leave the ground. My interpretation is that file_read() looks something like this:

int file_read(char* filename, int size, char** buffer) {
    // Set up FILE stream 

    // Allocate memory to buffer
    buffer = malloc(size * sizeof(char));

    // Add characters to buffer
    int i = 0;
    char c;
    while((c=fgetc(file))!=EOF){
         *(buffer + i) = (char)c;
         i++;
    }

Adding the characters to the buffer and allocating the memory is what is I cannot seem to wrap my head around.

If **buffer is pointing to *str which is equal to null, then how do I allocate memory to *str and add characters to it?

I understand that this is lengthy, but I appreciate the time you all are taking to read this! Let me know if I can clarify anything.

EDIT:

Whoa, my code is working now, thanks so much!

Although, I don't know why this works:

*((*buffer) + i) = (char)c;
12
  • buffer = malloc(size * sizeof(char)); --> *buffer = malloc(size * sizeof(char)); Commented Sep 21, 2017 at 21:05
  • str = (char*)(size * sizeof(char); should be str = (char*)malloc(size * sizeof(char); Commented Sep 21, 2017 at 21:06
  • 1
    char c; while((c=fgetc(file))!=EOF){ *(buffer + i) = (char)c;i++; } --> int c; while((c=fgetc(file))!=EOF && i < size-1){ (*buffer)[i++] = c;} (*buffer)[i] = 0; Commented Sep 21, 2017 at 21:07
  • 1
    That's a novel, not a question. Learn How to Ask and provide a minimal reproducible example. Commented Sep 21, 2017 at 21:23
  • 1
    @FaridKaradsheh As BLUEPIXY pointed out the variable c should be declared as having type int instead of char because in general char can behave as unsigned character. In this case the condition (c=fgetc(file))!=EOF will be always true.:) Commented Sep 21, 2017 at 21:26

2 Answers 2

3

So, a char takes up a single byte, correct?

Yes.

If I understand correctly, by default a char* takes up a single byte of memory.

Your wording is somewhat ambiguous. A char takes up a single byte of memory. A char * can point to one char, i.e. one byte of memory, or a char array, i.e. multiple bytes of memory.

The pointer itself takes up more than a single byte. The exact value is implementation-defined, usually 4 bytes (32bit) or 8 bytes (64bit). You can check the exact value with printf( "%zd\n", sizeof char * ).

If we were to have a char* str = "hello", would it be say safe to assume that it takes up 6 bytes of memory (including the null character)?

Yes.

And, if we wanted to allocate memory for some "size" unknown at compile time, then we would need to dynamically allocate memory.

int size = determine_size();
char* str = NULL;
str = (char*)malloc(size * sizeof(char));

Is this syntactically correct so far?

Do not cast the result of malloc. And sizeof char is by definition always 1.

If size was equal to 10, then str* would point to the first address of 10 memory addresses, correct?

Yes. Well, almost. str* makes no sense, and it's 10 chars, not 10 memory addresses. But str would point to the first of the 10 chars, yes.

Now, if we could go one step further.

int size = determine_size();
char* str = NULL;
file_read("filename.txt", size, &str);

This is where my feet start to leave the ground. My interpretation is that file_read() looks something like this:

int file_read(char* filename, int size, char** buffer) {
    // Set up FILE stream 

    // Allocate memory to buffer
    buffer = malloc(size * sizeof(char));

No. You would write *buffer = malloc( size );. The idea is that the memory you are allocating inside the function can be addressed by the caller of the function. So the pointer provided by the caller -- str, which is NULL at the point of the call -- needs to be changed. That is why the caller passes the address of str, so you can write the pointer returned by malloc() to that address. After your function returns, the caller's str will no longer be NULL, but contain the address returned by malloc().

buffer is the address of str, passed to the function by value. Allocating to buffer would only change that (local) pointer value.

Allocating to *buffer, on the other hand, is the same as allocating to str. The caller will "see" the change to str after your file_read() returns.


Although, I don't know why this works: *((*buffer) + i) = (char)c;

  • buffer is the address of str.
  • *buffer is, basically, the same as str -- a pointer to char (array).
  • (*buffer) + i) is pointer arithmetic -- the pointer *buffer plus i means a pointer to the ith element of the array.
  • *((*buffer) + i) is dereferencing that pointer to the ith element -- a single char.
  • to which you are then assigning (char)c.

A simpler expression doing the same thing would be:

(*buffer)[i] = (char)c;
Sign up to request clarification or add additional context in comments.

11 Comments

I realize now that I forgot the malloc(). I didn't copy my source exactly. Would it be the first of 10 or 11 memory addresses (+1 for null character)? Thanks, the final bit really clarifies it for me! Also, since char is equal to 1, then we don't need to use sizeof(), right? Which would then make the code more efficent.
Minor: "safe to assume that it takes up 6 bytes " -- more likely 6 for the literal and 4 or 8 for the pointer.
@chux Is it 4 or 8 due to padding the location with zeros?
@FaridKaradsheh: I am not quite sure what chux is about, either. The pointer itself takes up some space -- usually 4 (32bit) or 8 (64bit) bytes these days. The string literal it points to takes up 6 bytes -- 5 for the characters and one for the terminating null byte. But you were referring to what the pointer points to, not the space taken up by the pointer itself.
@FaridKaradsheh Depends on the implementation.
|
2

with char **buffer, buffer stands for the pointer to the pointer to the char, *buffer accesses the pointer to a char, and **buffer accesses the char value itself.

To pass back a pointer to a new array of chars, write *buffer = malloc(size).

To write values into the char array, write *((*buffer) + i) = c, or (probably simpler) (*buffer)[i] = c

See the following snippet demonstrating what's going on:

void generate0to9(char** buffer) {

    *buffer = malloc(11);  // *buffer dereferences the pointer to the pointer buffer one time, i.e. it writes a (new) pointer value into the address passed in by `buffer`
    for (int i=0;i<=9;i++) {
        //*((*buffer)+i) = '0' + i;
        (*buffer)[i] = '0' + i;
    }
    (*buffer)[10]='\0';
}

int main(void) {

    char *b = NULL;
    generate0to9(&b);  // pass a pointer to the pointer b, such that the pointer`s value can be changed in the function 
    printf("b: %s\n", b);
    free(b);
    return 0;
}

Output:

0123456789

2 Comments

So, '*buffer = malloc(size)' is changing *str? That is what I was thinking as well. Could you explain what is going on in your third paragraph/sentence?
@Stephan Lechner *buffer[i] = c; is an invalid statement Should be (*buffer)[i] = c

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.