0

Let me describe my problem. I want to create a list with a multitude of items. I am using a struct like this :

typedef struct Node{
                    char *element;
                    node *next;
                    node *prev;
}node;

Now, in my program I initialize a pointer as a pointer that points to such a struct :

temp = (node*)malloc(sizeof(node));
temp->element = (char*)malloc(sizeof(char)*maximum);
temp = InitElement(temp, maximum);

At first I give to my program that :

maximum = 10;

So far no problems. But my problem begins now : Edit/ Sigh. I'll try to make it simple :

I am making a list of elements that represent operands of an infix expression.

I am using my list as a Stack.

Each time my program detects an operand in the Initial Input of the user, it Pops the previous 2 nodes in order to combine them in an expression.

For example : If it detects '+' after having stored 234 and 2 then it will pop the previous 2 nodes, create a new node which will be (234+2) and push it back.

Now for my problem. I need my max size of the string to be strictly 10 unless it is a parenthesis, a full operation as an element. That is what I need to change in my program. But I also need it to return to how it was initially afterwards. How can I do it WITHOUT affecting any existing node on the list via temp with the size increase?

  • 3
    [Don't cast the result of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – UnholySheep Mar 20 '17 at 08:07
  • 4
    what you're asking is really unclear... – Jean-François Fabre Mar 20 '17 at 08:08
  • The purpose of a LL is to have constant time insertion and deletion anywhere inside the list... If you're asking whether you can increase the number of elements, then yes you can... – StoryTeller - Unslander Monica Mar 20 '17 at 08:11
  • Why do you need to `malloc` with a fixed maximum size? Just `strdup` the new string and `free` the old one. – David Ranieri Mar 20 '17 at 08:12
  • "Should I use an entirely new pointer" makes no sense. You have `temp->element`. That's the pointer. You can assign any value you like to it. – Mike Nakis Mar 20 '17 at 08:12
  • 3
    "Can I delete a pointer without deleting the memory it points to" makes no sense. When we say "delete a pointer" we do not really mean deleting the pointer. We mean deleting the memory it points to. There is no confusion because the former is an impossibility, so the latter is implied. – Mike Nakis Mar 20 '17 at 08:13
  • 1
    "I know that free() deletes a pointer but also the memory contents" Makes no sense. It seems that there is a fundamental flaw in your understanding of pointers and memory allocation. You are not going to have that solved by asking questions on stackoverflow. You are going to have to do some reading. – Mike Nakis Mar 20 '17 at 08:15
  • Just copy the old string, realloc the size, copy it back – kaldoran Mar 20 '17 at 08:17
  • The pointer itself is stored in the stack, in the function call memory. It will delete itself. You just assign it to point some place else. You only need to delete mallocs. – k_kaz Mar 20 '17 at 08:18
  • "Also realloc() would change the initial pointer size" Makes no sense. There is no such thing as an initial pointer size. All pointers have a fixed size which is 4 or 8 bytes, depending on whether you are on a 32-bit or a 64-bit architecture. The size of the block of memory a pointer points to may change. But there isn't even such a thing as changing the initial block size. When you change the size of a block of memory, the block has a new size. The initial size is a non-issue. – Mike Nakis Mar 20 '17 at 08:18
  • "but I need the nodes to stay as I need them on the list" what on earth does "stay" mean, and what on earth does your list of nodes have to do with the pointer to characters contained within a node? – Mike Nakis Mar 20 '17 at 08:21

1 Answers1

-1

You must know that every memory block allocated for your elements is independed from another one. So you can allocate (or reallocate) memory block with size that you really need in the moment when you copy data in it and it will not affect any other block.

struct {
...
} my_data;

temp->element = (char*) alloc( sizeof(my_data) );
memmove( temp->element, &my_data, sizeof(my_data) );
Alexander Ushakov
  • 5,139
  • 3
  • 27
  • 50
  • You still [shouldn't cast the return of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). And `alloc` is not a standard C function – UnholySheep Mar 20 '17 at 08:20