Click here to Skip to main content
14,981,770 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
If I have a structure:

struct user {
    char* username;
    char* password;
    int ID;

And I create an object like this:
user user1;
Here the object user1 has the size in memory of POINTER + POINTER + INT ?

But if I want to expand the user to:
user1.username = "Hellen";

What happens here ? The object expands and add data after the pointer? Or the pointer itself in the strucure also has the space of the INT ?

I want to read the data from the file and I dont know how to proceed.

Read the struct starting with 8 characters or starting with the pointer ? I need to know how much amount of data to read.
Updated 6-Dec-14 15:33pm

I think independent of read/write from/to a file, you need to understand pointers in C/C++. See any tutorial on pointers, e.g.[^].

Since you work in C++, I strongly recommend not to use plain char pointers for string handling, but rather the std::string class that takes care of memory management.

bobos_underground 7-Dec-14 3:38am
I read tutorials for the pointer and I know that struct has pointers that may adress or not some variable in the memory.
What I dont understand how the pointer size and space(as the size of the pointer in the memory) is managed on the memory. For example char *username beiong a char pointer is something phisical in the memory before pointing to something it must be held somewhere.

My question was: If I write a pointer that leads to no portion of memory to a file that file will contain a portion of data that is the pointer itself ? (not the data that it links)?

If the struct contains two pointers that will lead nowhere in the memory and I write the struct to file will the file contain the pointers since pointers are also spaces in memory ?

If I assign some chars to the 2 pointers from the struct when I write the struct to file the pointer data in the memory will not be taken in consideration and only what the pointers are pointed to ?
Andreas Gieriet 7-Dec-14 8:11am
Forget about writing to the file for a moment as long as you do not master the pointers.
Your understanding of pointers is still broken.

A pointer variable is a variable that holds an address.
You may set it to NULL to tell unambiguously that no item is referenced.
Any address not equal to NULL tells us that the variable holds an address to some memory location.
If the address points to some legal memory location, you can access that memory location.

In your case, the variable points to a character. You know that this is the first character of an array of characters (this is your responsibility as a programmer to know if this is pointing to one character only or if this is the beginning of an array of characters).

Now to the writing to a file: If you write the struct to a file, only the content of the struct is written... I.e. the struct contains an int and two pointer variables. So you get the int and two addresses written to the file.

If you want to write the referenced data instead of the addresses only, you have to implement that explicitly.

In any case: why the heck do you employ char pointers instead of std::string - you are programming in C++! Using character pointers for strings is the "wrong" way in C++. C++ hides memory management in all the STL data structures. Take benefit of that!

markkuk 8-Dec-14 6:16am
Writing pointers to a file is completely useless. Your code must write the actual data that's pointed to instead of the pointer values. This doesn't happen automatically.
user1.username = "Hellen";

This does not change the size of the structure, it just sets username to point to the string "Hellen", which is stored somewhere else in memory. If you now write the structure to a file, then you lose all the data, since all you are writing is two pointers and an integer. You would be better to use classes instead of structs, and study serialisation to ensure that you can persist your objects on disk when necessary.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900