Solved

How to read a string from memory in C?

Posted on 2008-10-09
8
205 Views
Last Modified: 2012-05-05
hello group,

I've two pointers which one points to beginning of a string in memory and also a 2nd pointer pointing to the end of it. Of course, using while() loop I can read it but how can I store it into a dynamic array or variable?

thanks,
ak
0
Comment
Question by:akohan
8 Comments
 

Author Comment

by:akohan
ID: 22683799


Ok I have done it this way but when I'm trying to close it by contatenating '\0' it causes segmentation error. Any idea?



range = end_addr-start_addr
char* p = malloc(range);
 
strncpy(p, res1, range);
strcat(p, '\0');        /* this causes segmentation error !*/

Open in new window

0
 
LVL 10

Expert Comment

by:peetm
ID: 22683853
Well, depeding upon the difference between the addresses, you could store it in a a piece of memory that's the sizeof whatever the pointers point to [type], do some subtraction, ans then love he result?
0
 

Author Comment

by:akohan
ID: 22683857

Ok, for this issue I also did as following in snippet. Any idea if this is a safe way?

Thanks.


char* p = '\0';
p = ((char*) malloc(range));
 
strncpy(p, start_addr, range);
 
//now the string is pointed by p.

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 10

Assisted Solution

by:peetm
peetm earned 90 total points
ID: 22683954
>>char* p = '\0';
p = ((char*) malloc(range));
 
strncpy(p, start_addr, range);
 
//now the string is pointed by p.

---

The cast on p = ((char*) malloc(range)); isn't necessary

And you should check that p, after the alloc isnt NULL

Whats range?

If all of the above works, you might check that theres the not the classic off by one  a Google test.


0
 

Author Comment

by:akohan
ID: 22684129


Hi thanks for the heads up. One thing that your last line is mixed with some unicode characters and had made it hard to read. Can you please explain what it means?

regarding your question, range is the difference between starting and ending memory space string lies in.

Regards,
ak
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 110 total points
ID: 22684296
>char* p = '\0';
>p = ((char*) malloc(range));
 >strncpy(p, start_addr, range);

If your string was "ab" - what would be your range? If your end_ptr was pointing to b then it would be 1 ... If it was pointing to \0 following b then it would be 2. You need 3 chars to store this string so you may have to add 2 or 1 to your range depending on where end_ptr points

>strcat(p, '\0');
second argument of strcat has to be char * or const char * ... you are passing a char - a low value that is being treated by strcat is an address - hence the segmentation fault.

You would rather do something like *(p+n) = '\0';
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 100 total points
ID: 22684598
>> strncpy(p, res1, range);
>> strcat(p, '\0');        /* this causes segmentation error !*/

Note also that you probably didn't copy a trainling '\0' character with the strncpy, so you cannot use strcat after that, since it depends on the '\0' already being there.

Second :

>> char* p = malloc(range);

You only allocated memory for 'range' characters - you need one more for the trailing '\0' character.
0
 

Author Comment

by:akohan
ID: 22689852

Thank you all for your advice.

Regards.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why this code doesn't work? 8 109
IIS Log files on Exchange 2013 server 6 184
What is atomic operation? 6 65
C qsort compare function issue 6 18
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question