-1

In the code which I have pasted below, after I copy the contents of split data i,e "he", "ll" and "oo" to new data array (which is 2d array) . I am freeing the StringSplit[i]. But after freeing, i am not able to see the data which i copied to 2d array newData also. what is happening in this code and how do i ensure the data in varible "newData" is saved. and newData is global variable

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

int main() 
{
    char *data= "Helloo";
    StringOperations(data);
    return 0;
}
Roops
  • 1
  • 3
  • 1
    Note that 1. `malloc(const_length)` allocates too little memory. 2. If it's really const length, you should not use `malloc()` because it's slower and because it's harder to then `free()` all the `malloc()`ed pointers at the right time. – Iharob Al Asimi Sep 21 '17 at 13:26
  • 2
    There is no 2D array in your code and nothing which can point to one. `char **` is a pointer to pointer, a pointer is not an array. – too honest for this site Sep 21 '17 at 13:28
  • What is your **specific** question? We are not an "explain/write" my code site. – too honest for this site Sep 21 '17 at 13:29
  • Well I closed as duplicate to "how to copy strings", but upon closer inspection this code has _lots_ of other major problems. Basically, you need to actually know what you are doing, you can't program by "take a chance" trial & error. – Lundin Sep 21 '17 at 13:40
  • @Lundin people learn this way, but they use debuggers, try and test their ideas -and they do not write posts on the forums with questions which can be answered by stepping through the code – 0___________ Sep 21 '17 at 13:56
  • 1
    @PeterJ_01 "they do not write posts on the forums with questions which can be answered by stepping through the code" I take it you are being sarcastic? Because questions without any debug effort by the OP what-so-ever are _quite_ common around here... – Lundin Sep 21 '17 at 14:01
  • @Lundin I think that my intentions are obvious ... :) – 0___________ Sep 21 '17 at 14:37

2 Answers2

0

There are 2 serious problems with your code,

  1. There is always one more byte for a string in so, you should have

    int const_length = 3;
    
  2. You free() StringSplit[i] and then dereference the pointer you assigned to it previously. Pointers are just that, they point to memory. Assigning a pointer to another does not create new memory and you dereference exactly the same poitner you just free()ed.

  3. Not so serious, but you should check if malloc() returns a valid non-NULL pointer.

  4. Not serious at all, but casting the return value of malloc() is considered bad practice.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • How to do fix this issue.. can you give me example. I did not understand how to solve it :( – Roops Sep 21 '17 at 13:46
  • If you need to use the pointer don't `free()` it, use `free()` exactly when you NO LONGER NEED THE DATA. – Iharob Al Asimi Sep 21 '17 at 13:51
  • If I want to free the StringSplit[i] in a different function for example​.. how do I free it? Because I won't have variable 'i' in new function as it local variable.. – Roops Sep 21 '17 at 13:56
-1
newData[j]=StringSplit[i];

newData[j] is pointing where StringSplit[i] is. So effectively you are freeing memory where both are pointing.

Shashwat Kumar
  • 5,159
  • 2
  • 30
  • 66