LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-25-2017, 11:44 AM   #1
Linusovic
LQ Newbie
 
Registered: Apr 2016
Posts: 8

Rep: Reputation: Disabled
g_stpcpy: assertion 'dest != NULL' failed [gtk+]


I'm getting the this message when i'm trying to run this code ( in this function ).

GLib-CRITICAL **: g_stpcpy: assertion 'dest != NULL' failed

This is my functions code :

static void voltagelabelfunction(GtkWidget *widget, AllaVariabler *test){

char* str1;
char* str2;
char* str3;
str1 = "sssss";
str2 = "kkkk";
const gchar *text = gtk_entry_get_text(GTK_ENTRY(test->evoltage));
g_stpcpy (str3, text);
printf("str3 = %s\n", str3); //currently prints out str3 = (null)
}

This is inspired by the code below, I would like to merge two strings.

int main(int argc, char** argv) {

char* str1;
char* str2;
str1 = "sssss";
str2 = "kkkk";
char * str3 = (char *) malloc(1 +sizeof(char*) * (strlen(str1)+ strlen(str2)));
strcpy(str3, str1);
strcat(str3, str2);
printf("%s", str3);

return 0;
}

//Best regards Linusovic
 
Old 05-25-2017, 12:50 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,194

Rep: Reputation: 1388Reputation: 1388Reputation: 1388Reputation: 1388Reputation: 1388Reputation: 1388Reputation: 1388Reputation: 1388Reputation: 1388Reputation: 1388
Quote:
Originally Posted by Linusovic View Post
This is inspired by the code below, I would like to merge two strings.
You missed the part that allocates the destination buffer:
Code:
      char * str3 = (char *) malloc(1 +sizeof(char*) * (strlen(str1)+ strlen(str2)));
 
1 members found this post helpful.
Old 05-25-2017, 01:16 PM   #3
Linusovic
LQ Newbie
 
Registered: Apr 2016
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
You missed the part that allocates the destination buffer:
Code:
      char * str3 = (char *) malloc(1 +sizeof(char*) * (strlen(str1)+ strlen(str2)));
thx alot! it worked =)
 
Old 05-25-2017, 01:29 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,194

Rep: Reputation: 1388Reputation: 1388Reputation: 1388Reputation: 1388Reputation: 1388Reputation: 1388Reputation: 1388Reputation: 1388Reputation: 1388Reputation: 1388
Code:
      char * str3 = (char *) malloc(1 +sizeof(char*) * (strlen(str1)+ strlen(str2)));
Although, looking at this again, it's allocating too much memory, that should be sizeof(char) which is the same as 1, which is redundant. The cast in front of malloc is also redundant, unless you're compiling C with a C++ compiler.

Code:
char * str3 = malloc(1 + strlen(str1) + strlen(str2));
 
1 members found this post helpful.
Old 05-26-2017, 05:12 PM   #5
Linusovic
LQ Newbie
 
Registered: Apr 2016
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
Code:
      char * str3 = (char *) malloc(1 +sizeof(char*) * (strlen(str1)+ strlen(str2)));
Although, looking at this again, it's allocating too much memory, that should be sizeof(char) which is the same as 1, which is redundant. The cast in front of malloc is also redundant, unless you're compiling C with a C++ compiler.

Code:
char * str3 = malloc(1 + strlen(str1) + strlen(str2));
Oh well i read somewhere that i needed the 1 + for the \0 end of the string. not sure if right or not. But also i'm compiling in c++ ^^
 
Old 05-26-2017, 05:51 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,194

Rep: Reputation: 1388Reputation: 1388Reputation: 1388Reputation: 1388Reputation: 1388Reputation: 1388Reputation: 1388Reputation: 1388Reputation: 1388Reputation: 1388
Quote:
Originally Posted by Linusovic View Post
Oh well i read somewhere that i needed the 1 + for the \0 end of the string.
That's correct. The extra part was the sizeof(char*).
 
Old 05-26-2017, 06:37 PM   #7
Linusovic
LQ Newbie
 
Registered: Apr 2016
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
That's correct. The extra part was the sizeof(char*).
Ahh, makes sense. Didn't see the highlighted thing you did before!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Assertion `initialized()' failed. Samrita Linux - Software 4 05-09-2016 08:53 AM
openCV: Assertion failed alishba Linux - Newbie 1 11-02-2015 03:08 PM
g_error_copy: assertion `error != NULL' failed chuangsheng Programming 0 06-13-2009 01:08 AM
openldap assertion error -- ldap_parse_result: Assertion `r != ((void *)0)' failed nottings Linux - Server 1 02-19-2009 12:20 AM
assertion failed TIFFDC101 General 3 09-19-2005 08:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration